how to preserve form content?

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
sunhede
Forum Newbie
Posts: 2
Joined: Wed May 16, 2007 6:47 pm

how to preserve form content?

Post by sunhede »

I have an e-mail form (form.php) employing Swift, based upon the tutorial in the Swift documentation:
http://www.swiftmailer.org/wikidocs/v3/ ... /form2mail
Before sending the mail, I first check the form was filled in correctly. However, regardless of the outcome, the user is always sent back to the form via "form.php?status=OUTCOME", which then displays an appropriate message:

Code: Select all

if (!empty($_GET["status"]))
{
  switch ($_GET["status"])
  {
    case "message_sent": ?>
      <em style="color: #FFF;">Thank you for your message, we will soon get back to you</em><br><br><?php
      break;
    case "not_enough_info": ?>
      <em style="color: #FFF;">** Please complete all fields **</em><br><br><?php
      break;
    case "invalid_email": ?>
      <em style="color: #FFF;">** Please provide a valid e-mail address **</em><br><br><?php
      break;
    case "sending_failed": ?>
      <em style="color: #FFF;">** Temporary problem, please try again later **</em><br><br><?php
      break;
  }
}

Code: Select all

<form action="contact/send.php" method="POST" enctype="multipart/form-data">
  <b>Name</b><br>
  <input type="text" name="sender_name" size=40><br>
  <b>e-mail</b><br>
  <input type="text" name="sender_email" size=40><br>
  <b>Subject</b><br>
  <input type="text" name="mail_subject" size=40><br>
  <b>Message</b><br>
  <textarea cols=40 rows=10 name="mail_body"></textarea><br>
  <input type="submit" value=" Send "><br>
</form>
This works fine, except for the fact that the form will of course always be emptied so that the user will have to start all over again. How do I make sure the filled in content will remain, except for the case when the message has been sent?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

echo '<input type="text" name="sender_name" size=40 value="'. (!empty($_POST['sender_name']) ? htmlspecialchars($_POST['sender_name']) : '').'"><br>';
Basically, just check if there is a column for the field, if so output it in the value attribute. Make sure you use htmlspecialchars() to avoid XSS injection though.
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

you can either change the page from using GET to POST... that way the browser sees it as the same page...

or you can pass the the variables on again.. ie:

Code: Select all

<input type="text" value="<?php echo $_POST['username']; ?>" />
...thats assuming you arent forwarding people onto that page (as in header('Location: foo.php"); )... if thats the case, you could save the variables to session variables, then echo them out.
sunhede
Forum Newbie
Posts: 2
Joined: Wed May 16, 2007 6:47 pm

Thank you!

Post by sunhede »

I am indeed forwarding people to the page via header(), so I created a session variable for each field and echoed it as in the example above. Worked like a charm
Post Reply