How to create form that never empty after submitted?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

exphp
New php-forum User
New php-forum User
Posts: 10
Joined: Thu Nov 10, 2011 11:15 pm

How to create form that never empty after submitted?

Postby exphp » Sun Aug 25, 2013 12:45 am

I have a form of html and php. After submitted the value of the form, the value of form that I was typed before is gone. Then, how to make the value is never gone?
Here my sample form:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Validate an Email</title>
</head>

<body>
<center>
<form action="" method="POST">
Enter email: <input type="text" name="email" size="30" />
<input type="submit" name="check" value="Check Email" />
</form>

<?php

$email = $_POST['email'];
if(isset($_POST['check']))
{
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
echo "Email is not valid <br />";
}
else
{
echo "Email is valid. <br/>";
}
}

?>

</center>
</body>
</html>

Form before and after enter email:
form1.png
form1.png (24.95 KiB) Viewed 438 times

After form submitted:
form3.png
form3.png (2.8 KiB) Viewed 438 times

Look, the email value is gone, right?
But, what I need to do is look like this:
form4.png
form4.png (2.6 KiB) Viewed 438 times

I mean, the value of text is never be gone after submitted. Anyone knows how to do that?

Thanks.

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: How to create form that never empty after submitted?

Postby johnj » Sun Aug 25, 2013 7:25 pm

One way will be - you can save the values to a session variable and then repopulate the form after submission.

exphp
New php-forum User
New php-forum User
Posts: 10
Joined: Thu Nov 10, 2011 11:15 pm

Re: How to create form that never empty after submitted?

Postby exphp » Mon Aug 26, 2013 7:07 am

johnj wrote:One way will be - you can save the values to a session variable and then repopulate the form after submission.

Would you like to show me a simple example, please?
:)

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: How to create form that never empty after submitted?

Postby johnj » Mon Aug 26, 2013 9:32 pm

if the fields are name and email, then save it to session variables before submit like this

$_SESSION['name'] = $name;
$_SESSION['email'] = $email;

and after submitting, use a flag to check if the form was submitted and then populate the fields,like
if ($submitted === true)
{
$name = $_SESSION['name'];
$email = $_SESSION['email'];
}
<input name='name' value = '<?php echo $name; ?>' />
<input name='email' value = '<?php echo $email; ?>' />


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron