我为html表单和signup_form_handler.php创建了一个signup.php文件,用于验证html表单。在创建单独的文件进行验证之前,我尝试在同一个文件(signup.php)中验证html表单,然后我成功了。在我将验证部分分成另一个文件后,我遇到了这个问题。如何将验证错误从signup_form_handler.php返回到html表单文件(signup.php)。
signup.php
<div class="container-fluid calbox col-md-4" style="margin-right: 10px;margin-bottom: 10px;">
<h2 style="text-align: center;color:#0275d8;">Sign Up Form</h2>
<form method="POST" action="signup_form_handler.php">
<div class="form-group">
<label style="padding-top: 10px;" class="col-form-label">Name</label>
<input type="text" class="form-control" placeholder="Type your name here" value="<?php if(isset($_POST['name'])){echo $_POST['name'];}?>" name="name" id="name">
<div class="errBox" style="padding-top: 5px;">
<?php echo $nameErr;?>
</div>
</div>
<div class="form-group">
<label class="col-form-label">Email</label>
<input type="email" class="form-control" placeholder="Type your email here" value="<?php if(isset($_POST['email'])){echo $_POST['email'];}?>" name="email" id="email">
<div class="errBox" style="padding-top: 5px;">
<?php echo $emailErr;?>
</div>
</div>
<div class="form-group">
<label class="col-form-label">City</label>
<select class="form-control" id="city" name="city[]">
<option value="Kandy">Kandy</option>
<option value="Colombo">Colombo</option>
<option value="Galle">Galle</option>
</select>
</div>
<fieldset class="form-group row">
<label class="col-form-label col-xs-4">Gender</label>
<div class="col-xs-7 form-check">
<label class="form-check-label">
<input type="radio" class="form-check-input" name="gender" id="genMale" value="Male" checked>
Male
</label>
<label class="form-check-label">
<input type="radio" class="form-check-input" name="gender" id="genFemale" value="Female">
Female
</label>
</div>
</fieldset>
<div class="form-group">
<label class="col-form-label col-xs-4">Interested Areas</label>
<div class="col-xs-7 form-check">
<label class="custom-control custom-checkbox mb-2 mr-sm-2 mb-sm-0 offset-xs-1">
<input type="checkbox" name="check[]" id="web" class="custom-control-input" value="Web Design">
<span class="custom-control-indicator"></span>
<span class="custom-control-description">Web Design</span>
</label>
<label class="custom-control custom-checkbox mb-2 mr-sm-2 mb-sm-0">
<input type="checkbox" name="check[]" id="gd" class="custom-control-input" value="Graphic Design">
<span class="custom-control-indicator"></span>
<span class="custom-control-description">Graphic Design</span>
</label>
<label class="custom-control custom-checkbox mb-2 mr-sm-2 mb-sm-0">
<input type="checkbox" name="check[]" id="se" class="custom-control-input" value="Software Engineering">
<span class="custom-control-indicator"></span>
<span class="custom-control-description">Software Engineering</span>
</label>
</div>
</div>
<div class="errBox" style="padding-top: 5px;">
<?php echo $checkErr;?>
</div>
<div class="form-group">
<label class="col-form-label">Phone Number</label>
<input type="tel" class="form-control" placeholder="Type your phone number here" value="<?php if(isset($_POST['phone'])){echo $_POST['phone'];}?>" name="phone" id="phone">
</div>
<div class="form-group">
<label class="col-form-label">Password</label>
<input type="password" class="form-control" placeholder="Type a password here" name="pass" id="pass">
<div class="errBox" style="padding-top: 5px;">
<?php echo $passErr;?>
</div>
</div>
<div class="form-group">
<label class="col-form-label">Confirm Password</label>
<input type="password" class="form-control" placeholder="Retype the password here" name="cpass" id="cpass">
<div class="errBox" style="padding-top: 5px;">
<?php echo $cpassErr;?>
</div>
</div>
<div class="form-group row offset-sm-8" style="padding-left: 10px;">
<button type="submit" class="btn btn-outline-primary" name="btn" value="signup" id="signupbtn">Register</button>
</div>
</form>
</div>
signup_form_handler.php
<?php
$nameErr = $emailErr = $checkErr = $passErr = $cpassErr = "";
$name = $email = $pass = $check = $checklist = $value = $gender = $city = $phone = $dateTime = "";
if (isset($_POST['btn'])) {
if (empty($_POST["name"])) {
$nameErr = '<div class="alert alert-danger">Name is required !</div>';
} else{
$name = test_validate($_POST['name']);
}
if (empty($_POST["email"])) {
$emailErr = '<div class="alert alert-danger">Email is required !</div>';
}else{
$email = test_validate($_POST['email']);
}
if (isset($_POST['phone'])) {
$phone = test_validate($_POST['phone']);
}
if (empty($_POST["pass"])) {
$passErr = '<div class="alert alert-danger">Password is required !</div>';
} elseif (!empty($_POST["pass"]) < 6) {
$passErr = '<div class="alert alert-danger">Minimum 6 characters required !</div>';
}
if (empty($_POST["cpass"])) {
$cpassErr = '<div class="alert alert-danger">Confirm password is required !</div>';
} elseif ($_POST["pass"] != $_POST["cpass"]) {
$cpassErr = '<div class="alert alert-danger">Password fields do not match !</div>';
}else{
$pass = test_validate($_POST['pass']);
}
if (empty($_POST['check'])) {
$checkErr = '<div class="alert alert-danger">You should at least select 1 area that you are interested !</div>';
}
if (isset($_POST['gender'])) {
$gender = test_validate($_POST['gender']);
}
if (isset($_POST['city'])) {
foreach ($_POST['city'] as $citylist) {
$city = $citylist;
}
}
date_default_timezone_set("Asia/Colombo");
$dateTime = date("Y-m-d h:i:s a");
}
function test_validate($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
答案 0 :(得分:0)
in signup_form_handler.php
SESSION_START();
SESSION['data'] = $data;
in signup.php
SESSION_START();
print SESSION['data'];
答案 1 :(得分:0)
您可以将错误消息保存在会话变量中。如果出现错误,您可以将用户重定向回signup.php页面。尝试使用signup_form_handler.php中的代码。它应该位于第一个 If 子句的底部
$error_message = "";
if ($nameErr != "") $error_message = $nameErr;
if ($emailErr != "") $error_message = $emailErr;
if ($checkErr != "") $error_message = $checkErr;
if ($passErr != "") $error_message = $passErr;
if ($cpassErr != "") $error_message = $cpassErr;
if ($error_message != "") {
$_SESSION['error_message'] = $error_message;
header("Location: " . $error_message);
exit;
}
还添加以下行:session_start();在signup_form_handler.php和signup.php。
的顶部在signup.php文件中,您必须添加以下行:
if (isset($_SESSION['error_message']) && $_SESSION['error_message'] != "") {
echo $_SESSION['error_message'];
unset($_SESSION['error_message']);
}