如何在另一个php文件中验证html表单并将错误返回到html表单文件

I've created a signup.php file for html form and signup_form_handler.php for validate the html form. Before create a separate file for validation I tried to validate the html form in the same file (signup.php) and I succeeded. After I separate the validation part into another file I have this problem. How can i get back the validation errors from signup_form_handler.php to the html form file (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 &nbsp;
                </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;
}

 ?>
douqianxun8540
douqianxun8540 既然我是初学者,你可以建议一种方法吗?
接近 4 年之前 回复
dongpang2483
dongpang2483 在signup_form_handler中将错误放入SESSION并在注册检查中查看是否存在任何错误。在注册中处理后,将其从会话中删除
接近 4 年之前 回复

2个回答



in signup_form_handler.php </ p>

  SESSION_START(); 
SESSION ['data' signup.php中的= $ data;
</ code> </ pre>

</ p>

  SESSION_START(); 
print SESSION ['data '];
</ code> </ pre>
</ div>

展开原文

原文

in signup_form_handler.php

SESSION_START();
SESSION['data'] = $data;

in signup.php

SESSION_START();
print SESSION['data'];

Well you can save the error message in a session variable. In case of error you can redirect the user back to the signup.php page. Try following code in signup_form_handler.php. It should go at the bottom of the first If clause

$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;
}

Also add the line : session_start(); at the top of the signup_form_handler.php and signup.php.

In signup.php file, you have to add the lines:

if (isset($_SESSION['error_message']) && $_SESSION['error_message'] != "") {
   echo $_SESSION['error_message'];
   unset($_SESSION['error_message']);
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