我在這裏有一個php記錄,我有一個多記錄的編輯頁面,並具有表單驗證。我的問題是在我的循環調用多個記錄。for循環中的多個記錄中的表單驗證
就像在上面的圖片,我的PR#文本框有一個消息,甚至有000174名和000176有一個正確的輸入兩個PR#。如何使我的驗證僅適用於每個ID具有無效輸入的文本框。
請幫忙嗎?
<form name="register_form" id="register_form" action="" method="post">
<?php
$id=$_POST['checkbox'];
$drop=$_POST['drop'];
$tier_two=$_POST['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="counter[]" type="text" value="<?php echo $row['counter'] ?>" readonly="readonly"/><span class="val_counter"></span> </p>
<p> <label for="">Item</label> <input name="item[]" type="text" value="<?php echo $row['item_name'] ?>" readonly="readonly"/><span class="val_item"></span> </p>
<p> <label for="">Description</label> <input name="desc[]" type="text" value="<?php echo $row['item_description'] ?>" readonly="readonly"/><span class="val_desc"></span> </p>
<p> <label for="">PR Quantity</label>
<input name="pr_qty[]" id="pr_qty[]" class="textbox tb1" type="text" value="<?php echo $row['total_quantity']; ?>" />
<span class="val_pr_qty"></span>
</p>
<p> <label for="">PR #</label> <input name="pr[]" type="text" value="<?php echo $row['pr'] ?>" /><span class="val_pr"></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 item = $("form#register_form input[name='item']").val();
var item_regex = /^[a-zA-Z0-9]+$/; // reg ex cost check
var desc = $("form#register_form input[name='desc']").val();
var desc_regex = /^[a-zA-Z0-9]+$/; // reg ex cost check
var pr_qty = $("form#register_form input[name='pr_qty']").val();
var pr_qty_regex = /^[a-zA-Z0-9]+$/; // reg ex cost check
var pr = $("form#register_form input[name='pr']").val();
var pr_regex = /^[0-9]+$/; // reg ex qty check
var counter = $("form#register_form input[name='counter']").val();
var counter_regex = /^[0-9]+$/; // reg ex qty check
/* validation start */
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!").addClass('validate');
validation_holder = 1;
} else {
$("span.val_desc").html("");
}
}
if(pr_qty == "") {
$("span.val_pr_qty").html("This field is Required.").addClass('validate');
validation_holder = 1;
} else {
if(!pr_qty_regex.test(pr_qty)){ // if invalid phone
$("span.val_pr_qty").html("Invalid Special Characters!").addClass('validate');
validation_holder = 1;
} else {
$("span.val_pr_qty").html("");
}
}
if(pr == "") {
$("span.val_pr").html("This field is required.").addClass('validate');
validation_holder = 1;
} else {
if(!pr_regex.test(pr)){ // if invalid phone
$("span.val_pr").html("Integer Only is Allowed!").addClass('validate');
validation_holder = 1;
} else {
$("span.val_pr").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>
但我如何改變,如果記錄在循環? – user3097736
將我的值追加到for循環的id ....像這樣...-> –
對不起我,但我不是那麼好使用jquery -_- – user3097736