javascript - Selecting an autocomplete list does not save the word fully when using ajax to save -


i built autocomplete list of cities using mysql query. typing in input field correctly initiates search , builds selectable city list. problem city name in full not saved, characters typed when using .on('change'... tried other mouse events such mouseover, mousemove, mouseout, mouseenter, mouseleave - , require user first select item list, move out, swipe cursor on input second time trigger save of selected city name. html looks like:

<input type="text" onmouseover="this.focus();" class="savecity" name="pob_city" member_id="<?php echo $member_id; ?>" value="<?php echo $pob_city; ?>" placeholder="city" id="pob_city" /> <script language="javascript" type="text/javascript"> $("#pob_city").coolautosuggest({ url:"tools/autosuggest/query_cities.php?chars=" }); </script> 

the savecitycountry javascript:

$(document).on('mouseout','.savecity',function() {   var data=$(this).val();   var n =remove_whitespaces(data);   var member_id=$(this).attr('member_id');   $("#results").html( "" );   if(n  !=''){   var datastring = 'pob_city='+ n +'&member_id='+member_id;     $.ajax({       type: "post",       url: "savedata.php",       data: datastring,       cache: false,       success: function(html)       {         $("#results").html( html );       }     });   } }) 

you can see demo at: http://dottedi.us/misc/ajaxsave/

the other thing bothersome ordinary input fields such first or last name, on change event triggers when click somewhere else on page, placebo "save" button.

update: cleaned code in example splitting off city country , updated post reference savecity. not affect result, bit easier follow.

inside http://dottedi.us/misc/ajaxsave/tools/autosuggest/js/ folder 2 javascript files might relevant, jquery.coolautosuggest.js , jquery.coolfieldset.js. on line 46 of jquery.coolautosuggest.js see success:function(data){. want me place "source: data, autofocus: true, delay: 0, minlength: 0, select: response, response: response" somewhere in here? please make clear , right syntax, semicolin, etc.

in codeblock added, assume change html adding ,response , function response gets placed somewhere within savedata.js. once right, go change opposed mouseout or mouseover?

include response function in autocomplete call, can assign selected value hidden field, allow retain full text.

i assume getting array of cities, store each city in array in key called "city" , return it.

define 1 hidden field "selected_city".

inside coolautosuggest success function place code

source: data, autofocus: true, delay: 0, minlength: 0, select: response, response: response

$("#pob_city").coolautosuggest({ url:"tools/autosuggest/query_cities.php?chars=" },response);  function response(){    if (typeof ui.content != "undefined" && ui.content.length === 1) {       city= ui.content[0].city;    }    else if (typeof ui.item != "undefined" && ui.item.label.length > 0) {       city= ui.item.label;    }    if (label.length > 0 || index.length > 0) {       $("#selected_city").val(city);       // value can use display    } } 

before doing please change below function on jquery.coolautosuggest.js @ line 120

if(me.submitonselect==true){ $("form").has(me.textfield).submit(); }

to

if(me.submitonselect==true){ $("#pob_city").val($(this).find(".suggestion_title").text()); $("form").has(me.textfield).submit(); }


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -