Alexis Guerra Alexis Guerra - 4 months ago 14
PHP Question

How to keep entered values in PHP so users don't have to type them again after hitting the submit button?

Let's say we have a form with several input fields such as first name, last name, phone number, the message, and all of these have validation. We enter all but the first name because we forgot. We hit submit to send the data, but we're returned with an error because we forgot to enter our first name. All information entered is still present so we don't have to type it again.

How can I go on about doing this? The code below was taken from this website.

<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Demo Contact Form';
$to = 'myemail@mail.com';
$subject = 'Message from Contact Demo ';

$body ="From: $name\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}

// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}

//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
}
?>

Answer

You can use Session to store information and display value in form like

PHP

<?php

  session_start();

   $form_name = isset($_SESSION['name']) ? $_SESSION['name'] : '';
   $form_email= isset($_SESSION['email']) ? $_SESSION['email'] : '';
   $form_message= isset($_SESSION['message']) ? $_SESSION['message'] : '';
   $form_human = isset($_SESSION['human ']) ? $_SESSION['human'] : '';

    if (isset($_POST["submit"])) {

        //store information into session

        $_SESSION["name"]=$_POST['name'];
        $_SESSION["email"]=$_POST['email'];
        $_SESSION["message"]=$_POST['message'];
        $_SESSION["human"]=intval($_POST['human']);

        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $human = intval($_POST['human']);
        $from = 'Demo Contact Form'; 
        $to = 'myemail@mail.com'; 
        $subject = 'Message from Contact Demo ';

    .....
     ....
if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch</div>';

      //remove information from session
      unset($_SESSION["name"]);
      unset($_SESSION["email"]);
      unset($_SESSION["message"]);
      unset($_SESSION["human "]);

    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
    }

HTML

<input type="text" value="<?php echo $form_name; ?>" name="name">
-----
-----