表单验证不在多个记录中工作

时间:2014-01-28 03:21:44

标签: php jquery

我在这里是从数据库编辑记录。为安全起见,我进行了表单验证。我的表单验证脚本仅适用于1条记录。但是当我尝试编辑2个或更多记录时,脚本不适用于其他记录。只在第一个记录功能我的脚本。我需要验证所有记录。

请帮忙吗?

表格

<form action="" method="post" id="register_form"  name="register_form" onsubmit="javascript:parent.location.reload(true);">

<p class="validate_msg">Please fix the errors below!</p>
<?php
$mysqli = new mysqli("localhost", "root", "", "app");
$id=$_POST['checkbox'];
$drop=$_POST['drop'];
$tier_two=$_POST['tier_two'];

$drop=mysqli_real_escape_string($mysqli,$drop);
$tier_two=mysqli_real_escape_string($mysqli,$tier_two);

$N = count($id);
for($i=0; $i < $N; $i++)
{
    $result1 = $mysqli->query("
SELECT a.item_name, a.item_description, a.counter, b.counter, b.pr, b.total_quantity
FROM app a
LEFT OUTER JOIN purchase_request b
ON a.counter=b.counter
WHERE a.counter='$id[$i]'
    ");
    while ($row = $result1->fetch_assoc())
      { ?>
 <p> <label for="">ID</label> <input name="n_counter[]" type="text" id="n_counter" value="<?php echo $row['counter'] ?>" readonly="readonly"/><span class="val_counter"></span> </p>

 <p> <label for="">Item</label> <input name="n_itemname" type="text" value="<?php echo $row['item_name'] ?>" readonly="readonly"/><span class="val_item"></span> </p>

 <p> <label for="">Description</label> <input name="n_itemdescription" type="text" value="<?php echo $row['item_description'] ?>" readonly="readonly"/><span class="val_desc"></span> </p>

 <p> <label for="">PR Quantity</label> <input name="n_quantity[]" id="n_quantity"  class="tb1" type="text" value="<?php echo $row['total_quantity']; ?>" />
                <span class="val_qty"></span> </p>

 <p> <label for="">PR #</label> <input name="n_app_cn[]" id="n_app_cn" type="text" value="<?php echo $row['pr'] ?>" /><span class="val_project"></span> </p>    
    <br>
      <?php
      }
}

?>
<input name="submit" type="submit" id="sbtBtn" value="Update">
</form>

这是我的脚本

<script>

    jQuery(function($) {
        var validation_holder;

        $("form#register_form input[name='submit']").click(function() {

        var validation_holder = 0;

            var project         = $("form#register_form input[id='n_app_cn']").val();
            var project_regex   = /^[a-zA-Z0-9]+$/; // reg ex cost check    

            var item            = $("form#register_form input[name='n_itemname']").val();
            var item_regex      = /^[a-zA-Z0-9]+$/; // reg ex cost check

            var desc            = $("form#register_form input[name='n_itemdescription']").val();
            var desc_regex      = /^[a-zA-Z0-9]+$/; // reg ex cost check

            var qty             = $("form#register_form input[id='n_quantity']").val();
            var qty_regex       = /^[0-9]+$/; // reg ex qty check

            var counter         = $("form#register_form input[id='n_counter']").val();
            var counter_regex   = /^[0-9]+$/; // reg ex qty check
            //var email_regex   = /^[\w%_\-.\d]+@[\w.\-]+.[A-Za-z]{2,6}$/; // reg ex email check    
            //var password      = $("form#register_form input[name='password']").val();
            //var repassword        = $("form#register_form input[name='repassword']").val();
            //var phone             = $("form#register_form input[name='phone']").val();
            //var phone_regex       = /^[0-9]{4,20}$/; // reg ex phone check    

            /* validation start */  

            if(project == "") {
                $("span.val_project").html("This field is Required.").addClass('validate');
                validation_holder = 1;
            } else {
                if(!project_regex.test(project)){ // if invalid phone
                    $("span.val_project").html("Invalid Special Characters!").addClass('validate');
                    validation_holder = 1;

                } else {
                    $("span.val_project").html("");
                }
            }

            if(item == "") {
                $("span.val_item").html("This field is Required.").addClass('validate');
                validation_holder = 1;
            } else {
                if(!item_regex.test(item)){ // if invalid phone
                    $("span.val_item").html("Invalid Special Characters!").addClass('validate');
                    validation_holder = 1;

                } else {
                    $("span.val_item").html("");
                }
            }

            if(desc == "") {
                $("span.val_desc").html("");
            } else {
                if(!desc_regex.test(desc)){ // if invalid phone
                    $("span.val_desc").html("Invalid Special Characters! Sucks").addClass('validate');
                    validation_holder = 1;

                } else {
                    $("span.val_desc").html("");
                }
            }

            if(qty == "") {
                $("span.val_qty").html("This field is required.").addClass('validate');
                validation_holder = 1;
            } else {
                if(!qty_regex.test(qty)){ // if invalid phone
                    $("span.val_qty").html("Integer Only is Allowed!").addClass('validate');
                    validation_holder = 1;

                } else {
                    $("span.val_qty").html("");
                }
            }

            if(counter == "") {
                $("span.val_counter").html("");
            } else {
                if(!counter_regex.test(counter)){ // if invalid phone
                    $("span.val_counter").html("Please Refresh to avoid database error!").addClass('validate');
                    validation_holder = 1;

                } else {
                    $("span.val_counter").html("");
                }
            }

            if(validation_holder == 1) { // if have a field is blank, return false
                $("p.validate_msg").slideDown("fast");
                return false;
            }  validation_holder = 0; // else return true
            /* validation end */    
        }); // click end 

    }); // jQuery End
    </script>

0 个答案:

没有答案