Jump to content

Archived

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

rstrik

Form input errors help

Recommended Posts

Hello,

I was curious to see if maybe one of you may be able to help me with an issue I'm having.
I am creating a membership signup form, data is entered into the form and then it is inserted into MySQL tables after all error checks have been completed. I am using WAMP5 1.6.1.

Problem: The fields are displaying the $_POST php code instead of a blank field in the form. It is suppose to allow users to enter the information and if an error is encountered with their information it will allow them to click the back button in their browser and their info that was entered should remain in the form fields. But instead it is displaying "<?=$_POST['first_name'];?>" , "<?=$_POST['last_name'];?>" and etc for each field.

Here is my Html form
[code]
<p><font size="4" face="Verdana, Arial, Helvetica, sans-serif"><strong>Become
  a Member!</strong></font></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Join our website
  and enjoy the benefits of becoming a member!</font></p>

<?php
if($errors){
echo "<p align=\"center\"><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#FF0000\">$errors</font></p>\n";
}
?>

<div align="center"></div>
<form method="post" action="/join.php">
  <table width="50%" border="1" align="center" cellpadding="4" cellspacing="0">
    <tr>
      <td width="200" align="left" valign="top" nowrap><font size="2" face="Verdana, Arial, Helvetica, sans-serif">First
        Name</font></td>
      <td width="179" align="left" valign="top"><input name="first_name" type="text" id="first_name" value="<?=$_POST['first_name'];?>"></td>
    </tr>
    <tr>
      <td width="200" align="left" valign="top" nowrap><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Last
        Name</font></td>
      <td align="left" valign="top"><input name="last_name" type="text" id="last_name" value="<?=$_POST['last_name'];?>"></td>
    </tr>
    <tr>
      <td width="200" align="left" valign="top" nowrap><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Email Address</font></td>
      <td align="left" valign="top"><input name="email_address" type="text" id="email_address" value="<?=$_POST['email_address'];?>"></td>
    </tr>
    <tr>
      <td align="left" valign="top" nowrap><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Verify Email Address</font></td>
      <td align="left" valign="top"><input name="email_address2" type="text" id="email_address3" value="<?=$_POST['email_address2'];?>"></td>
    </tr>
    <tr>
      <td width="200" align="left" valign="top" nowrap><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Desired
        Username</font></td>
      <td align="left" valign="top"><input name="username" type="text" id="username" value="<?=$_POST['username'];?>"></td>
    </tr>
    <tr>
      <td width="200" align="left" valign="top" nowrap><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Password</font></td>
      <td align="left" valign="top"><input name="password" type="password" id="password" value="<?=$_POST['password'];?>"></td>
    </tr>
    <tr>
      <td width="200" align="left" valign="top" nowrap><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Password
        Again</font></td>
      <td align="left" valign="top"><input name="password2" type="password" id="password2" value="<?=$_POST['password2'];?>"></td>
    </tr>
    <tr>
      <td width="200" align="left" valign="top" nowrap><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Tell
        us about yourself!</font></td>
      <td align="left" valign="top"><textarea name="bio"><?=$_POST['bio'];?></textarea></td>
    </tr>
    <tr>
      <td align="left" valign="top">&nbsp;</td>
      <td align="left" valign="top"><input name="req" type="hidden" id="req" value="process">
        <input type="submit" name="Submit" value="Submit Information!"></td>
    </tr>
  </table>
</form>
[/code]

And here is my Join.php file.

[code]
<?php
include $_SERVER['DOCUMENT_ROOT'].'/layout.php';

