Need help with autocomplete script!

6 posts by 2 authors in: Forums > CMS Builder
Last Post: September 13, 2019   (RSS)

By nmsinc - September 8, 2019 - edited: September 8, 2019

The script below was written by the pros at Interactive Tools a few years back and works great! 

The result of the code in red below is what is listed in the autocomplete and is what is now recorded upon file save. I still need this information to show, however I want the account number associated with the file recorded and not the associated text!

Any ideas?

Thanks - nmsinc

   <div class="form-group">
   <label for="debtor">Name of Debtor</label>
   <input name='debtor' class='form-control' id='debtor' />
   <script>
   $( "#debtor" ).autocomplete({  source: [
   <?php foreach (mysql_select("debtor", "agency ORDER BY debtor_name") as $Groups): ?>
   <?php if ($CURRENT_USER['agency'] == $Groups['agency']): ?>
   "<?php echo $Groups['debtor_name'];?> - <?php echo $Groups['debtor_address_1'];?>  <?php echo $Groups['debtor_city'];?> <?php echo $Groups['debtor_state'];?>",
   <?php endif; ?>
   <?php endforeach ?>
   ]});
   </script>
   </div>

nmsinc

Hi nmsinc,

It looks like this is using the jQuery UI autocomplete function. If that's the case, you should be able to do something like the following to differentiate the value saved from the label displayed:

   $( "#debtor" ).autocomplete({  source: [
   <?php foreach (mysql_select("debtor", "agency ORDER BY debtor_name") as $Groups): ?>
   <?php if ($CURRENT_USER['agency'] == $Groups['agency']): ?>
   {
   value: "<?php echo $Groups['num']; ?>",
   label: "<?php echo $Groups['debtor_name'];?> - <?php echo $Groups['debtor_address_1'];?>  <?php echo $Groups['debtor_city'];?> <?php echo $Groups['debtor_state'];?>"
   },
   <?php endif; ?>
   <?php endforeach ?>
   ]});

Note that you may need to change some details to match your specific needs. Let me know if that gets you on the right track, or if you have any other questions!

Thanks,

Daniel
Technical Lead
interactivetools.com

Hi nmsinc

is their a means to keep the name in the search box and pass the value upon submission?

Could you explain this in a bit more detail? E.g. what is the current behaviour, and how would you like it to change?

Also, payment is not necessary, but your support is appreciated! You can always consider making a donation the next time you upgrade CMSB :) (https://www.interactivetools.com/upgrade/)

Thanks,

Daniel
Technical Lead
interactivetools.com

Once the selection is made from the autofill script the box shows the value of the record a not the autofill label text!!

nmsinc

Hi nmsinc,

Oh yes, I see now what you're talking about. I think there is a way to do something like how you want, but it's rather complex with this sort of autocomplete. It actually sounds like what you want is closer to a select box than a text field. I would recommend looking at using a select dropdown with the Select2 plugin (https://select2.org/). A select dropdown will let you use the value:label combination how you're wanting here, and the Select2 plugin adds some type-to-search functionality.

I hope that points you in the right direction; let me know if you have any further questions!

Thanks,

Daniel
Technical Lead
interactivetools.com