Jump to content

Archived

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

Guteman

User Registration Issues

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Pretty much your form field should look like this:

[code]

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'];?>" />

[/code]

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..

Share this post


Link to post
Share on other sites
When you receive the values via GET or POST, store them in a session variable, say an array.  

[code]
//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;
[/code]

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

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

[code]
//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');
[/code]

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:
[code]
$_SESSION['sent_values']=array();
unset($_SESSION['sent_values']);
[/code]

Share this post


Link to post
Share on other sites
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.

[code]
<?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>

[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

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.