PHP表单无法验证,仅在提交时提供空白页

时间:2019-03-13 05:44:22

标签: php html

我决定通过测试来做一点测试,以查看该表单是否会检测到一个空的输入字段,并且它不起作用,我不知道问题出在哪里,我也不想写剩下的该项目的问题,如果这一件小事不起作用,那么这是代码,我已经看过它了,我认为我没有错过任何事情。

这是HTML:

<html>
    <head>
        <title>Title of the document</title>
    </head>
    <body>
        <form method="POST" action="myform.php">
            <fieldset>
                <legend>Personal Info</legend>
                First name <input name="name" type="text"> 
                Middle name <input name="middlename" type="text"> 
                Surname <input name="lastname" type="text">  
                Age <input name="age" type="number"> 
                Date of birth <input name="dob"  type="date">
            </fieldset>
            <fieldset>
                <legend>Regional & location info</legend>
                Continent 
                <select>
                    <option value="europe">Europe</option>
                    <option value="americas">America</option>
                    <option value="africa">Africa</option>
                    <option value="asia">Asia</option>
                    <option value="australia">Australia</option>
                    <option value="eurasia">Eurasia</option>
                </select>
                Country <input type="text"> State <input type="text"> 
                City <input type="text">
                Street number <input type="number"> 
                Street name <input type="text"> <br><br>
                Suburb <input type="text"> Postcode <input type="number"> 
                If none of these apply to your accommodations, enter a typed location here <input  type="text">
            </fieldset>
            <fieldset>
                <legend>Previous lifestyle accommodations</legend>
                Previous &/or most recent job title <input name="job" type="text"> 
                First   time job seeker <input type="checkbox" name="check1" value="ftjb"> 
                I'm a student <input type="checkbox" name="check2" value="ias"> 
                Previous &/or most recent acedemic title <input name="school" type="text"> 
                First time applying for a qualification <input type="checkbox" name="check3" value="ftafaq"> 
                I have work experience <input type="checkbox" name="check4" value="ihwe">
            </fieldset>
            <fieldset>
                <legend>Details of arrival</legend>
                Reason for arrival of all parties <input name="reason" type="text"> 
                Date of arrival <input name="arrival" type="date"> 
                Amount of stay expectancy 
                <input type="checkbox" name="check3">Temporary 
                <input type="checkbox" name="check4">Longterm
            </fieldset>
            <fieldset>
                <legend>Signiture</legend>
                <input type="text"> 
            </fieldset>
            <input type="submit" value="Submit"><button type="reset">Reset</button>
        </form>
    </body>
</html> 

这是到目前为止我在PHP中为表单所做的事情:

<?php
$nameInvalid = "";
$middleInvalid = "";
$surnameInvalid = "";
$ageInvalid = "";
$dobInvalid = "";
$countryInvalid = "";
$cityInvalid = "";
$strtInvalid = "";
$strnameInvalid = "";
$suburbInvalid = "";
$postcodeInvalid = "";
$jobInvalid = "";
$ftjsInvalid = "";
$iasInvalid = "";
$schoolInvalid = "";
$check1Invalid = "";
$check2Invalid = "";
$checl3Invalid = "";
$check4Invalid = "";
$reasonInvalid = "";
$arrivalInvalid = "";
if (isset($_POST['submit'])) {
    if (empty($_POST["name"])) {
        $nameInvalid = "Name is required";
    }
}
?>

4 个答案:

答案 0 :(得分:1)

请在按钮中添加 name =“ submit” 属性。

<input type="submit" value="Submit" name="submit">

并且还请在验证中添加回显,以打印验证消息,如下所示。

if (isset($_POST['submit'])) {
    if (empty($_POST["name"])) {
       echo $nameInvalid = "Name is required";
    }
}

答案 1 :(得分:0)

HTML

如果表单不符合您的规则,您需要使用HTML5 validation来阻止发送表单。

摘自官方文档:

最简单使用的HTML5验证功能是required属性-如果要强制输入,则可以使用此属性标记元素。设置此属性后,当输入为空(输入也将被视为无效)时,表单将不会提交(并显示错误消息)。

根据您的情况,尝试将required标记添加到要测试的字段,例如name字段:

First name <input name="name" type="text" required>

要使其更加有趣,请添加一些CSS:

