dongyu5104 2017-01-10 08:55
浏览 57

如何在另一个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;
}

 ?>
  • 写回答

2条回答 默认 最新

  • douxianliu6756 2017-01-10 09:04
    关注

    in signup_form_handler.php

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

    in signup.php

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

报告相同问题?

悬赏问题

  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号