我已经创建了动态添加/删除输入字段。我想通过某些验证。如果用户将其保留为空白,则在提交表单后应显示一条消息。我希望显示PHP验证。
<div class="panel panel-default">
<div class="panel-heading"><center><b>Allocation of Funds</b></center></div>
<div class="panel-body">
<div class="row">
<div class="col-md-5"><label>Allocation Items <b style="color:#FF0000;">*</b></label></div>
<div class="col-md-5"><label>Amount <b style="color:#FF0000;">*</b></label></div>
<div class="col-md-2"></div>
</div>
<div class="row">
<div class="col-md-5">
<div class="form-group">
<input type="text" class="form-control" name="allocate_items[]" placeholder=""></div>
</div>
<div class="col-md-5">
<div class="form-group">
<input type="text" class="form-control" name="allocate_amount[]" placeholder=""></div>
</div>
<div class="col-md-2">
<button type="button" class="btn btn-success" id="add-allocation-fields"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add</button>
</div>
</div>
<div id="fund-allocation-fields"></div>
<p class="help-block"><i>Total amount must be equal to the goal amount.</i></p>
</div>
</div>
<script type="text/javascript">
var i = 0;
jQuery(document).ready(function($) {
//fadeout selected item and remove
$(document).on('click', '#remove-allocation-fields', function(event) {
event.preventDefault();
$(this).parent().fadeOut(300, function() {
$(this).parent().empty();
return false;
});
});
var rows = '<div class="fund-fields"><div class="row"><div class="col-md-5"><div class="form-group"><input type="text" class="form-control" name="allocate_items[]" placeholder=""></div></div><div class="col-md-5"><div class="form-group"><input type="text" class="form-control" name="allocate_amount[]" placeholder=""></div></div><div class="col-md-2"><button type="button" class="btn btn-danger" id="remove-allocation-fields"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Remove</button></div></div><div class="clear"></div></div>';
//add input
$('#add-allocation-fields').click(function() {
$(rows).fadeIn("slow").appendTo('#fund-allocation-fields');
i++;
return false;
});
});
</script>
谢谢!
答案 0 :(得分:0)
将一个输入值存储在会话变量或全局会话中,并使用条件语句将其与所需输入值进行比较。就这么简单。
喜欢
if(isset($_SESSION['A']) && empty(trim($_POST['RequiredField']))){
echo 'required field can\'t be empty';
}
答案 1 :(得分:0)
我找到了答案...
<?php
$errors = array();
if (isset($_POST['error_test'])) {
$allocate_items = $_POST['allocate_items'];
foreach ( $allocate_items as $key => $allocate_item ) {
if ( empty($allocate_item[$key])) {
$errors[$key] = '<p>Allocate Item field is required</p>';
}
}
if ( ! empty( $errors ) ) {
foreach ($errors as $key => $value) {
echo $value;
}
}
}
?>