Jump to content


Photo

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


  • Please log in to reply
4 replies to this topic

#1 bladechob

bladechob
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationDerby, UK

Posted 26 September 2006 - 02:10 PM

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 :-*
To forgive is to forget... to forget is... what was I saying?

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 26 September 2006 - 02:12 PM

When the form is first submitted to store any inputed data into the $_SESSION array.

#3 steveclondon

steveclondon
  • Members
  • PipPipPip
  • Advanced Member
  • 161 posts

Posted 26 September 2006 - 02:15 PM

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.

#4 KevinM1

KevinM1
  • Moderators
  • Snarkimus Prime
  • 5,248 posts

Posted 26 September 2006 - 03:02 PM

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!

<?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');
?>

Using 'global' is a sign of doing it wrong

#5 bladechob

bladechob
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationDerby, UK

Posted 27 September 2006 - 01:07 PM

Thanks, that'll do nicely - congrats on your first post matey
To forgive is to forget... to forget is... what was I saying?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users