Samuel
Samuel

Reputation: 109

PHP form validation without resetting all other values

I have a form with 3 fields, and I want to validate the fields.

Let's say that the user make a mistake or didn't introduce the email, then all other fields (name and Address) reset to blank.

Is there any way to show the error Message (Email is required) without resetting the name and address so the user doesn't have to introduce all values again?

This is a great help for user the form has multiple values.

Thank you so much

Please see my PHP code below:

<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>

<?php
// define variables and set to empty values
$nameErr = $emailErr = $addressErr = "";
$name = $email = $address = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

if (empty($_POST["address"])) {
 $AddressErr = "Address is required";
} else {
 $address = test_input($_POST["address"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$address)) {
  $addressErr = "Only letters and white space allowed";
}
}


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

if (empty($_POST["email"])) {
  $emailErr = "Email is required";
} else {
   $email = test_input($_POST["email"]);
 // check if e-mail address is well-formed
 if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
   $emailErr = "Invalid email format";
 }
 }


 <p><span class="error">* required field.</span></p>
 <form method="post" action="<?php echo  htmlspecialchars($_SERVER["PHP_SELF"]);?>">


<input type="text" name="name" placeholder="First Name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>

<input type="text" name="email" placeholder="Email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>


<input type="text" name="address" placeholder="Address">
<span class="error">* <?php echo $AddressErr;?></span>
<br><br>


<input type="submit" name="submit" value="SAVE">
</form>


<?php

if ($nameErr == '' && $emailErr == '' && $AddressErr == '') 
{

   $db = pg_connect('host=localhost dbname=test user=samuelraul password=naikaerikamber');

    $firstname = pg_escape_string($_POST['name']);
    $emailaddress = pg_escape_string($_POST['email']);
    $address = pg_escape_string($_POST['address']);

 $query = "INSERT INTO host(firstname, emailaddress, address) VALUES('" .     $firstname . "', ' '" . $emailaddress . "',  '" . $address . "')";

    $result = pg_query($db, $query);
    if (!$result) {
        $errormessage = pg_last_error();
        echo "Error with query: " . $errormessage;
        exit();
    }
    pg_close();

 }
?>

</body>
</html>

Upvotes: 1

Views: 1051

Answers (2)

Uttam Kumar Roy
Uttam Kumar Roy

Reputation: 2058

You can use value="<?php echo $name;?>" for name field because you already define $name variable.You can use $email and $address variable for email and address field respectively

<form method="post" action="<?php echo  htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <input type="text" name="name" placeholder="First Name" value="<?php echo $name;?>">
    <span class="error">* <?php echo $nameErr;?></span>
    <br><br>
    <input type="text" name="email" placeholder="Email" value="<?php echo $email;?>">
    <span class="error">* <?php echo $emailErr;?></span>
    <br><br>
    <input type="text" name="address" placeholder="Address" value="<?php echo $address;?>">
    <span class="error">* <?php echo $AddressErr;?></span>
    <br><br>
    <input type="submit" name="submit" value="SAVE">
</form>

Upvotes: 2

KIMB-technologies
KIMB-technologies

Reputation: 889

The easiest way via PHP would look like:

<input type="text" name="name" placeholder="First Name" value="<?php echo ( isset( $_POST['name'] ) ? htmlspecialchars($_POST['name']) : '' ); ?>">

This code checks if POST[name] has been send to the server and if so, it simply sets the content as value for the input.
For the other inputs you have to replace $_POST[name] with e.g. $_POST[email].

Also I would set the input type to email, for the e-mail-address.

Upvotes: 1

Related Questions