简体   繁体   中英

PHP form doesn't validate, only gives blank page on submit

I decided to do a little test by testing to see if the form would detect an empty input field, and it didn't work, I don't know what the problem is and I don't want to write the rest of the project if this one small thing doesn't work so here's the code, I've looked over it and I don't think I've missed anything.

Here's the 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> 

And here's what I've done in PHP so far for the form:

<?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";
    }
}
?>

Please add name="submit" attribute into the button.

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

And also please add echo into the validation to print validation message like below.

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

HTML

You need to use HTML5 validation to prevent the form from being sent if it doesn't conform to your rules.

From the official documentation:

The simplest HTML5 validation feature to use is the required attribute — if you want to make an input mandatory, you can mark the element using this attribute. When this attribute is set, the form won't submit (and will display an error message) when the input is empty (the input will also be considered invalid).

In your case, try adding the required tag to the fields you want to test, for instance the name field:

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

To make it a little bit more fancy, add some CSS:

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

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

PHP

To validate PHP responses, all you need to do is the following:

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

We're using $_REQUEST , which means both $_POST and $_GET , but you can stick to $_POST if you'd like. We're using if(!$val) to validate, it's the easiest way, but has caveats (see below), you can also use if(empty($val)) but for strings, the first is fine.

Get rid of the list of variables you define as empty in the beginning of your script. Instead look at setting an array if a value that shouldn't be empty, is empty. For instance:

# 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));
}

Caveat

Using a if(!$val) is a shortcut, and won't work if you allow values like " " (space) or "0" (zero), but works fine if you're expecting string values.

用这个

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

I checked code and its working properly. Just give name to submit button and echo validation message in php page.

<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> 

Display echo message in php page.

<?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";
    }
}
?>

Its done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM