【发布时间】:2016-06-07 16:56:22
【问题描述】:
我有一个注册表单,该表格将:
- 成功提交后清除表单
- 在提交错误时不清除表单(在我的情况下,密码不匹配)
这是我的代码:
<input name="uname" type="text" class="form-control" id="uname" placeholder="Username" value="<?php if (!empty($_POST["uname"])) { echo $_POST["uname"]; } else { echo ''; }; ?>" required>
此代码适用于在提交错误时不清除表单。虽然提交成功了,但是表单提交后表单仍然保留数据。因为,我正在使用这段代码,显然重置按钮也不起作用。那么,提交成功后如何清除呢?
无论如何,这是表单验证的代码:
if (isset($_POST['submit'])) {
try {
if ($_POST["pwd"] == $_POST["cpwd"]) {//matched passwords
$stmt = $conn->prepare("INSERT INTO tbl_customers(fld_customer_fname,
fld_customer_lname, fld_customer_gender, fld_customer_phone, fld_customer_email, fld_customer_username, fld_customer_password) VALUES(:fname, :lname,
:gender, :phone, :email, :uname, :pwd)");
$stmt->bindParam(':fname', $fname, PDO::PARAM_STR);
$stmt->bindParam(':lname', $lname, PDO::PARAM_STR);
$stmt->bindParam(':gender', $gender, PDO::PARAM_STR);
$stmt->bindParam(':phone', $phone, PDO::PARAM_STR);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
$stmt->bindParam(':uname', $uname, PDO::PARAM_STR);
$stmt->bindParam(':pwd', $pwd, PDO::PARAM_STR);
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$gender = $_POST['gender'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$uname = $_POST['uname'];
$pwd = $_POST['pwd'];
$stmt->execute();
?>
<br>
<div class="alert alert-success fade in">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<strong>Success!</strong> Account created.
</div>
<?php
}
else{//unmatched passwords
?>
<br>
<div class="alert alert-danger fade in">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<strong>Error!</strong> Passwords do not match.
</div>
<?php
}
}
【问题讨论】:
-
你会反对用javascript做吗?还是需要与 PHP 一起使用?
-
@NoReceipt4Panda 两者兼得会很棒吗?但我更喜欢它在 PHP 中。
标签: php html forms validation