简体   繁体   中英

how to get value from php select item with java script, or jquery, where select item value is from query mysql

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?

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);
    });


});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM