lucy Posted August 13, 2009 Share Posted August 13, 2009 I am writing an email form with php validation. When a user does not enter some data into a field, and licks submit, the form returns itself, blank, with an error message at the top of the page. How can i get it so any information which was added into text fields before submit was clicked, comes back into the same text fields after the form has been submitted and found to be fields without any text in. Hope ive explained it well enough for people to understand. Code (contact.php): <?php $name = $_REQUEST['name'] ; $email = $_REQUEST['email'] ; $subject = $_REQUEST['subject'] ; $message = $_REQUEST['message'] ; $finalmessage = "This email has been sent to you from the online contact form.\n\nName: $name\nEmail: $email\nSubject: $subject\n\nMessage: ". $message; if (!isset($_REQUEST['email'])) { echo""; } elseif (empty($email) || empty($subject) || empty($message) || empty($name)) { echo "Please enter your email and message!"; } else { mail( "EMAIL", "Feedback Form Results - '$subject'", $finalmessage, "From: $email\nName:$name\n" ); header( "Location: http://www.google.com" ); } ?> <form method="post" action="contact2.php"> Name: <input type="text" name="name" id="name"> <br /> <p>Email: <input type="text" name="email" /> </p> <p>Subject: <input type="text" name="subject" id="subject"> </p> <p><br /> Message:<br /> <textarea name="message" rows="15" cols="40"> </textarea> <br /> <input type="submit" /> </p> </form> </body> </html> Thanks for any suggestions Lucy Quote Link to comment https://forums.phpfreaks.com/topic/170082-remeber-data-from-text-field/ Share on other sites More sharing options...
.josh Posted August 13, 2009 Share Posted August 13, 2009 <input type="text" name="name" id="name" value="<?php echo $_POST['name']; ?>"> etc... Quote Link to comment https://forums.phpfreaks.com/topic/170082-remeber-data-from-text-field/#findComment-897224 Share on other sites More sharing options...
lucy Posted August 13, 2009 Author Share Posted August 13, 2009 Thanks i keep getting an error message, i think it is to do with the ' marks and " marks used. I cant figure out how to fix it. Error message is: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/sites/****.co.uk/public_html/contact2.php on line 40 Ive edited my code a little, and also tried putting the code you gave me into different speach marks but it does not seem to be working. full code: <?php $name = $_REQUEST['name'] ; $email = $_REQUEST['email'] ; $subject = $_REQUEST['subject'] ; $message = $_REQUEST['message'] ; $finalmessage = "This email has been sent to you from the online contact form at ****.co.uk.\n\nName: $name\nEmail: $email\nSubject: $subject\n\nMessage: ". $message; if (!isset($_REQUEST['email'])) { echo "<html> <body> <form method='post' action='contact2.php'> Name: <input type='text' name='name' id='name'> <br /> <p>Email: <input type='text' name='email'/> </p> <p>Subject: <input type='text' name='subject' id='subject' > </p> <p><br /> Message:<br /> <textarea name='message' rows='15' cols='40'> </textarea> <br /> <input type='submit'/> </p> </form> </body> </html>"; } elseif (empty($email) || empty($subject) || empty($message) || empty($name)) { echo "<b>Please complete all fields!</b>"; echo "<html> <body> <form method='post' action='contact2.php'> Name: <input type='text' name='name' id='name' value='<?php echo $_POST['name']; ?>'> <br /> <p>Email: <input type='text' name='email' /> </p> <p>Subject: <input type='text' name='subject' id='subject'> </p> <p><br /> Message:<br /> <textarea name='message' rows='15' cols='40'> </textarea> <br /> <input type='submit'/> </p> </form> </body> </html>"; } else { mail( "EMAIL", "Feedback Form Results - '$subject'", $finalmessage, "From: $email\nName:$name\n" ); header( "Location: http://www.google.com" ); } ?> Line 40, is about half way down, and contains the code <input type='text' name='name' id='name' value='<?php echo $_POST['name']; ?>'> which keeps spitting out the error message, and the line which contains the code which you jsut gave me, with the speach marks swapped, since its all in an echo statement. Can you suggest why its spitting out the error? Thanks, Lucy Quote Link to comment https://forums.phpfreaks.com/topic/170082-remeber-data-from-text-field/#findComment-897241 Share on other sites More sharing options...
.josh Posted August 13, 2009 Share Posted August 13, 2009 the example I gave you was showing the input tag inside the normal html scope. That's why I have opening and closing tags around the value. In the code you just posted, you are within php scope, and you're already using echo, so you need to remove the php tags and echo. <input type='text' name='name' id='name' value='{$_POST['name']}'> Quote Link to comment https://forums.phpfreaks.com/topic/170082-remeber-data-from-text-field/#findComment-897248 Share on other sites More sharing options...
lucy Posted August 13, 2009 Author Share Posted August 13, 2009 Works like a dream. Thanks :D Quote Link to comment https://forums.phpfreaks.com/topic/170082-remeber-data-from-text-field/#findComment-897249 Share on other sites More sharing options...
lucy Posted August 13, 2009 Author Share Posted August 13, 2009 So far, i have it so it highlights the field which is not filled in, i.e. if name is not filled in when submit is clicked, it brings the form back with the data and highlights the name field. but if more than one field are not filled in, it only highlights the top one, and then if the second one is not filled in, after submit is clicked, it will bring the form back and then highlight the second. Is there any way in which when submitted is clicked, that all fields with no text in are highlighted? fully working code: <?php $name = $_REQUEST['name'] ; $email = $_REQUEST['email'] ; $subject = $_REQUEST['subject'] ; $message = $_REQUEST['message'] ; $finalmessage = "This email has been sent to you from the online contact form\n\nName: $name\nEmail: $email\nSubject: $subject\n\nMessage: ". $message; if (!isset($_REQUEST['email'])) { echo "<html> <body> <form method='post' action='contact2.php'> Name: <input type='text' name='name' id='name'> <br /> <p>Email: <input type='text' name='email'/> </p> <p>Subject: <input type='text' name='subject' id='subject' > </p> <p><br /> Message:<br /> <textarea name='message' rows='15' cols='40'> </textarea> <br /> <input type='submit'/> </p> </form> </body> </html>"; } elseif (empty($name)) { echo "<b>Please complete all fields!</b>"; echo "<html> <body> <form method='post' action='contact2.php'> <p style='color:red'>Name: <input type='text' name='name' id='name' value='{$_POST['name']}'> </p> <p>Email: <input type='text' name='email' value='{$_POST['email']}'/> </p> <p>Subject: <input type='text' name='subject' id='subject' value='{$_POST['subject']}'> </p> <p> Message:<br /> <textarea name='message' rows='15' cols='40' value='{$_POST['message']}'> </textarea> <br /> <input type='submit'/> </p> </form> </body> </html>"; } elseif (empty($email)) { echo "<b>Please complete all fields!</b>"; echo "<html> <body> <form method='post' action='contact2.php'> <p>Name: <input type='text' name='name' id='name' value='{$_POST['name']}'> </p> <p style='color:red'>Email: <input type='text' name='email' value='{$_POST['email']}'/> </p> <p>Subject: <input type='text' name='subject' id='subject' value='{$_POST['subject']}'> </p> <p> Message:<br /> <textarea name='message' rows='15' cols='40' value='{$_POST['message']}'> </textarea> <br /> <input type='submit'/> </p> </form> </body> </html>"; } elseif (empty($subject)) { echo "<b>Please complete all fields!</b>"; echo "<html> <body> <form method='post' action='contact2.php'> <p>Name: <input type='text' name='name' id='name' value='{$_POST['name']}'> </p> <p>Email: <input type='text' name='email' value='{$_POST['email']}'/> </p> <p style='color:red'>Subject: <input type='text' name='subject' id='subject' value='{$_POST['subject']}'> </p> <p> Message:<br /> <textarea name='message' rows='15' cols='40' value='{$_POST['message']}'> </textarea> <br /> <input type='submit'/> </p> </form> </body> </html>"; } elseif (empty($message)) { echo "<b>Please complete all fields!</b>"; echo "<html> <body> <form method='post' action='contact2.php'> <p>Name: <input type='text' name='name' id='name' value='{$_POST['name']}'> </p> <p>Email: <input type='text' name='email' value='{$_POST['email']}'/> </p> <p>Subject: <input type='text' name='subject' id='subject' value='{$_POST['subject']}'> </p> <p style='color:red'> Message:<br /> <textarea name='message' rows='15' cols='40' value='{$_POST['message']}'> </textarea> <br /> <input type='submit'/> </p> </form> </body> </html>"; } else { mail( "EMAIL", "Feedback Form Results - '$subject'", $finalmessage, "From: $email\nName:$name\n" ); header( "Location: http://www.google.com" ); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/170082-remeber-data-from-text-field/#findComment-897264 Share on other sites More sharing options...
lucy Posted August 16, 2009 Author Share Posted August 16, 2009 Can someone suggest how this can be done please? Thanks, Lucy Quote Link to comment https://forums.phpfreaks.com/topic/170082-remeber-data-from-text-field/#findComment-899493 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.