我有3个字段正确验证。提交正确地发布到文本文件。然而

时间:2016-10-05 21:33:08

标签: php

我有3个字段正确验证。提交正确地发布到文本文件。但是,即使单击提交按钮而未填写信息,它们也会发布 - (因为我在代码中将每个部分分开)。只有2 - 没有任何空信息 - 打印到文本文件而不先填写3个字段。我该如何解决这个问题?很抱歉给您带来不便,我在另一篇文章中回答了我自己的问题并且能够解决问题。谢谢你的时间。

这是我的代码

<?php
    if(isset($_POST['name'], $_POST['email'], $_POST['website'])) {
        if(empty($_POST['name'])) {
            $errors[] = "Name is required";
        } else{
            $name = htmlentities($_POST['name']);
            $name = test_input($_POST['name']);
            if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
                $errors[] = "Only letters and white space allowed for the name";
            }
        }
        if(empty($_POST['email'])) {
            $errors[] = "Please provide your Email address.";
        } else if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ===       
    false){
            $errors[] = "Your Email is not valid.";
        } else {
            $email = htmlentities($email);
        }
        if(empty($_POST['website'])) {
            $errors[] = "Please provide your company URL.";
        } else{
            $website = htmlentities($_POST['website']);
            $website = test_input($_POST['website']);
            if(!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
                $errors[] = "Please provide a valid URL for your company.";
            }
        }
    } 

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

    $name = $_POST['name'];
    $email = $_POST['email'];
    $website = $_POST['website'];
    $data = $name . " - " . $email . " - " . $website;
    $file = "textfile.txt";

    if($_POST){
        file_put_contents($file, $data . PHP_EOL, FILE_APPEND);
    }
?>

在我的HTML中                                                                        

    <?php
    if(empty($errors) === false){
    ?>
    <ul>
    <?php
    foreach($errors as $error){
    echo "<li>",$error,"</li>";
    }
    ?>
    </ul>
    <?php
    }else{
    if(isset($name, $email, $website)){
    echo "<b>Thank you for your submission.</b>"; 
    } 
    }
    ?> 
    </div>
    <div class="wrapper w3-round-xlarge">
    <div class="formtitle w3-round-xlarge">Thank you for filling in all fields below
    </div>
    <div class="formwrapper w3-round-xlarge">
    <form name="mobile" id="mobile" method="post" enctype="multipart/form-data" action="data.php"><br/> 

等。 (html文档正确开始和结束。感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

如果需要所有三个输入,请将所需输入放入html输入。

<Input name="email" required />

为每个输入执行此操作。这将不允许在提交所有字段之前处理表单。

答案 1 :(得分:0)

很抱歉给任何人花时间阅读我的帖子给您带来不便。我只是通过从我的html上面的PHP代码中删除以下内容并更改以下

来解决问题
    if($_POST){
    file_put_contents($file, $data . PHP_EOL, FILE_APPEND);
    }

    to ... 
    <?php
    }else{
    if(isset($name, $email, $website)){
    file_put_contents($file, $data . PHP_EOL, FILE_APPEND);
    echo "<b>Your submission has been sent.</b>"; 
    } 

    and adding it to the html div area