停止PHP如果空文本框和处理其他HTML表单


stop php if empty textbox and process other html forms

下面是我的代码,它检查文本框是否为空,然后在文本框旁边给出警告,一旦点击提交。

<?php
if(isset($_POST["submit"])) {

    $fqdn = $_POST["fqdn"];
    $ip = $_POST["ip"];
    $fileText = $fqdn."'n".$ip;
    $file = fopen("inputFile.txt","w");
    fwrite($file, $fileText);
    fclose($file);
}
?>
 <form action = "<?php $_PHP_SELF ?>" method = "post">
            <table style= "width:400px">
                <tr class="spaceUnder">
                    <td><b>FQDN:</b></td>
                    <td><input type="text" name="fqdn" placeholder="server.domain.com"/></td>
                    <td>  <?php if(isset($_POST['fqdn']) && $_POST['fqdn'] == ''){ echo "<font color='red'>FQDN cannot be empty</font>";} ?> </td>
                </tr>
                <tr class="spaceUnder">
                    <td><b>IP:</b></td>
                    <td><input type="text" name="ip" placeholder="***.***.***.***"/></td>
                    <td>  <?php if(isset($_POST['ip']) && $_POST['ip'] == ''){ echo "<font color='red'>IP cannot be empty</font>"; } ?> </td>
                </tr>
                <tr>
                    <td align="center" colspan="2">
                    <input type = "submit" name="submit" value="Submit"/>
                   </td>
                </tr>
            </table>
            </form>

点击提交后,php仍然会写入文件。如果我给出exit()或返回false;在下面的步骤:

<td>  <?php if(isset($_POST['fqdn']) && $_POST['fqdn'] == ''){ echo "<font color='red'>FQDN cannot be empty</font>"; exit();} ?> </td>

表单变得不完整,意味着IP文本框和提交按钮将不存在。有办法弥补吗?

我们这里有。我们用两个变量来显示误差。第一个变量是空的,因为我们不知道后面是什么。在我们发布数据之后,我们可以看到该帖子是否为空。如果post为空,则将错误消息赋值给该变量,并将其显示在表中。

$error_FQDN = "";
$error_ip = "";
if(isset($_POST["submit"])) {
    if($_POST["fqdn"] == "" || $_POST["ip"] == ""){
        if($_POST["fqdn"] == ""){
            $error_FQDN = "FQDN cannot be empty!";
        }
        if($_POST["ip"] == ""){
            $error_ip = "IP cannot be empty!";
        }
    } else {
        $fqdn = $_POST["fqdn"];
        $ip = $_POST["ip"];
        $fileText = $fqdn."'n".$ip;
        $file = fopen("inputFile.txt","w");
        fwrite($file, $fileText);
        fclose($file);
    }
}
<form action = "<?php $_PHP_SELF ?>" method = "post">
    <table style= "width:400px">
        <tr class="spaceUnder">
            <td><b>FQDN:</b></td>
            <td><input type="text" name="fqdn" placeholder="server.domain.com"/></td>
            <td><?php echo $error_FQDN ?></td>
        </tr>
        <tr class="spaceUnder">
            <td><b>IP:</b></td>
            <td><input type="text" name="ip" placeholder="***.***.***.***"/></td>
            <td><?php echo $error_ip ?></td>
        </tr>
        <tr>
            <td align="center" colspan="2">
            <input type = "submit" name="submit" value="Submit"/>
           </td>
        </tr>
    </table>
</form>

试试这个,

// Check for form submit and if fields aren't empty
if(isset($_POST["submit"]) && $_POST['fqdn'] != "" && $_POST['ip'] != "") {
    $fqdn = $_POST["fqdn"];
    $ip = $_POST["ip"];
    $fileText = $fqdn."'n".$ip;
    $file = fopen("inputFile.txt","w");
    fwrite($file, $fileText);
    fclose($file);
}

通过验证fqdn和ip,如果值不为空,则执行if case。