-1

我正在尝试使用下拉菜单“实时”更新表格。我只会在内部使用这个网页。

我真的不知道很多javascript,所以我需要一些帮助。

我写的脚本只获取第一行的值,所以并没有真正起作用......

这是两个页面的代码: HTML

<html>
<head>     
<script>
function UpdateUser(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;
    }

  }
    var lastname = document.getElementById('lastname').innerHTML;    
    var firstname = document.getElementById('firstname').innerHTML;                  
    var state = document.getElementById('state').innerHTML;             

    xmlhttp.open("GET","updateuser3.php?q="+str+"&lastname="+lastname+"&firstname="+firstname+"&state="+state,true);
    xmlhttp.send();
}


</script>
</head>
<body>
<?php

$con = mysql_connect('localhost','root','');
if (!$con)
  {
  die('Could not connect: ' . mysql_error($con));
  }


mysql_select_db("test");
$sql="SELECT lastname, firstname, state FROM author";

$result = mysql_query($sql);

echo $sql;
echo "<br />";


echo "<table border='1'>
<tr>
<th>lastname</th>
<th>firstname</th>
<th>State</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td name='lastname' id='lastname'>" . $row['lastname'] . "</td>";
  echo "<td name='firstname' id='firstname'>" . $row['firstname'] . "</td>";

  echo "<td><select onchange='UpdateUser(this.value)'><option name='state' id='state' value='".$row['state']."'>".$row['state']."</option>"; 

                        if  ($row['state']==='CA')
                            {
                            echo '<option value="OR">OR</option>';
                            echo '<option value="UT">UT</option>';
                            echo '<option value="MI">MI</option>';
                            echo '</select></td>';                                          
                            } 
                            else if ($row['state']==='OR')
                            {                    
                            echo '<option value="CA">CA</option>';
                            echo '<option value="UT">UT</option>';
                            echo '<option value="MI">MI</option>';
                            echo '</select></td>';  
                            }
                            else
                            {
                            echo '<option value="CA">CA</option>';
                            echo '<option value="UT">UT</option>';
                            echo '<option value="MI">MI</option>';
                            echo '<option value="OR">OR</option>';
                            echo '</select></td>';                                  
                            }                  

  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>
<br>
<div id="txtHint"><b>State info will be listed here.</b></div>

</body>
</html>

php

<?php
$q=$_GET["q"];
$w=$_GET["lastname"];
$e=$_GET["firstname"];
$r=$_GET["state"];

echo "<br /> New State: ";
echo $q;
echo "<br /> Last Name: ";
echo $w;
echo "<br /> First Name: ";
echo $e;
echo "<br /> Old State: ";
echo $r;
echo "<br />";

$con = mysql_connect('localhost','root','');
if (!$con)
  {
  die('Could not connect: ' . mysql_error($con));
  }


mysql_select_db("test");


$sql = "UPDATE author SET state = '$q' WHERE lastname = '$w' AND firstname = '$e' AND state = '$r'";

echo $sql;
echo "<br />";

mysql_close($con);
?>

有人可以帮忙吗?泰

4

1 回答 1

0

我将从查询开始进行故障排除,以查看您的查询是否返回多个结果。

SELECT lastname, firstname, state FROM author在 MySQL 中运行查询。

如果此查询返回多个结果,那么您就知道问题出在您的代码上。

我使用 phpMySQL 来调试我的 SQL。

于 2013-08-21T00:24:02.683 回答