Jump to content


Photo

User Registration Issues


  • Please log in to reply
5 replies to this topic

#1 Guteman

Guteman
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts
  • LocationWisconsin

Posted 03 August 2006 - 04:33 PM

Hey All,

Im doing my first PHP project and I have hit a screaming halt with an annoying issue. Im creating a client registration system for my web development company. My problem is, I have if statements used to if for example "client name" is left blank, It sends an error message back saying "You did not enter a Client First Name, go back and try again." When I click back, it clears the form in IE, but not in firefox. What has to be done so it will work in IE?

Thanks ahead of time.

Paul G.

#2 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 03 August 2006 - 04:45 PM

Pretty much your form field should look like this:


Name: <input type="text" size="20" name="cName" value="<?php if (isset($_POST['cName'])) echo $_POST['cName']; ?>" />
Email: <input type="text" size="20" name="cEmail" value="<?php if(isset($_POST['cEmail'])) echo $_POST['cEmail'];?>" />


If it works in Firefox and not in IE, might be a silly HTML error. Firefox seems to be smart enough to fix them on the fly, and IE isnt... or thats my experience anyway..


#3 bltesar

bltesar
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 03 August 2006 - 04:49 PM

When you receive the values via GET or POST, store them in a session variable, say an array.  

//get all your POSTed information and store it in a session variable(an array)
foreach($_POST as $key=>$value)
{
      $sent_values[$key]=$value;
}
$_SESSION['sent_values']=$sent_values;

then if invalid data is received, you can send the client back automatically using
header('Location:http://mydomain.com/form_page.php?error=name');    //redirects back to form page
exit();   //this must-have prevents subsequent code from running

on your form page, get the sent_values session variable and load your form inputs with the values

//check for existence of session variable holding sent values
if(isset($_SESSION['sent_values']))
{
     $sent_values=$_SESSION['sent_values'];
     //create variables from the array populated with the sent values
     foreach($sent_values as $key=>$value)
     {
          $$key=$value;
     }
}

//you can use the querystring appended at the end of the URL to print the error message
if($_GET['error']=='name'){echo 'name is missing');

Then your form input elements look like this:
<input type="text" name="input_1" value="<?PHP echo $input_1;?>">

you might want to destroy the SESSION variable so that the form clears on reload:
$_SESSION['sent_values']=array();
unset($_SESSION['sent_values']);



#4 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 03 August 2006 - 05:33 PM

Why store them in a session when they are already stored in the $_POST[]?
This is how I do it when I make forms and want them to echo the fields that have already been filled in so they don't have to do it again.

<?php

if(isset($_POST['submit'])) {
   $errMsg = "";    // String to store error messages to be displayed later...
   // Form has been submitted use $_POST[] to get the values
   if(!empty($_POST['cName'])) {
       // Name was not left blank
       $name = $_POST['cName'];
    } else {
        $errMsg .= "<li>You cannot leave the Name field blank.</li\n";
    }
    if($errMsg == "") {
       // Do your think there
    } else {
       // There was a submission error
       echo "There have been errors with your submission, they are:  <br />
                 <ul>{$errMsg}</ul>\n";
    }

     

}
// Display Form and check to see if the $_POST[] variables are already set

?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
Name <input type="text" name="cName" value="<?php if(isset($_POST['cName'])) echo $_POST['cName'];?>" />
<input type="submit" value="Submit" name="submit" />
</form>



#5 bltesar

bltesar
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 03 August 2006 - 05:38 PM

the SESSION variable is only needed if the form and validation are done on different pages.  I assumed that to be the case because the original question indicated that invalid data would give a 'go back' method. 

I sometimes use a hidden validation page that redirects the user depnding on success/failure of input.  That way, data cannot be inadvertantly resubmitted by using the back button.

#6 Guteman

Guteman
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts
  • LocationWisconsin

Posted 03 August 2006 - 05:42 PM

well both of you are right. I like the idea of using sessions, but then again $_POST already has it stored. I originally had this on 2 seperate pages, which would need sessions. I find it easier (since im no PHP guru) to just use the $_POST in the value and make it one page.

Thanks guys




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users