简体   繁体   中英

populate textbox based on combobox value using ajax

I badly need your help on this. I can't seem to find what's wrong in my code.

The case is, I am trying to populate my textbox field based on my combobox value and it is connected in sql database. I have tried some codes on the web and then I found a code which seems accurate but I can't seem to display the result on my textbox.

here is my HTML Code:

<?php 
echo"Concept Store:";
echo "<select width='100' id='strs' name='strs'  >";
echo "<option></option>";
  while($row=sqlsrv_fetch_array($stmt))
    {
        $x= $row['strnm'];
                echo " <option> $x</option>" ;
    }
      echo "</select>";
?>
 Address &nbsp&nbsp&nbsp: <input type="text" id="add" name="add" size="27" /><br><br>

here's the AJAX:

 <script type="text/javascript">
  $(document).ready(function(){
    $('#strs').change(function(){
      $.post("gadd.php",{strs:$(this.val() )},function(result){
            $("#add").val(result);
      });
   });
  });
 </script>

and here's my 'gadd.php'

<?php

session_start();
include ('sqlconn.php');
$gadd=$_post['strs'];

//$t1= mysql_real_escape_string($t1);
$sql="Select distinct dadd1 from ostore where strnm='".$gadd."' ";
$stmt = sqlsrv_query($conn,$sql);
//echo "<option></option>";
while ($row = sqlsrv_fetch_array($stmt))
{
      // $type2=$row['dadd1'];
       echo $row['dadd1'];
        //echo '<option value ="'.$type2.'">'.$type2.'</option>';

}

?>

if you could help me, that would be really really awesome thank you!

Check it. Global variables should be in upper case.

$gadd = $_POST['strs'];

First, make sure you use the proper format for global variables $gadd = $_POST['strs'];

Second, check your query if it is working, try this

$sql=("Select distinct dadd1 from ostore where strnm = '$gadd'");

Most problably it's because you're missing the value in the option. Use this:

echo " <option value='$x'> $x</option>" ;

instead of this:

echo " <option> $x</option>" ;

If still don't work, like was said before check if the query is working. You can do that in chrome by the option "Inspect element" and the go to the "Network" tab. You can see what is sent through ajax and the return data.

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