switch($_REQUEST['req']){
case "process":
    myheader("Become a Member: Step 2");
    
    // Validate all required fields were posted
    if(!$_POST['first_name'] ||
       !$_POST['last_name'] ||
       !$_POST['email_address'] ||
       !$_POST['email_address2'] ||
       !$_POST['username'] ||
       !$_POST['password'] ||
       !$_POST['password2'] ||
       !$_POST['bio']){
      
          $error = true;
          $errors .= "<strong>Form Input Errors:".
                       "</strong>\n\n";
              
                    
          if(!$_POST['first_name']){
               $errors .= "Missing First Name\n";
          }
          
          if(!$_POST['last_name']){
               $errors .= "Missing Last Name\n";
          }
          
          if(!$_POST['email_address']){
               $errors .= "Missing Email Address\n";
             $email_error = true;
          }
          
          if(!$_POST['email_address2']){
               $errors .= "Missing Email Address".
                           "Verification\n";
             $email_error = true;
          }
          
          if(!$_POST['username']){
               $errors .= "Missing Username\n";
          }
          
          if(!$_POST['password']){
             $errors .= "Missing Password\n";
             $password_error = true;
            
          }
          
          if(!$_POST['password2']){
             $errors .= "Missing Password Verification\n";
             $password_error = true;
            
          }
          
          if(!$_POST['bio']){
               $errors .= "Missing Information About ".
                         "Yourself\n";
          }
    }
    
    // If both emails were posted, validate they match.    
    if($email_error == false){
            if($_POST['email_address'] !=
                    $_POST['email_address2']){
              $error = true;
              $errors .= "Email addresses do not match!\n\n";
              $email_error = true;
          }
    }
    
    // If both passwords were posted, validate they match.
    if($password_error == false){
            if($_POST['password'] != $_POST['password2']){
                    $error = true;
                    $errors .= "Passwords do not match!\n\n";
                    $password_error = true;
          }
    }
    
    if($email_error == false){
       // Verify if email address has been used already.
       $ecount = mysql_result(mysql_query("SELECT COUNT(*)
                       AS ecount FROM members
                    WHERE email_address =
                    '{$_POST['email_address']}'"),0);
                    
    // If email exists, generate error and message.
    if($ecount > 0){
       $error = true;
       $errors .="This email address has already ".
                    "been used ".
                 "please choose another.\n\n";
          }
    }
    
    // Verify if user name already exists
    $ucount = mysql_result(mysql_query("SELECT COUNT(*)
                    AS ucount FROM members
                    WHERE username = '{$_POST['username']}'"),0);
                    
    // If user name exists, generate error and message.
    if($ucount > 0){
       $error = true;
       $errors .= "User name already exists, ".
                     "please choose another.\n\n";
    }
    
    // If $error is TRUE, then include the signup form
    // and display the errors we found.
    
    if($error == true){
       $errors = nl2br($errors);
       include $_SERVER['DOCUMENT_ROOT'].
                  '/html/forms/membership_signup.html';
       footer();
       exit();
    }
    
    // All checks have passed, insert user in database
    
    // Email User
    
    // Email Admin
    
    // That's it! Done!
    
break;
    
    
    default:
        myheader("Become a member!");
        include $_SERVER['DOCUMENT_ROOT'].
                '/html/forms/membership_signup.html';
        footer();
    break;
}
?>
[/code]

Any help would be much appreciated, the form works as far as not generating any errors it's just displaying the code instead of a blank field.

Share this post


Link to post
Share on other sites
What is the name of the file containing the form? If the extension is not ".php" then the PHP processor won't get invoked and your code will be treated as data and displayed.

Ken

Share this post


Link to post
Share on other sites
value="<?=$_POST['first_name'];?>"

Should be

value="<?php echo $_POST['first_name']; ?>"

whether or not the post vars get sent on the 'back button' is a different story.
You would probably need to store them in a session...

Share this post


Link to post
Share on other sites
The "<?=" is a short hand for "<?php echo".

IMHO, it is one of the worst contructs in the langauge. I believe it will be going away along with short tags "<?" in version 6, so start making sure you stop using them now.

Ken

Share this post


Link to post
Share on other sites
[!--quoteo(post=380662:date=Jun 6 2006, 11:48 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Jun 6 2006, 11:48 AM) [snapback]380662[/snapback][/div][div class=\'quotemain\'][!--quotec--]
The "<?=" is a short hand for "<?php echo".

IMHO, it is one of the worst contructs in the langauge. I believe it will be going away along with short tags "<?" in version 6, so start making sure you stop using them now.

Ken
[/quote]

Thanks for the help guys, that seemed to do the trick. It now displays blank and also displays form data when you click the back button. I'll be sure I use the <?php echo... from now on.

Share this post


Link to post
Share on other sites
[!--quoteo(post=380668:date=Jun 6 2006, 12:07 PM:name=rstrik)--][div class=\'quotetop\']QUOTE(rstrik @ Jun 6 2006, 12:07 PM) [snapback]380668[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Thanks for the help guys, that seemed to do the trick. It now displays blank and also displays form data when you click the back button. I'll be sure I use the <?php echo... from now on.
[/quote]

This method worked great for one form but I have a simple login_form.html that it won't work with for some reason. I'm using the same method from above but it still displays the php code in the input box. Any ideas why it would do this on one form and not another using the same <?php echo...method?

login_form.html
[code]<div align="center"><strong><font size="4" face="Verdana, Arial, Helvetica,
sans-serif">Please Login </font></strong></div>
<form action="/login.php" method="post">
    <table width="30%" border="0" align="center" cellpadding"4" cellspacing"0">
         <tr>
        <td width="19%"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">
            <strong>Username:</strong></font></td>
            <td width="81%"><input name="username" type"text" id="username"
            value="<?=$_POST['username'];?>"></td>
        </tr>
        <tr>
            <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">
            <strong>Password:</strong></font></td>
            <td><input name="password" type="password" id="password"></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><div align="center">
                <input type="hidden" name="req" value="validate">
                <input type="submit" name="Submit" value="Submit">
            </div></td>
        </tr>
    </table>
</form>
[/code]

Share this post


Link to post
Share on other sites
Just curious if anybody had a chance to look at this? I have no idea why it would work fine in one form but not another.

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.