Email Form with required fields reset's itself

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
antoniomb
Forum Newbie
Posts: 8
Joined: Thu Aug 16, 2007 7:29 pm

Email Form with required fields reset's itself

Post by antoniomb »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi, I'm new to PHP and i've found this script  off another website which works great for me, but i would like to know how do i change it so that when a required field is left empty, and the user has to go back and refill it in, all the fields are not reset. For instance, so if someones typed a long message, then forget to put their email address in, when submitted, it is not cleared and they don't have to type the whole long message again. Any help would be much appreciated. thank you.

Code: Select all

<?php
if (($_POST) and (!empty($_POST['name'])) and (!empty($_POST['email'])) and (!empty($_POST['message']))) {
if (get_magic_quotes_gpc()) {
foreach ($_POST as $key => $value) {
$temp = stripslashes($value);
$_POST[$key] = $temp;
}
}
$to = 'info@domain.com';
$subject = 'formsubject';

// message goes here
$message = "Full Name: " . $_POST['name'] . "\n";
$message .= "Email: " . $_POST['email'] . "\n";
$message .= "Telephone: " . $_POST['phone'] . "\n";
$message .= "Message: " . $_POST['message'] . "\n";

// headers go here
$headers = "From: " . $_POST['email'] . "\n";
$headers .= "Content-type: text/plain; charset=UTF-8";

//send mail
$sent = mail($to, $subject, $message, $headers);
$autorespond = mail($_POST['email'], "Form Auto Response.", "Thank you for contacting me, your message has been received and we will contact you shortly.", "From: Me <info@domain.com>");
}
?>

<!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" lang="en">

<head>
<title>Title</title>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>

<h1>Contact</h1>

<?php
if (isset($sent)) {
echo '<p><span class="thanks"><strong>Thank you</strong> for contacting us. Your message will be responded to as soon as possible.</span></p>';
}
else if ((empty($_POST['name'])) or (empty($_POST['email'])) or (empty($_POST['message']))) {
echo '<p>Please ensure you have filled out all the required fields</p>';
}
?>

<div id="form">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<fieldset>
<legend>Personal information</legend>

<span class="formLabel"><label for="name">Full Name:</label><br /></span>
<input class="inputfield" type="text" id="name" name="name" size="30" />

<span class="formLabel"><label for="email">Email:</label><br /></span>
<input class="inputfield" type="text" id="email" name="email" size="30" />

<p><span class="formMessage"><label for="message">Message:</label><br /></span>
<textarea class="inputarea" id="message" name="message" rows="5" cols="30"></textarea></p>

</fieldset>

<p><input type="submit" class="inputsend" name="Submit" value="Send Form" /></p>

</form></div><!-- end #form -->
</body>
</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Code: Select all

<input class="inputfield" type="text" id="name" name="name" size="30" value="<?php echo $_POST['name'] ?>" />
and so on for the other fields
antoniomb
Forum Newbie
Posts: 8
Joined: Thu Aug 16, 2007 7:29 pm

Post by antoniomb »

Thank you very much, that worked a treat. Now if i could ask another question, how do i reset the form when it has finally been submitted and disable the back browser button so the information submitted cannot be retrieved? This is probably a simple one, but i'm an absolute newbie, and we all have to learn somewhere!
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

antoniomb wrote:Thank you very much, that worked a treat. Now if i could ask another question, how do i reset the form when it has finally been submitted and disable the back browser button so the information submitted cannot be retrieved? This is probably a simple one, but i'm an absolute newbie, and we all have to learn somewhere!
Redirect to the same page, but without the POST information.

Code: Select all

header('Location: http://domain.tld/emailForm.php');
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Or better header('Location: http://domain.tld/emailForm.php?thankyou');

and somewhere in the page

Code: Select all

if(isset($_GET['thankyou'])) echo 'Thank you bla bla'; else {

 // the form
}
antoniomb
Forum Newbie
Posts: 8
Joined: Thu Aug 16, 2007 7:29 pm

Post by antoniomb »

Thank you very much for your replies, but i'm having a little trouble with this. Absolute beginner rather than newbie would suit me better! Could any one please instruct me where to write the header and the code in the original script i posted, i.e between which lines? Thanks again in advance.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

antoniomb wrote:Thank you very much for your replies, but i'm having a little trouble with this. Absolute beginner rather than newbie would suit me better! Could any one please instruct me where to write the header and the code in the original script i posted, i.e between which lines? Thanks again in advance.
After successfully posting and before any output.
Post Reply