我有一个重复的表格。我已经使用 java 脚本向表单添加了验证,但我必须在其他表单验证之前在第一个表单中输入信息。我希望每个表单上的每个验证过程都是分开的。我尝试了许多不同的方法,但都失败了。谢谢
PHP/html 代码如下:
$prodid = $row['id'];
echo'<tr class="notetr3">';
echo '<form action="insertlink.php" method="post" name="' . $prodid. '" onSubmit="return newchoose1(' . $prodid. ')" >';
//first column (Company name)
echo '<td><select name="companyid" id="companyid"><option value="">Select Company</option>';
$result = mysql_query("
SELECT * FROM company
WHERE id NOT IN
(SELECT DISTINCT companyid FROM joincomppro WHERE productid = '$prodid')
"); //select productid that matches productid table
while ($row = mysql_fetch_array($result)){
$compid = $row['id'];
$num_rows = mysql_num_rows($result);
$sqlcompany2 = mysql_query("SELECT * from company WHERE id = '$compid'");
while ($row = mysql_fetch_array($sqlcompany2)){
$companyname = $row['companyname'];
if ($num_rows) {
echo '<option value="' . $compid . '">' . $companyname . '</option>';
}
}
}
echo '</select></td>';
//Second column (Batch size)
echo '<td><input type="text" value="Batch Size" name="batchsize" id="batchsize" size="17" maxlength="40"/></td>';
//third column (Batch price)
echo '<td><input type="text" value="Batch Price" name="batchprice" id="batchprice" size="17" maxlength="40"/></td>';
//fourth column
echo '<td><input type="text" value="Unit Price" name="unitprice" id="unitprice" size="17" maxlength="40"/></td>';
//hidden value
$groupselect3 = mysql_query("SELECT * FROM product WHERE categorysubtype = '$categorysubtype' AND productname = '$productname' AND colour = '$colour'");
while($row = mysql_fetch_array($groupselect3))
{
echo '<input type="hidden" name="productid" id="productid" value="' . $row['id'] . '"/>';
echo '<input type="hidden" name="groups2" id="groups2" value="' . $group . '"/>';
}
//submit
echo '<td><input type="submit" name="' . $prodid . '" value="Submit" /></td></tr>';
echo '</form>';
然后我有 JavaScript 函数,如图所示:
function newchoose1(id) {
var emptyvalue = "";
if (document.getElementById('companyid' + id).value == "") {
emptyvalue += "Select a Company \n";
}
if (document.getElementById('batchsize' + id).value == "") {
emptyvalue += "Enter a batch size \n";
}
if (isNaN(document.getElementById('batchsize' + id).value)) {
emptyvalue += "Batch Size has to be a whole number \n";
}
if((document.getElementById('batchprice' + id).value == "Batch Price" ) && (document.getElementById('unitprice' + id).value == "Unit Price")){
emptyvalue += "Enter a number in Batch Price or Unit Price \n";
}
else if ((document.getElementById('batchprice' + id).value == "Batch Price") && (isNaN(document.getElementById('unitprice' + id).value))){
emptyvalue += "Unit price has to be a valid number \n";
}
else if((document.getElementById('unitprice' + id).value == "Unit Price") && (isNaN(document.getElementById('batchprice' + id).value))){
emptyvalue += "Batch price has to be a valid number \n";
}
else {}
if (emptyvalue != "") {
alert (emptyvalue);
return false;
}
}