Page 1 of 1

Two quick questions about links

Posted: Tue May 29, 2007 1:48 pm
by toby_c500
Hi, two quick questions that you might be able to help me with:

1. I have a register form that checks to see if the user has input the password the same twice. (Password...Re-type password). I have put a link if the details are not correct, to go back to the html form. How do I have a link to go back to the original form that the user has filled out with there details on? It goes back to a blank page.


2. Is related to above. I want my users to updated there profiles. How do I input the users original data in an html form so that they can update only the information they want?

Thanks. Hope you can help.

Posted: Tue May 29, 2007 1:58 pm
by superdezign
Check if the passwords match. If they don't, then create a variable like.. $theyDontMatch. Then, have a check somewhere where it says:

Code: Select all

if(isset($theyDontMatch)){ echo "The entered passwords do not match."; }

Posted: Tue May 29, 2007 2:13 pm
by toby_c500
Hi superdezign,

I have that bit sorted. What I need is to go back to the form with the users data in it. As in, before they click the submit. How do I link back to that filled in form?

Posted: Tue May 29, 2007 5:08 pm
by RobertGonzalez
The easiest thing to do is keep the users information by posting back to the same page. Otherwise you will need to use cookies or sessions to refill the form with the information the user submitted already.

Posted: Tue May 29, 2007 5:24 pm
by s.dot
A general page setup for what you're trying to acheive. I generally use a $showForm control variable.

Code: Select all

$showForm = true;

if ($formWasSubmitted) {
   $showForm = false;

   // process the form
   if ($thereIsErrorInFormDetails) {
      $showForm = true; //show the form again with values filled in
      //show errors here or later on in the form
   }
}

if ($showForm === true) {
   //show form here
}
For this to work, the form must be submitted to the same page it is shown on.

Posted: Tue May 29, 2007 5:35 pm
by RobertGonzalez
Generally speaking, I do the same thing.

Code: Select all

<?php
// Set defaults
$show_form = true;
$error = '';
$username = '';

// Check if the form was posted
if (!empty($_POST['username']))
{
    // assign the post value for checking
    $username = $_POST['username'];

    // Check if it meets our criteria
    if (!ctype_alnum($username))
    {
        // Set the error var
        $error = 'The username field must be alphanumeric only.';
    }
    else
    {
        $show_form = false;
    }
}

if ($show_form): if (!empty($error)):
?>
<div style="background: #ccc; color: #c00; border: 1px solid #c00;"><?php echo $error; ?></div>
<?php endif; ?>
<form method="post" action="<?php echo basename(__FILE__); ?>">
<p>Please enter an alphanumeric user name:  
<input type="text" name="username" value="<?php echo $username; ?>" /></p>
<p><input type="submit" name="submit" value="Submit Me" /></p>
</form>
<?php endif; ?>

Posted: Wed May 30, 2007 11:27 am
by toby_c500
Awesome, thanks guys. I will give those ideas a go. Thanks for help.