php - 如果输入更改,用户填充框将不会更改

标签 php html mysql ajax

下拉列表是从数据库“meal_planner”中填充的使用第一个选择。我只是把它拼凑在一起。

<?php
$link=mysqli_connect("localhost", "root", "");
mysqli_select_db($link,"meal_planner");
?>
<!DOCTYPE html>
<html>
<head>
<script>
function showUser(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else { 
        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 (this.readyState == 4 && this.status == 200) {
                document.getElementById("txtHint").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET","getuser.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>
</head>
<body>

<form name="form1" action="" method="post">
<select name="users" onchange="showUser(this.value)">
  <option value="">Select a person:</option>
  <?php
  $res=mysqli_query($link,"select name from protein");
  while($row=mysqli_fetch_array($res))
  {
  ?>
  <option><?php echo $row["name"]; ?></option>

  <?php
  }

  ?>

  </select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>

</body>
</html>

获取用户.php

<!DOCTYPE html>
<html>
<head>
<style>
table {
    width: 100%;
    border-collapse: collapse;
}

table, td, th {
    border: 1px solid black;
    padding: 5px;
}

th {text-align: left;}
</style>
</head>
<body>

<?php
$q = intval($_GET['q']);

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

mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM protein WHERE protein_id = '".$q."'";
$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['calories'] . "</td>";
    echo "<td>" . $row['protein'] . "</td>";
    echo "<td>" . $row['carbs'] . "</td>";
    echo "<td>" . $row['fats'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>

最佳答案

您没有输入 protein_id进入<option>元素。

$res=mysqli_query($link,"select name, protein_id from protein");
while($row=mysqli_fetch_array($res))
{
?>
<option value="<?php echo $row["protein_id"]; ?>"><?php echo $row["name"]; ?></option>
<?php
}

当没有 value 时属性,它使用元素的文本,因此您将蛋白质名称作为选择值结束。然后当你使用 intval($q) , 它返回 0因为蛋白质名称不是有效整数,并且 AJAX 调用中的查询总是使用 protein_id = 0 获取蛋白质.

关于php - 如果输入更改,用户填充框将不会更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39627756/

相关文章:

php - 在 URL 中传输 session ID( session trans_sid 等效 Laravel)

html - 使用 :before CSS3 pseudo-selector 使背景图像变暗

mysql - rails : Creating a survey data structure with variable type answers

mysql - 如何在 phpMyAdmin 中将部分 mySQL 表导出为 XML

php - 如何通过PHP将图像上传到Azure存储?

php - php 中的 mysql_query SELECT 语句在一段时间后停止返回结果?

php - MySQL 中的 SUM 函数问题

javascript - 从 HTML 表格创建 Highcharts 时忽略 tfoot 表格行

html - 菜单格式 HTML

php - 创建论坛灯泡(未读)系统的最有效方法是什么?