【问题标题】:Get table field based on result of dropdown menu (poplated from another table field)根据下拉菜单的结果获取表字段(从另一个表字段弹出)
【发布时间】:2014-01-20 12:31:45
【问题描述】:

我希望我的标题有意义。
基本上,我有一个包含几个字段的 MySQL 表。重要的是,一个名为“物种”的字段被填充到一个下拉框中。这个下拉框位于 HTML 表单中,数据来自 PHP。我想要做的是让同一个表中的另一个字段填充物种下面的 HTML 表单字段,这个另一个字段称为“sku”。我是 AJAX 的新手,我想我了解我的脚本应该如何工作,但它没有返回任何结果。这是我的主要 PHP 脚本:

<html>
 <head>
  <link rel="stylesheet" type="text/css" href="css/styles.css" media="screen" />
  <title>Add Inventory</title>
  <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","getsku.php?q="+str,true);
xmlhttp.send();
}
</script>
 </head>
 <body>
<?php
session_start();

require_once('includes/config.inc.php');
require_once('includes/functions.inc.php');

$thisPage='add';
include('includes/navbar.inc.php');
?>

<h1>Add New Inventory Record</h1>
<form method="POST" action="submitadd.php" />

<table id="add">

<tr>
<td class="headings"><b>Species:</b></td>
<td><select name="species:" onchange="showUser(this.value)">
      <option value="select">Choose a Species</option>
      <?php
      $prodquery="SELECT name FROM products ORDER BY name ASC";
      $result=mysqli_query($con,$prodquery) or die(mysqli_error($con));
      while ($row = mysqli_fetch_array($result)) {
        echo "<option value='" . $row['name'] . "'>" . $row['name'] . "</option>";
      }
      ?>
    </select>
</td>
</tr>

<tr>
<td class="headings"><b>SKU:</b></td>
<td><input type="text" name="sku" value="<div id="txtHint" />" readonly="readonly" size="35" /></td>
</tr>
</table>
<br />
<input type="submit" name="submit" value="submit" class="button1" />
</form>

</body>
</html>

这里是getsku.php:

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

require_once('includes/config.inc.php');

$sql="SELECT sku FROM products WHERE name = '".$q."'";

$result = mysqli_query($con,$sql);

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['sku'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?>

当我在下拉列表中选择一个项目时,我可以看到正在发生的事情,但没有出现预期的结果(该产品的“sku”)。

谁能帮帮我?

【问题讨论】:

  • 您使用的是 Firefox 吗?安装萤火虫。它使这类问题更容易识别。
  • 在 Firefox 中使用 Firebug,我可以看到 AJAX 脚本正在运行,但它错误地调用了“名称”。例如,物种名称是 (Aulonocara rubescens "Ruby Red") 但被调用的 $_GET 是: q= 的变量通过使用 $_GET,我假设它得到: Aulonocara%20rubescens%20%22Ruby%20Red%22 而不是:Aulonocara rubescens "Ruby Red" 所以一切正常,但是由于名称不正确,SQL 查询只是没有返回任何结果。
  • 等等,我错了。虽然 URL 错误,但 Firebug 向我显示: 参数: q Aulonocara rubescens "Ruby Red" 所以它正确地看到了变量。那么getsku.php一定有问题?

标签: php mysql ajax forms


【解决方案1】:

这是我最终用于解决我的问题的getsku.php:

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

require_once('includes/config.inc.php');

$sql="SELECT sku FROM products WHERE name = '".$q."'";

$result = mysqli_query($con,$sql);

while($row = mysqli_fetch_array($result))
  {
  echo "<td><input type='hidden' name='sku' value='" . $row['sku'] . "' readonly='readonly' size='35' /></td>";
  }

mysqli_close($con);
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 1970-01-01
    • 2016-09-20
    • 2015-07-19
    • 1970-01-01
    相关资源
    最近更新 更多