Kie21
Kie21

Reputation: 184

Show error if form is submitted

I'm wanting my form to show an error such as "Enter first name" if the field has not being filled in. I have got to right code(I think) to do this but it is showing the error when the page is first loaded. I want it to be displayed when the form has been submitted.

    <div class="Row">
<div class="Lable">First Name:</div> <!--End of Lable-->
<div class="input">
<input type="text" id="firstname" class="detail" name="firstname" placeholder="First Name"  />
</div> <!--End input-->
</div> <!--End row-->
<span class="error">* <?php echo $nameErr;?></span>

The PHP validation is

 if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["firstname"])) {
 $nameErr = "Name is required";
} else {
 $firstname = test_input($_POST["name"]);
 // check if name only contains letters and whitespace
 if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {
   $nameErr = "Only letters and white space allowed"; 
 }
}

Thanks

Upvotes: 0

Views: 624

Answers (3)

Friedrich
Friedrich

Reputation: 2290

I would double check the form with PHP and JavaScript

For the javascript part add this line to your form, so the function validate() will run before the forms content will be sent to the server.

<form onsubmit="validate()">

Then you can add a javascipt function to your document like this:

function validate() {
  if (/*check if fields are filled out properly*/) {
    this.submit();
  } else {
    // show message here when fields are missing

    return false; // this line is important otherwise the form gets submitted anyway
  }
}

For the PHP part it is enough to check for the differnt fields with isset()

This would look like this:

$formOK = true;
if (!isset($_POST["field-name-1"]) || $_POST["field-name-1"] == "") {
  echo "field-1 is missing";
  $formOK = false
}
if (!isset($_POST["field-name-2"]) || $_POST["field-name-2"] == "") {
  echo "field-2 is missing";
  $formOK = false
}

if (formOK) {
  echo "everything is fine";
  // do something with your form data
}

To double check is definitly the best way because first of all you are not having so many requests and the javascript is more flexible in is reactions and speed but the PHP will provide you the guarantee that nothing will go wrong.

Upvotes: 1

blex
blex

Reputation: 25634

In order to apply the validation only if the form was submitted, you could check if you received $_POST['submit'] this way :

<?php
    $nameError="";

    if(isset($_POST['submit']))
    {
        //validate the form
        if(condition is not met){ $nameError="your error message"; }
    }
?>

<form>
    ...
    <input type="text" name="firstname"/>
    <input type="submit" name="submit"/>
    <span class="myError">* <?=$nameError?> </span>
</form>

Here, the submit button will send $_POST['submit']. The error message at the beginning is empty. If there is an error, change it. <?=$nameError?> will then display it whether it is empty or not.

Upvotes: 0

hsz
hsz

Reputation: 152226

Just try with:

if (isset($nameErr)) {
    echo '<span class="error">* ' . $nameErr . '</span>';
}

Upvotes: 0

Related Questions