简体   繁体   English

PHP重复表格验证。 如何使用JavaScript进行验证?

[英]PHP repeating forms validation. How to validate using javascript?

I have a repeating form. 我有一个重复的表格。 I have added validation to the forms with java script but I have to enter information in the first form before the other forms validate. 我已经使用Java脚本在表单中添加了验证,但是在其他表单验证之前,我必须在第一个表单中输入信息。 I would like each validation process separate on each form. 我希望每个验证过程在每个表单上都分开。 I have tried many different ways and all have failed. 我尝试了许多不同的方法,但都失败了。 Thanks 谢谢

PHP/html code below: 下面的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>';

I then have JavaScript function as shown: 然后,我具有如下所示的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;        
        }

}

One approach would be to add $prodid to each form element control (eg, "companyid") to ensure that each form control is unique and can thus be found by JavaScript. 一种方法是将$prodid添加到每个表单元素控件(例如“ companyid”),以确保每个表单控件都是唯一的,并因此可以被JavaScript找到。 Then just include that $prodid inside your call to newchoose1() . 然后只需在对newchoose1()调用中包含$prodid即可。

Something like this: 像这样:

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>';

...and: ...和:

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

Another approach would be to similarly pass $prodid to newchoose1() but instead of changing form controls, you would change the IDs inside the form to classes and then grab the element with the given class inside the specific form (I would recommend switching to jQuery to make your life simpler in this regard). 另一种方法是类似地将$prodid传递给newchoose1()但无需更改表单控件,而是将表单内的ID更改为类,然后在特定表单内获取具有给定类的元素(我建议切换到jQuery以简化您的生活)。 This would also have the benefit of allowing you to present the same form controls with the same styles across different forms. 这样做的好处还在于允许您在不同的表单之间呈现具有相同样式的相同表单控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM