Forum Moderators: coopster

Message Too Old, No Replies

Keep form data after error message?

         

tonynoriega

8:45 pm on Nov 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This script is simple and has served me well over the past years...

problem i find is that when the echo error is displayed, the form data is lost...

how can i keep the form data in tact, and echo the error message?

<?php 

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

if (!$_POST['name'] | !$_POST['email'])
{
echo"<div class='error'>Error<br />You did not fill in a required field, please review your form and correct the missing information.</div>";
}
else
{
$name = $_POST['name'];
$email = $_POST['email'];
$email2 = $_POST['email2'];
$legal = $_POST['legal'];
$legal2 = $_POST['legal2'];
$address = $_POST['address'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];



$yoursite = "See Me Shine Models";
$youremail = $email;

$subject = "Website Model Form";
$message = "$name would like you to contact them about See Me Shine Models.
Contact PH: $phone
Email: $email
Email2: $email2
Legal: $legal
Legal2: $legal2
Address: $address
Address2: $address2
City: $city
State: $state
Zip: $zip
Phone: $phone
Comments: $comments";

$email3 = "myaddress@me.com";

mail($email3, $subject, $message, "From: $email");

echo"<div class='thankyou'>Thank you for contacting us,<br /> we will respond as soon as we can.</div>";

}
}
?>

enigma1

8:55 pm on Nov 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



just remove the else clause and leave the if in place

//else
//{
and the closing bracket.

PS: Actually you're sending the mail further down so you need to check for the error before sending the mail too.

if ($_POST['name'] && $_POST['email']) {
mail($email3, $subject, $message, "From: $email");

echo"<div class='thankyou'>Thank you for contacting us,<br /> we will respond as soon as we can.</div>";
}

tonynoriega

9:45 pm on Nov 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So essentially i would have 3 if statements....

does that sound right?

enigma1

9:51 pm on Nov 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you need to add another conditional because you don't want to send an email on failure. Yes 3 if statements 2 inside the first one.

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

tonynoriega

9:54 pm on Nov 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php 

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

if (!$_POST['name'] | !$_POST['email'])
{
echo"<div class='error'>Error<br />You did not fill in a required field, please review your form and correct the missing information.</div>";
}

$name = $_POST['name'];
$email = $_POST['email'];
$email2 = $_POST['email2'];
$legal = $_POST['legal'];
$legal2 = $_POST['legal2'];
$address = $_POST['address'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];



$yoursite = "See Me Shine Models";
$youremail = $email;

$subject = "Website Model Form";
$message = "$name would like you to contact them about See Me Shine Models.
Contact PH: $phone
Email: $email
Email2: $email2
Legal: $legal
Legal2: $legal2
Address: $address
Address2: $address2
City: $city
State: $state
Zip: $zip
Phone: $phone
Comments: $comments";

$email3 = "myaddress@me.com";

if ($_POST['name'] && $_POST['email']) {
mail($email3, $subject, $message, "From: $email");

echo"<div class='thankyou'>Thank you for contacting us,<br /> we will respond as soon as we can.</div>";
}
}
?>


as such?

tonynoriega

9:57 pm on Nov 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I just tested this, although no errors, when the error message is displayed, and i close it, the data is not in the previously populated fields.

do i need to set the $_Session variable?

enigma1

10:32 pm on Nov 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



when you say "I close it" what do you mean, reload the page?

for a simple form like this you shouldn't need to setup sessions.

tonynoriega

10:43 pm on Nov 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



i have a jquery .hide() function attached to the echo <div> so that the user can close it...

it functions like a pop up.. and the user can close it...


<a class="close" href="#">close</a>

BUT even if i dont close it, i can see the rest of the form behind it, and the fields are once again empty...

so it seems to be happening when the page re-loads...

which it is re-loading..i can actually see the page refresh... why? i dont know...

could it be the way my form is processing?

<form id="contact_us" method="post" action="index.php">

enigma1

11:01 pm on Nov 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



what the handler for the close link inside the jquery does? Make sure it only closes the popup and returns false. Otherwise the click will propagate ie: do the default which is to follow the link (like when you click a link).

see when you disable js does it work after submitting the form?

PS: Can you also change this code


if (!$_POST['name'] | !$_POST['email'])

to this


if (!$_POST['name'] || !$_POST['email'])

enigma1

11:08 pm on Nov 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually what I posted earlier is incorrect. Is not the problem

Basically you want to check for empty fields so its

if ( empty($_POST['name']) || empty($_POST['email']) )

rocknbil

3:53 pm on Nov 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



how can i keep the form data in tact, and echo the error message?


A skeleton of one decent approach. Needs quite a bit more filtering to avoid data injection, but it's a start.


if (isset($_POST['submit'])) {
if (empty($_POST['email']) or empty($_POST['name'])) {
// echo out the ENTIRE FORM here, with the error and form fields intact
$errors = '<p class="error">Error<br />You did not fill in a required
field, please review your form and correct the missing information.</p>';
echo output_form($errors);
exit; // this line eliminates need for "else"
}
// do mail functions
}
else {
// echo out entire form here
echo output_form();
// End of script, no exit needed
}
//
// then the form function, so you don't have to code it twice
//
function output_form($errors=null) {
$form = null; // To squelch undefined variable errors
if ($errors) { $form .= $errors; }
$form .='<form action...... (etc.)';
return $form;
}


Since $_POST is a superglobal, you shouldn't have to declare any global variables within the function.