使用javascript验证从php到html回显的下拉列表?
好吧,这个问题的标题可能有点令人困惑,我会尽力向您解释一下: 我有一个 php 文件,其中包含一个包含多个表单字段的数组,如下所示:
$var = array(0 => '<input type=....>', 1=> '<input type=....>');
我创建了一个 while 循环来循环遍历该数组以将它们显示在表格中,如下所示:
echo '<table>';
while($row = mysql_fetch_array($result)){
$num = $row['fieldId'];
$field = $num.' '.$row['field'];
echo '<tr><td>'.$field.'</td>';
echo '<td>'.$var[$q].'</td></tr>';
echo '<tr><td> </td></tr>';
$q++;
}; echo '</table>';
我将此 php 文件包含在 html 文件中,以便我可以得到一个有两列的表,第一列包含来自数据库的 $field,第二列包含 $var[$q],这是从 $var 数组中获取的某个输入字段。 不管你相信与否,这确实有效,现在的问题是,在 $var 数组中,我有一个带有多个选项的
更多信息: 包含 php 文件的 html 文件有这样的内容:
<html>....
<form name="mainForm">
<?php
include 'myPhpFile.php';
?>
<input type="button" name="btnNext" onclick="functionToValidate();">
</form>
...</html>
所以基本上,当我包含我的 php 文件时,它会创建一个表并向其中添加字段,并使用 mysql 数据库中的数据和一些使用上面显示的 $var 数组的输入字段填充它们 所有这些都有效,但问题是当我单击下一个按钮时,它会调用 javascript 函数来验证字段,一切正常,直到到达
这是我正在使用的 javascript 函数:
function myFunctionNameHere(){
if(!document.mainForm.p1_1[0].checked && !document.mainForm.p1_1[1].checked)
alert("1 - some warning here");
else if(!document.mainForm.p1_2[0].checked && !document.mainForm.p1_2[1].checked)
alert("2 - some warning here");
else if(!document.mainForm.p1_3[0].checked && !document.mainForm.p1_3[1].checked)
alert("3 - some warning here");
else if(!document.mainForm.p1_4[0].checked && !document.mainForm.p1_4[1].checked)
alert("4 - some warning here");
else if(!document.mainForm.p1_5[0].checked && !document.mainForm.p1_5[1].checked)
alert("5 - some warning here");
else if(!document.mainForm.p1_6[0].checked && !document.mainForm.p1_6[1].checked)
alert("6 - some warning here");
else if(document.mainForm.p1_7.value == "")
alert("7 - some warning here");
// this condition does not work:
else if(document.mainForm.p1_8.selectedIndex == 0)
alert("8 - some warning here);
// can't get the selectd field from the html select field
else
window.location = "OTHERPAGE.html";
}
这几乎就是该函数的作用,所有条件都有效,除了我发表评论的那一个。 (我仍在学习中,所以请不要嘲笑我的代码......拜托。)
抱歉,我实际上已经解决了这个问题,我的 php 数组中的 html 代码中有一些错误。现在它确实有效了。 谢谢。
ok so, the title of this question might be a little confusing, and i will try to explain it to you:
i have a php file with an array containing multiple form fields like this:
$var = array(0 => '<input type=....>', 1=> '<input type=....>');
and i created a while loop to loop through the array to show them in a table, something like this:
echo '<table>';
while($row = mysql_fetch_array($result)){
$num = $row['fieldId'];
$field = $num.' '.$row['field'];
echo '<tr><td>'.$field.'</td>';
echo '<td>'.$var[$q].'</td></tr>';
echo '<tr><td> </td></tr>';
$q++;
}; echo '</table>';
i included this php file in a html file so i can get a table with two columns, the first one with $field from the database and the second with $var[$q] which is some input field taken from the $var array.
Believe it or not that actually works, now the problem is that inside that $var array i have a <select>
field with multiple options, i want to validate that field with some javascript, but for some reason i can't get the values assigned in <option value="SOMEVALUE">
. i don't know why but i can't, one thing i found weird is that in that $var array i have some radio buttons which i CAN validate with javascript.
more info:
the html file that includes the php file has this:
<html>....
<form name="mainForm">
<?php
include 'myPhpFile.php';
?>
<input type="button" name="btnNext" onclick="functionToValidate();">
</form>
...</html>
so basically when i include my php file it creates a table and adds fields to it filling them with data from a mysql database and some input fields using the $var array shown above
all that works, but the problem is when i click the next button it calls a javascript function to validate the fields, everything works until it gets to the <select>
input,
i can validate radio buttons, text fields and textareas but i can't validate those <select>
this is the javascript function i am using:
function myFunctionNameHere(){
if(!document.mainForm.p1_1[0].checked && !document.mainForm.p1_1[1].checked)
alert("1 - some warning here");
else if(!document.mainForm.p1_2[0].checked && !document.mainForm.p1_2[1].checked)
alert("2 - some warning here");
else if(!document.mainForm.p1_3[0].checked && !document.mainForm.p1_3[1].checked)
alert("3 - some warning here");
else if(!document.mainForm.p1_4[0].checked && !document.mainForm.p1_4[1].checked)
alert("4 - some warning here");
else if(!document.mainForm.p1_5[0].checked && !document.mainForm.p1_5[1].checked)
alert("5 - some warning here");
else if(!document.mainForm.p1_6[0].checked && !document.mainForm.p1_6[1].checked)
alert("6 - some warning here");
else if(document.mainForm.p1_7.value == "")
alert("7 - some warning here");
// this condition does not work:
else if(document.mainForm.p1_8.selectedIndex == 0)
alert("8 - some warning here);
// can't get the selectd field from the html select field
else
window.location = "OTHERPAGE.html";
}
that's pretty much what the function does, all conditions work except for that one i put the comments on. (i'm still learning so don't laugh at my code... please.)
Sorry i actually got this solved, there were some errors in the html code from my php array. Now it actually works.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是在 javascript 中访问选择值的方式:
this is how you access select values in javascript: