Two quick questions about links

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
toby_c500
Forum Commoner
Posts: 50
Joined: Fri May 11, 2007 11:29 am
Location: Leeds, England

Two quick questions about links

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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."; }
toby_c500
Forum Commoner
Posts: 50
Joined: Fri May 11, 2007 11:29 am
Location: Leeds, England

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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; ?>
toby_c500
Forum Commoner
Posts: 50
Joined: Fri May 11, 2007 11:29 am
Location: Leeds, England

Post by toby_c500 »

Awesome, thanks guys. I will give those ideas a go. Thanks for help.
Post Reply