我这里有一个php记录,我有一个多个记录的编辑页面,并有表单验证。我的问题出在我的for循环中,用于调用多个记录。
就像上图中一样,我的PR#文本框中有一条消息,即使两个PR#的id为000174和000176也有正确的输入。如何使我的验证仅适用于每个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>
答案 0 :(得分:0)
每个记录的span标记的id是相同的,所以如果任何记录中都有错误,它将反映在每个记录的span标记中。对于每个记录使用不同的id就是解决方案...
答案 1 :(得分:0)
你必须像这样循环每个输入
$("input:text[name=counter]").each(function(){
alert($(this).val());
});