2

i have autocomplete function in jquery like this

$(document).ready(function(){
    `...`
$("#tag").autocomplete("autocomplete.php?bro="+valBro, {
        selectFirst: true
    });`

i need to get value from selected item from my php code for my valBro variabel, my php code like this:

<select name="region_name" id="region_name"> 
<?php
$regionQuery = mysql_query("SELECT * FROM region ORDER BY id_region");
while($reg=mysql_fetch_array($regionQuery)){
echo "<option value=\"$reg[id_region]\">$reg[region_name]</option>\n";
          }
 ?>
    </select>

I have try to add jquery code in my above function

 var valBro=$("#region_name").val();

but give me nothing I have also try to add

var valBro=document.getElementById("region_name").value;

I have try to set valBro variabel as global variabel, and call change.function like this before function autocomplete

$("#region_name").change(function(){
valBro = $("#region_name").val();

}); And got nothing... Any help?

2
  • Do you get any console errors? Commented Jul 6, 2015 at 2:17
  • no there is no error, is it possible to get the value from html objeck where the value from query? Commented Jul 6, 2015 at 2:23

1 Answer 1

2

Your data shouldn't be show because you are not providing any query in this line

$reg=mysql_fetch_array($region)

but your query is in this variable

$regionQuery = mysql_query("SELECT * FROM region ORDER BY id_region");

Try like this

$reg=mysql_fetch_array($regionQuery)

EDIT

Javascript

$(document).ready(function() {

    var tag = $("#tag"),   
    region_name = $("#region_name"),
    options = region_name.find("option");
    var value = options.filter(":selected").attr("value");
    tag.autocomplete({
        source: "autocomplete.php?bro=" + value,
        selectFirst: true
    });

    region_name.on("change", function() {
        value = options.filter(":selected").attr("value");
        tag.autocomplete("option", "source", "autocomplete.php?bro=" + value);
    });


});
Sign up to request clarification or add additional context in comments.

5 Comments

sory, i have misedit my code region from my original code, because the code in my country language, but in my original code i have $reg=mysql_fetch_array($regionQuery), i dont get error console. Any help?
Where did you put it var valBro=$("#region_name").val(); ?
in exacly above my autocomplet function
$("#region_name").val(); return the selected value of select. it called before the php generate value. add a timeout to get the value
sorry i am new be in php or java script, can you more detail, how i add timeout to get the value?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.