我已经挖了很多帖子,但在这里找不到像这样的场景。。。
我有一个PHP表单,它包含一个下拉选择,其中包含来自MySQL数据库表的选项值。这部分工作得很好。这是我需要帮助的地方。。。
有一个textarea字段,我需要用同一个数据库表中的数据填充它,但需要使用不同的字段。此表包含“name”和相应的“text”集。下拉列表显示“名称”作为选项。所以我需要的是当一个选择,一个脚本运行(像onChange)。。。获取所选值,查询数据库以获取与所选“名称”关联的“文本”。然后,该“文本”将显示在文本区域窗体字段内。
此代码的一部分供您查看:

echo "<form action=$PHP_SELF method=\"post\">\n";
echo "<select id=\"conditions\" name=\"conditions\">\n";
echo "<option value=\"Select\" selected>Select a Message</option>\n";

$result = mysqli_query($link, "SELECT * FROM db_scripts");
while ($data = mysqli_fetch_object($result)) {
    echo "<option value=".$data->script_id.">".$data->script_name."</option>\n";
}

echo "</select>\n";

echo "<br><textarea name=\"message\" style=\"width:300px; height:130px\" data-maxsize=\"160\" data-output=\"status1\" wrap=\"virtual\" maxlength=\"160\"></textarea><br />\n";

所以,就像我说的,这部分工作得很好。我有一个下拉菜单,可以选择“脚本名”。现在我只需要将相应的消息放入textarea字段“消息”。非常感谢您的帮助!

最佳答案

以以下格式在PHP中创建关联javascript对象文本数组:

<?php
echo "
<!-- this script goes in your <head> -->\n
<script>\n
var db_array = [";
$result = mysqli_query($link, "SELECT * FROM db_scripts");
$first = TRUE; // remove trailing comma from array
while ($data = mysqli_fetch_object($result)) {
  if ($first === TRUE) $first = FALSE;
  else echo ',';
  echo "
  {
    id: '$data->script_id',
    name: '$data->script_name'
    message: '$data->script_message'
  }";
}
echo "];\n
</script>";
?>

关键是,如果消息与id不在同一行,则需要在查询中使用JOIN。这一部分背后的想法是,您只查询一次数据库。您不希望客户机每次切换下拉菜单中的选项时都查询数据库。这里的例外是如果你有超过兆字节的消息。在这种情况下,请考虑实现缓存措施。
继续。。。一旦构建了数组,就可以通过迭代数组来构建<option>s,如下所示:
<?php
echo "
<form action=\"$PHP_SELF\" method=\"post\">\n
<select id=\"conditions\" name=\"conditions\">\n
<option value=\"Select\" selected>Select a Message</option>\n
<script>\n
for (var i = 0; i < db_array.length; i++) {\n
  document.write('<option value=\"'+db_array[i].id+'\">'+db_array[i].name+'</option>');\n
}\n
</script>\n
</select>\n
<!-- notice the id attribute I added -->\n
<br><textarea id=\"message\" name=\"message\" style=\"width:300px; height:130px\" data-maxsize=\"160\" data-output=\"status1\" wrap=\"virtual\" maxlength=\"160\"></textarea><br />\n";
?>

…然后有一个脚本可以访问这些动态创建的DOM元素(例如,在<body>标记的末尾)
<script>
// returns the db_array index of the object id supplied
function returnIndex (id) {
  for (var i = 0; i < db_array.length; i++) {
    if (db_array[i].id === id) return i;
  }
  return -1; // returns index of -1 if id is not found
}
// changes the <textarea> message. notice the id I added to the messages <textarea> above
function changeMessage (id) {
  // remember to handle the case of an id not being found within db_array (where returnIndex returns -1)
  document.getElementById('message').innerHTML = db_array[returnIndex(id)].message;
}
// on the event that we switch options, do this event. realize that you'll have to run the changeMessage function once the page loads if you want the first option's message to show
document.getElementById('conditions').onchange = function() {
  changeMessage(this.selectedIndex);
}
</script>

关于javascript - 根据下拉字段选择从数据库填充表单文本字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19822704/

10-12 12:47
查看更多