简体   繁体   English

使用ajax根据组合框值填充文本框

[英]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. 情况是,我试图根据我的组合框值填充我的文本框字段,并且它已连接到sql数据库中。 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: 这是我的HTML代码:

<?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: 这是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' 这是我的“ 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']; 首先,确保对全局变量$gadd = $_POST['strs'];使用正确的格式$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. 您可以在Chrome中通过“检查元素”选项并转到“网络”标签来实现。 You can see what is sent through ajax and the return data. 您可以看到通过ajax发送的内容和返回数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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