0

我有一个包含动态内容的表格。该表包含一个下拉列表,用户需要在其中选择一个选项,然后显示的数据将使用 ajax 返回。目前它可以工作,但问题是它只适用于第一行。l 从第二行的下拉列表中选择后,结果显示在第一行:"/!!

表代码

<table class="bordered" >
<tr class="bordered">

<?php
$qry=mysql_query("SELECT * 
FROM  `order` ");
    //Check whether the query was successful or not
    $qry1="SELECT * 
FROM  `order` ";
    $result=mysql_query($qry1);
    if(mysql_num_rows($qry)>0){
?>
<th>delete</th><th>edit</th><th>staff info</th><th>staff</th><th>ex</th><th>phone</th><th>name</th><th>id</th><th>quantity</th><th>status</th><th>time</th><th>date</th><th>order number</th><th></th>
</tr>
    <?php
            while($info = mysql_fetch_array( $result )) 
            { 
            $idem = $info['emp_id'];
            $q="select * from empoyee where emp_id= '".$idem."';";
            $r=mysql_query($q);
            ?>
            <tr>
            <td>
            <form method="POST" action="delete_asu.php">
            <input type="image" src="delete1.png" width="25%" height="20%"/>
            <input type="hidden" name="delete_id" value="<?php echo $info['order_num']; ?>" />
            </form></td>
            <td>
            <?php $id=$info['order_num']; 
            echo '<form method="POST" action="update_asu_form.php?id='.$id.'">' ?>
            <input type="image" src="settings-24-128.png" width="20%" height="15%"/>
            </form></td>
            <td id="txtHint"></td>
            <td>

                <select name="users" onchange="showUser(this.value)">
                <option></option>
                <?php

                $sql=mysql_query("SELECT asu_name from asu_staff");
                while($row = mysql_fetch_array($sql)){
                echo "<option>" .$row["asu_name"]. "</option>";
                }

                ?>
                </select>

            </td>
            <?php 
            while($row = mysql_fetch_array( $r )) 
            {
            ?>
            <td><?php echo $row['ex']; ?></td><td><?php echo $row['phone']; ?></td><td><?php echo $row['emp_name']; ?></td><td><?php echo $row['emp_id']; ?></td>
            <?php
            }
            ?>
            <td><?php echo $info['quantity']; ?></td><td><?php echo $info['status']; ?></td>
            <td><?php echo $info['time']; ?></td><td><?php echo $info['date']; ?></td>
            <td><?php echo $info['order_num']; ?></td><td><input type="radio" name="choose" value="<?php echo $info['order_num']; ?>" /></td>
            </tr>
            <?php
            } 
        }
        else echo "<h1>no orders</h1>";
        ?>

</table>

ajax 代码

<script>
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;

    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>

php代码

 $q=$_GET["q"];
            $sql="SELECT * FROM asu_staff WHERE asu_name = '".$q."'";
            $result = mysql_query($sql);
            while($row = mysql_fetch_array($result))
            {
            echo "<table>";
            echo "<tr> <td>" . $row['asu_id'] . " </td><td>staff id</td></tr>";
            echo "<tr> <td>" . $row['phone'] . " </td><td>staff phone</td></tr>";
            echo "</table>";

Yaaah 这是工作

想法:

Alhamdullilallah 它现在工作得很好:D 问题出在我检索内容的 id 上,我创建了一个增量 id 并将其发送到 ajax,然后就可以了

4

1 回答 1

0

select 给出的值是每个选项的 value="" 属性中的值。在您的情况下,您需要将显示选项的代码更改为:

<select name="users" onchange="showUser(this.value)">
 <option value="" selected></option>
<?php
   $sql=mysql_query("SELECT asu_name from asu_staff");
   while($row = mysql_fetch_array($sql)){
     echo "<option value=\"".$row["asu_name"]."\">" .$row["asu_name"]. "</option>";
   }

?>
</select>

尝试这个。

ps 您可以在查询中选择一个带有名称的 id,然后在 value="" 属性中使用该 id。然后,您在 php 代码中的查询可以使用 id 代替名称。

于 2013-07-04T20:03:51.000 回答