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....
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?