gpt4 book ai didi

PHP 重复表单验证。如何使用javascript进行验证?

转载 作者:行者123 更新时间:2023-11-28 02:10:52 24 4
gpt4 key购买 nike

我有一个重复表格。我已经使用 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;
}

}

最佳答案

一种方法是将 $prodid 添加到每个表单元素控件(例如“companyid”),以确保每个表单控件都是唯一的,因此可以被 JavaScript 找到。然后只需将 $prodid 包含在对 newchoose1() 的调用中即可。

类似这样的事情:

echo '<form action="insertlink.php" method="post" name="' . $prodid. '" onSubmit="return newchoose1(' . $prodid . ')" >';

// ...

echo '<td><select name="companyid" id="companyid' . $prodid . '"><option value="">Select Company</option>';

...和:

function newchoose1(id) {
// ...
if (document.getElementById('companyid' + id).value == "") {
// etc....

另一种方法是类似地将 $prodid 传递给 newchoose1() 但不更改表单控件,而是将表单内的 ID 更改为类,然后获取特定表单内具有给定类的元素(我建议切换到 jQuery 以使您的生活在这方面更简单)。这还有一个好处是允许您在不同的表单中呈现具有相同样式的相同表单控件。

关于PHP 重复表单验证。如何使用javascript进行验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17118034/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com