AJAX 预填充下拉菜单
这有点复杂,所以请耐心等待:
在 FORM.php 上,我有 2 个下拉菜单:
Plant type
下拉菜单允许用户选择“Plant type”。
Plant sub-type
下拉菜单通过 AJAX 和对数据库的调用提供了“Plant sub-type”的多种选择。
AJAX 代码:
<script type="text/javascript">
function getXMLHTTP() {
var xmlhttp = false;
try {
xmlhttp = new XMLHttpRequest();
}
catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e1) {
xmlhttp = false;
}
}
}
return xmlhttp;
}
function getPlantSubtype(strURL) {
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function () {
if (req.readyState == 4) {
if (req.status == 200) {
document.getElementById('plant_subtype_div').innerHTML = req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
</script>
效果很好,当我提交时,值将保存在数据库中并显示在表单下方的表格中。
现在,用户想要编辑此条目,请单击包含信息的表格单元格旁边的“编辑”。
“植物类型”和“植物子类型”的值通过 $_POST
传递到 EDIT.php。
EDIT.php 代码(使用 Smarty 标签):
<select name="plant_type" onchange="getPlantSubtype('plant_subtype.php?plant_type='+this.value)">
<option selected="selected" value="{$plant_type}">
{$plant_type}
</option>
<option value="" disabled="disabled">
---
</option>
<option value="1">
Tomato
</option>
<option value="2">
Carrot
</option>
</select>
<div id="plant_subtype_div">
<select name="plant_subtype">
<option selected="selected" value="{$plant_subtype}">
{$plant_subtype}
</option>
<option value="" disabled="disabled">
---
</option>
</select>
</div>
在 EDIT.php 上,两个下拉菜单都显示正确的值。
但是当我点击Plant sub-type
下拉菜单时,多个选择不可用。
这是因为表单已预先填充了 $_POST
并且 onChange
尚未发生。
加载 EDIT.php 时,如何使 Plant sub-type
下拉列表显示其选择?
This is a bit complicated so bear with me:
On FORM.php, I have 2 drop down menus:
Plant type
drop down that allows the user to select 'Plant type'.
Plant sub-type
drop down that via AJAX and a call to the database presents multiple choices for 'Plant sub-type'.
AJAX code:
<script type="text/javascript">
function getXMLHTTP() {
var xmlhttp = false;
try {
xmlhttp = new XMLHttpRequest();
}
catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e1) {
xmlhttp = false;
}
}
}
return xmlhttp;
}
function getPlantSubtype(strURL) {
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function () {
if (req.readyState == 4) {
if (req.status == 200) {
document.getElementById('plant_subtype_div').innerHTML = req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
</script>
That works perfectly and when I submit, values are saved in the DB and shown on a table below the form.
Now the user wants to edit this entry, clicking on 'Edit' adjacent to the table cell with the info.
Values for 'Plant type' and 'Plant sub-type' are passed via $_POST
to EDIT.php.
EDIT.php code (using Smarty tags):
<select name="plant_type" onchange="getPlantSubtype('plant_subtype.php?plant_type='+this.value)">
<option selected="selected" value="{$plant_type}">
{$plant_type}
</option>
<option value="" disabled="disabled">
---
</option>
<option value="1">
Tomato
</option>
<option value="2">
Carrot
</option>
</select>
<div id="plant_subtype_div">
<select name="plant_subtype">
<option selected="selected" value="{$plant_subtype}">
{$plant_subtype}
</option>
<option value="" disabled="disabled">
---
</option>
</select>
</div>
On EDIT.php both drop down menus show the correct values.
But when I click on Plant sub-type
drop down the multiple choices are not available.
That's because the form was already pre-populated with $_POST
and onChange
hasn't happened yet.
How do I make Plant sub-type
drop down show its choices when EDIT.php is loaded?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种选择是在页面加载时调用
getPlantSubtype('plant_subtype.php?plant_type={$plant_type}')
。例如(在页面底部):
更好的选择是从
plant_subtype.php
从(缓存、数据库)获取子类型并填充选项值的同一位置获取子类型。One option is just to call
getPlantSubtype('plant_subtype.php?plant_type={$plant_type}')
on the page load.For example (on the bottom of the page):
A Better option would be to fetch the subtypes from the same place
plant_subtype.php
fetches them from (cache, db) and populate the option values.