值未从 ajax 类生成的元素中获取
这是 HTML 表单
echo "<tr><td>Course</td><td><select name='Course' onchange='branch_selector(this.value)'>";
echo "<option value='B.Tech'>B.Tech</option>";
echo "<option value='M.Tech'>M.Tech</option>";
echo "<option value='MBA'>MBA</option>";
echo "<option value='MCA'>MCA</option>";
echo "</select></td></tr>";
echo "<tr><td><div id='branch_div_name'></div></td><td><div id='branch_div'></div></td></tr>";
这是 javascript 代码
function branch_selector(val) {
if(val=='B.Tech' || val=='M.Tech') {
show_name('branch_div_name','Branch');
var xmlhttp;
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('branch_div').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","branch_selector.php",true);
xmlhttp.send();
}
else {
document.getElementById("branch_div").innerHTML="";
disappear_name('branch_div_name');
}
}
这是在后台运行的 PHP 代码
$result = mysql_query("SELECT DISTINCT Branch FROM student_main ORDER BY Branch ASC") or die(mysql_error());
echo "<select name='Branch' id='Branch' onchange='harb()'>";
while($row = mysql_fetch_array($result)) {
if($row[0]=='' or $row[0]=='N/A') {
continue;
}
else {
echo "<option value='".$row[0]."'>".$row[0]."</option>";
}
}
echo "</select>";
生成下拉列表没有任何问题。但是提交表单时,$_POST['Branch'] 未设置。 请帮忙
This is the HTML form
echo "<tr><td>Course</td><td><select name='Course' onchange='branch_selector(this.value)'>";
echo "<option value='B.Tech'>B.Tech</option>";
echo "<option value='M.Tech'>M.Tech</option>";
echo "<option value='MBA'>MBA</option>";
echo "<option value='MCA'>MCA</option>";
echo "</select></td></tr>";
echo "<tr><td><div id='branch_div_name'></div></td><td><div id='branch_div'></div></td></tr>";
this is the javascript code
function branch_selector(val) {
if(val=='B.Tech' || val=='M.Tech') {
show_name('branch_div_name','Branch');
var xmlhttp;
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('branch_div').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","branch_selector.php",true);
xmlhttp.send();
}
else {
document.getElementById("branch_div").innerHTML="";
disappear_name('branch_div_name');
}
}
This is the PHP code running at the backened
$result = mysql_query("SELECT DISTINCT Branch FROM student_main ORDER BY Branch ASC") or die(mysql_error());
echo "<select name='Branch' id='Branch' onchange='harb()'>";
while($row = mysql_fetch_array($result)) {
if($row[0]=='' or $row[0]=='N/A') {
continue;
}
else {
echo "<option value='".$row[0]."'>".$row[0]."</option>";
}
}
echo "</select>";
The dropdown is generated without any problem. But when the form is submitted the $_POST['Branch'] is not set.
Please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我已经解决了这个问题,该解决方案不是最好的,但它可以完成工作。
我现在使用的是一个隐藏字段,该字段的值是根据 Branch of 的值设置的。当用户单击“提交”时,使用 Javascript 设置该值。
它工作正常,没有任何问题,但如果有人有更好的解决方案,请分享。
Ok, I have solved this issue, The solution is not the best but its getting the job done.
I am using now a hidden field and the value is this field is set according to the value of Branch of. The value is set using Javascript when user click on submit.
Its working fine, no issue whatsoever but if someone have better solution, please share.