dongtangjie0495 2017-06-04 20:40
浏览 30
已采纳

以PHP形式验证并保存到文件

   if($_POST){



$errors = array();


//start validation
if(empty($_POST['email'])){
    $errors['email1'] = "Your Email Cannot be empty"; 
}

if (!filter_var(['email'], FILTER_VALIDATE_EMAIL)){
    $errors['email2'] = "Email not valid!";
}


if(empty($_POST['phone'])){
    $errors['phone1'] = "Your Number Cannot be empty"; 
}
if(strlen($_POST['phone']) < 8){
    $errors['phone2'] = "Your Phone Number must be at least 8 characters long";
}

if(strlen($_POST['phone']) > 8){
    $errors['phone3'] = "Your Phone Number must be not more than 8 characters long";
}

//check errors
if(count($errors) == 0)
{
    //redirect to home page
    header("Location: index.php");
    exit();
}
}

This is my code which needs to be validated. The validation works ok but here is the problem.

<div class="form-group">
            <label for="name">Email</label>
            <input type="email" class ="form-control" name="email" id="email" placeholder="Enter Email">
            <p><?php if(isset($errors['email1'])) echo $errors['email1'];?></p>
            <p><?php if(isset($errors['email2'])) echo $errors['email12'];?></p>
        </div>

        <div class="form-group">
            <label for="phone">Phone Number</label>
            <input type="text" class ="form-control" name="phone" id="phone" placeholder="Enter Phone Number">
            <p><?php if(isset($errors['phone1'])) echo $errors['phone1'];?></p>
            <p><?php if(isset($errors['phone2'])) echo $errors['phone2'];?></p>
            <p><?php if(isset($errors['phone3'])) echo $errors['phone3'];?></p>
        </div>

I have the form set to save to a file. The inputs will be validated and the error messages will be displayed but the form that has wrong inputs will still be saved to a file. Any help would be appreciated thanks.

<?php
    if(isset($_POST['email'])  && isset($_POST['phone'])  && isset($_POST['enquiry'])){

        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $enquiry = $_POST['enquiry'];

        $file = "form.txt";


        $save = "Email: ".$email."
".
                "Phone: ".$phone."
".
                "Comment: ".$enquiry."
";

        file_put_contents($file,$save, FILE_APPEND);
}
?>
  • 写回答

1条回答 默认 最新

  • doumiyi7063 2017-06-04 21:04
    关注

    Based off your comment, you want ONLY VALID data to go into the file.

    But your PHP check is looking to see if ANY values are set, here:

    if(isset($_POST['email'])  && isset($_POST['phone'])  && isset($_POST['enquiry'])) {
    

    If you change that to check to make sure ONLY valid data is present, then you'd be good. Adding a simple && count($errors) == 0 could do it (if I understand the structure of your files).

    So just replace that line above with this:

    if(isset($_POST['email'])  && isset($_POST['phone'])  && isset($_POST['enquiry']) && count($errors) == 0) {
    

    So your file would look something like this:

        ....PHP code above here....
    
        //check errors
        if(count($errors) == 0)
        {
              if(isset($_POST['email'])  && isset($_POST['phone'])  && isset($_POST['enquiry'])){
    
                    $email = $_POST['email'];
                    $phone = $_POST['phone'];
                    $enquiry = $_POST['enquiry'];
    
                    $file = "form.txt";
    
                    $save = "Email: ".$email."
    ".
                            "Phone: ".$phone."
    ".
                            "Comment: ".$enquiry."
    ";
    
                    file_put_contents($file,$save, FILE_APPEND);
            }
    
            //redirect to home page
            header("Location: index.php");
            exit();
        } // end count($errors) == 0 check
    } // end if($_POST) check
    ?>
    
    <div class="form-group">
            <label for="name">Email</label>
            <input type="email" class ="form-control" name="email" id="email" placeholder="Enter Email">
            <p><?php if(isset($errors['email1'])) echo $errors['email1'];?></p>
            <p><?php if(isset($errors['email2'])) echo $errors['email12'];?></p>
        </div>
    
    ....HTML code continues here....
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效