input:invalid {
  border: 2px dashed red;
}

input:valid {
  border: 2px solid black;
}

PHP

要验证PHP响应,您需要做的是以下操作:

if (!$_REQUEST["name"]) {
    //If the name field is empty
    die("Name is missing");
    //Replace this with whatever logic fits your code
}

我们正在使用$_REQUEST,这意味着同时使用 $_POST$_GET,但是如果您愿意,可以坚持使用$_POST 。我们正在使用if(!$val)进行验证,这是最简单的方法,但是有一些警告(见下文),您也可以使用if(empty($val)),但对于字符串,第一个很好。

摆脱在脚本开头定义为空的变量列表。相反,要查看设置数组 if 的值,该值不应为空,而是为空。例如:

# Repeat this structure for each form field you want
if (!$_REQUEST["name"]) {
    //If the name field is empty
    $empty_fields[] = "name";
    //Fill the $empty_fields array with all the fields that are missing
}

# At the end, cycle thru the missing fields and tell the user
if(!empty($empty_fields)){
    die("The following fields are missing: ".implode(", ", $empty_fields));
}

注意事项

使用if(!$val)是快捷方式,使用won't work是允许的值,例如“”(空格)或“ 0”(零),但是如果希望使用字符串值,则可以使用。< / p>

答案 2 :(得分:0)

使用此

<input type="submit" name="submit" value="Submit">

答案 3 :(得分:0)

我检查了代码及其正常工作。只需提供名称以提交按钮并在php页面中回显验证消息即可。

<html>
<head>
<title>Title of the document</title>
</head>
<body>
<form method="POST" action="test1.php">
<fieldset>
<legend>Personal Info</legend>
First name <input name="name" type="text"> 
Middle name <input       name="middlename" type="text"> Surname <input name="lastname" type="text">  Age <input name="age" type="number"> Date of birth <input name="dob"  type="date">
</fieldset>
<fieldset>
<legend>Regional & location info</legend>
Continent 
<select>
<option value="europe">Europe</option>
<option value="americas">America</option>
<option value="africa">Africa</option>
<option value="asia">Asia</option>
<option value="australia">Australia</option>
<option value="eurasia">Eurasia</option>
</select>
Country <input type="text"> State <input type="text"> City <input type="text">
    Street number <input type="number"> Street name <input type="text"> <br><br>
Suburb <input type="text"> Postcode <input type="number"> If none of    these apply to your accommodations, enter a typed location here <input  type="text">
</fieldset>
<fieldset>
<legend>Previous lifestyle accommodations</legend>
Previous &/or most recent job title <input name="job" type="text"> First   time job seeker <input type="checkbox" name="check1" value="ftjb"> I'm a student <input type="checkbox" name="check2" value="ias"> Previous &/or most recent acedemic title <input name="school" type="text"> First time applying for a qualification <input type="checkbox" name="check3" value="ftafaq"> I have work experience <input type="checkbox" name="check4" value="ihwe">
</fieldset>
<fieldset>
<legend>Details of arrival</legend>
Reason for arrival of all parties <input name="reason" type="text"> Date of arrival <input name="arrival" type="date"> Amount of stay expectancy <input type="checkbox" name="check3">Temporary <input type="checkbox" name="check4">Longterm
</fieldset>
<fieldset>
<legend>Signiture</legend>
<input type="text"> 
</fieldset>
<input type="submit" name="submit" value="Submit">
<button type="reset">Reset</button>
</form>
</body>
</html> 

在php页面中显示回显消息。

<?php
$nameInvalid = "";
$middleInvalid = "";
$surnameInvalid = "";
$ageInvalid = "";
$dobInvalid = "";
$countryInvalid = "";
$cityInvalid = "";
$strtInvalid = "";
$strnameInvalid = "";
$suburbInvalid = "";
$postcodeInvalid = "";
$jobInvalid = "";
$ftjsInvalid = "";
$iasInvalid = "";
$schoolInvalid = "";
$check1Invalid = "";
$check2Invalid = "";
$checl3Invalid = "";
$check4Invalid = "";
$reasonInvalid = "";
$arrivalInvalid = "";


if (isset($_POST['submit']))
{
    if (empty($_POST["name"])) 
    {
        echo $nameInvalid = "Name is required";
    }
}
?>

完成了。