Jump to content

Archived

This topic is now archived and is closed to further replies.

bladechob

I'm being really dim here but can someone advise the code for form memory

Recommended Posts

I need to get a submit form to 'remember the registration details so that if a visitor is filling in a form and misses a required field or puts in a malformed e-mail address that when they return to the form it has retained their earlier inputted stuff i.e. they don't have to go through the process of re-entering stuff all over again.

Thanks :-*

Share this post


Link to post
Share on other sites
When the form is first submitted to store any inputed data into the $_SESSION array.

Share this post


Link to post
Share on other sites
if you mean for form validation then what you need to do is start the forms with values that are null and ERROR=FALSE. then after a form is submitted run through it and check for errors then return ie ERROR=TRUE if there is an error. Here you will get the values from your post information and then set the values in the form as these.

Share this post


Link to post
Share on other sites
My first post -- yay! :)

It sounds like you want a sticky form, a kind of form that will alert the user that they did something wrong (like not filling in a form element) upon submission, without bringing them to an error page.  I made a simple one in my own attempts at learning PHP that seems to work well.  Of course, you'll need to modify it for your specific purpose, but it's simple enough to modify easily.  I recommend using regular expressions in the test statements, though.  I was using JavaScript to do it as a test exercise, but that's not a secure method of validation.  The files I include in my code (header.inc and footer.inc) are just basic XHTML.  They only contain the header info (like page title, any CSS, that sort of thing) and the closing tags for the footer (</body> and </html>).  Hope this helps!

[code]<?php

$page_title = 'Sticky Form Test';
include('header.inc');

$message = NULL;

if(isset($_POST['submit'])){
  if(empty($_POST['username'])){
      $u = FALSE;
      $message .= "Please input your username!<br />";
  }

  else {
      $u = $_POST['username'];
  }

  if(empty($_POST['password'])){
      $p = FALSE;
      $message .= "Please input your password!<br />";
  }

  else {
      $p = $_POST['password'];
  }

  if(empty($_POST['email'])){
      $e = FALSE;
      $message .= "Please input your e-mail address!";
  }

  else {
      $e = $_POST['email'];
  }

  if($u && $p && $e){
      echo "You correctly submitted everything!";
  }
}

?>

<form id="inputform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <fieldset class="narrow"><legend>Please input your information</legend>
      <p><label for="username">Name:</label><input name="username" id="username" type="text" class="txt" value="<?php if(isset($_POST['username'])) echo $_POST['username']; ?>" /></p>
      <p><label for="password">Password:</label><input name="password" id="password" type="password" class="txt" /></p>
      <p><label for="email">E-mail Address:</label><input name="email" id="email" type="text" class="txt" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>"/></p>
      <p><input type="submit" name="submit" id="submit" value="Submit" /></p>
  </fieldset>
</form>

<?php
if(isset($message)){
  echo "<div style='color: #FF0000;'>$message</div>";
}

include('footer.inc');
?>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.