我在使用PHP进行html输入验证方面遇到了一些问题。验证本身正在运行。我有两个输入:名称和办公室。 如果我在名称输入上输入值但我没有将值放到办公室输入并单击提交按钮,则办公输入上的验证有效,但它清除/清空我在名称输入中输入的数据
我在这里做错了什么?
这是我的PHP验证:
<div class="overlay">
<img src="http://gifyo.com/public/img/loading.gif"/>
</div>
<h2>Test<h2/>
这是我的HTML代码
if (isset($_POST['submit'])){
$signatory_name = $_POST['sig_name'];
$signtory_position = $_POST['sig_position'];
if (!$_POST['sig_name']) {
$errname='<div class="alert alert-danger">Sorry there was an error: Please Enter Your Name</div>';
}
if (!$_POST['sig_office']) {
$erroffice='<div class="alert alert-danger">Sorry there was an error: Please Enter Your office</div>';
}
}
答案 0 :(得分:2)
目前还不清楚你在做什么或试图做什么,但这是我的尝试:
首先:您应该知道if (!$_POST['sig_name']) {
表示如果分配的值为FALSE,您可能需要重新考虑并改为使用empty()
。
验证输入后,您需要使用提交的值重新填充表单 - 这是一个示例:
<?php
$errname = "";
$erroffice= "";
if (!empty($_POST)) { // Only if there are POST values attached.
$signatory_name = $_POST['sig_name'];
$signtory_position = $_POST['sig_position'];
if (empty($_POST['sig_name'])) {
$errname='<div class="alert alert-danger">Sorry there was an error: Please Enter youre Name</div>';
}
if (empty($_POST['sig_office'])) {
$erroffice='<div class="alert alert-danger">Sorry there was an error: Please Enter youre office</div>';
}
if (empty($errname) && empty($erroffice)) {
//Do whatever you need with the validated inputs...
} else {
//Expose the alerts:
echo $errname.$erroffice;
}
}
?>
<form method="POST" role="form">
<input class="form-control" id="signatoryname" name="sig_name" value="<?php echo (isset($_POST['sig_name']))?$_POST['sig_name']:""; ?>" placeholder="Name:" />
<input class="form-control" id="signatoryoffice" name="sig_office" value="<?php echo (isset($_POST['sig_office']))?$_POST['sig_office']:""; ?>" placeholder="Office:" />
<!-- rest of your form and buttons -->
</form>
答案 1 :(得分:1)
你需要像Scuzzy说的那样重新填充你的表格。
大多数浏览器可能会为您服务,但您不能依赖它。
<form action="signatory.php" method="Post" role="form">
<input class="form-control " id="signatoryname" name="sig_name" placeholder="Name:" value="<?php echo !empty($_POST['sig_name']) ? $_POST['sig_name'] : ''; ?>">
<input class="form-control " id="signatoryoffice" name="sig_office" placeholder="Office:" value="<?php echo !empty($_POST['sig_office']) ? $_POST['sig_office'] : ''; ?>">
</form>