根据下拉字段选择从数据库填充表单文本字段 [英] Populate form text field from database based on dropdown field selection

查看:55
本文介绍了根据下拉字段选择从数据库填充表单文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了很多帖子,但是在这里找不到这样的情况...

I've dug through many posts but can't find a scenario quite like this on here...

我有一个PHP表单,其中包含一个下拉选择,其中包含一个MySQL数据库表中的选项值.这部分工作正常.这是我需要帮助的地方...

I have a PHP form that contains a dropdown selection containing option values from a MySQL database table. This part works fine. Here's where I need help...

有一个textarea字段,我需要用相同数据库表中的数据填充,但字段不同.该表包含一个名称"和一组对应的文本".下拉菜单显示名称"作为选项.因此,我需要做的是选择之后,运行脚本(如onChange)...获取选定的值,查询数据库以获取与选定的名称"关联的文本".然后,该文本"将显示在textarea表单字段内.

There is a textarea field that I need to populate with data from the same database table, but a different field. This table contains a 'name' and a corresponding set of 'text'. The dropdown shows the 'name' as the options. So what I need is when a selection is made, a script runs (like onChange)... takes the selected value, queries the database to get the 'text' that is associated with the selected 'name'. That 'text' is then displayed inside of the textarea form field.

此代码的一部分供您查看:

A section of this code for you to view is:

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";

所以,就像我说的那样,这部分工作正常.我有一个以'script_name'为选项的下拉菜单.现在,我只需要将相应的消息输入到textarea字段"message"中即可.非常感谢您的协助!

So, like I said, this part works just fine. I have the dropdown with the 'script_name' as the options. Now I just need to get the corresponding message into the textarea field "message". Any assistance is most appreciated!

推荐答案

好,我已经解决了这个问题...但是最终没有使用任何其他答案.我尝试了它们,尤其是Jared的那个.它使我离得很近,但最终并没有完全起作用.我确实从贾里德那里得到了一些非常有益的指导和想法!最后,我找到了一种更简单的方法来完成此任务...

Ok, I have solved this issue... but ended up NOT using any of the other answers. I tried them, particularly the one from Jared. It got me very close, but in the end, did not fully work. I did get some very helpful direction and ideas from Jared though! In the end though, I found a simpler way to get this done...

1)从Google提取< head> 区域中的JQuery.没有其他脚本加载在那里.

1) Pulled JQuery in the <head> area from Google. No other scripts were loaded there.

2)将此代码放在表单上方的PHP文件中,但在<?php> 标记之外.

2) Put this code in the PHP file above my form, but outside of <?php> tags.

<script>
$(function () {
    $("#conditions").change(function() {
    $("#message").val($("#conditions").val());
   })
   $("#conditions").trigger("change");
})
</script>

3)将下拉选择字段和textarea目标字段设置为...

3) Setup the dropdown selection field and textarea target field as...

$result = mysqli_query($link, "SELECT * FROM db_scripts");
$first = TRUE; // remove trailing comma from array
while ($data = mysqli_fetch_object($result)) {
$html_opts .= "<option value='".preg_replace('/[^a-zA-Z0-9\s]/', '', strip_tags($data->script_text))."'>".$data->script_name."</option>";   
}
echo '<select id="conditions" name="conditions">';
echo '<option value="" selected>Select a Script</option>';
echo $html_opts.'</select>';
echo '<br><textarea id="message" name="message" style="width:300px; height:130px" data-maxsize="160" data-output="status1" wrap="virtual" maxlength="16\"></textarea><br />';

当然,整个表格未在此处发布,但重要的部分在此发布.现在这对我来说是完美的.希望它也可以帮助其他人!

Of course the entire form is not posted here, but the parts that matter are. This is working perfectly for me now. Hopefully it can help someone else too!

这篇关于根据下拉字段选择从数据库填充表单文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