php form self post

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
jamrop
Forum Commoner
Posts: 80
Joined: Fri May 16, 2003 5:38 pm

php form self post

Post by jamrop »

Hey , I am trying to make the information that a user has filled in the forms to be kept there if they do something wrong e.g. invalid email address. Php will tell the user that it is invalid, but all the fields that a person filled in, are all blank again. Hard to explain

the code

Code: Select all

<?php


require('../dbinfo.php');

<table width="100%" border="0" cellspacing="1" cellpadding="0">
              <tr>
                <td width="30%">Username:</td>
                <td width="63%"><form name="register" action="<?php $PHP_SELF ?>" method="post">
                    <input type="text" name="username" size="30">
                 </td>
              </tr>
              <tr>
                <td>Password:</td>
                <td><input type="password" name="password"></td>
              </tr>
              <tr>
                <td>Confirm Password:</td>
                <td><input type="password" name="confirm"></td>
              </tr>
              <tr>
                <td>Address Postcode<span class="small">(<font color="#FF0000"><br>
                  </font>)</span></td>
                <td><input type="text" name="postcode" size="10"></td>
              </tr>
              <tr>
                <td>Email:<span class="small">(<font color="#FF0000">A valid email address is required <br>
                  to activate your account </font>)</span></td>
                <td><input type="text" name="email" size="30"></td>
              </tr>
              <tr>
                <td>Home Telephone :</td>
                <td><input type="text" name="hometel"></td>
              </tr>
              <tr>
                <td>Mob Telephone (Optional):</td>
                <td><input type="text" name="mobtel"></td>
              </tr>
              <tr>
                <td>&nbsp;</td>
                <td>
                    <input type="submit" name="submit" value="Submit">
                  </form></td>
              </tr>
            </table>
            <p><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"> </font></p>
            <?php 
			
if ($submit) {
$valid=true;
if (!$username | !$password | !$confirm |  !$postcode  |  !$email  | !$hometel  |  !$mobtel)
{$errmsg.="You have not filled in a required field<br>"; $valid=false;}
					
$sql = "select username from members where username = '$username'";
$rs=mysql_query($sql,$db);
$numofrows =mysql_num_rows($rs);
if($numofrows !=0) {
$errmsg.=" Sorry, the username: <strong> $username </strong> 
is already taken, please pick another one<br>"; $valid=false;}
							
if  ($password != $confirm){
$errmsg.="Passwords did not match, Please try again<br>"; $valid=false;}
									
$email = trim($email);
$_name = "/^[-!#$%&''*+\\.\/0-9=?A-Z^_'{|}~]+";
$_host = "([-0-9A-Z]+\.)+";
$_tlds = "([0-9A-Z]){2,4}$/i";
if (!preg_match($_name."@".$_host .$_tlds,$email) )
{$errmsg.="Invalid email address, Please try again<br>";
$valid= false;}
							
}
					
if ($valid!=true)
{echo ( $errmsg.$form);}
					
else 
{echo "Welldone";} /// Testing atm
					
?> 
?>
RFairey
Forum Commoner
Posts: 52
Joined: Fri Jun 06, 2003 5:23 pm

Post by RFairey »

Use isset($var) to see if the POST variables are set -> if not, show the form.

Then check the vars for validity - whether they are empty, or whether they are not long enough (for passwords) or whether they must be numbers etc -> display error messages, and then display the form, echoing those variables that ARE correct into your form:

Code: Select all

<?php
echo '<INPUT TYPE="text" NAME="postcode" VALUE="'$postcode'">

?>
jamrop
Forum Commoner
Posts: 80
Joined: Fri May 16, 2003 5:38 pm

Post by jamrop »

Thanks for your help , but could you explain
Use isset($var) to see if the POST variables are set -> if not, show the form
not sure what to do


MAny thanks
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Here's an example page...

Code: Select all

<?php

if(isset($username) && isset($password))
&#123;
if(strlen($username)!=0 && strlen($password)!=0)
&#123;
//check username and password against database or something
//then bounce to another page using header('location:newpage.php')
&#125;
&#125;

//if the username and password isn't set then the page displays as normal

?>

<html>

<body>

<form method=post action="?">
<input name=username type=text value="<?php echo($username); ?>">
<input name=password type=password value="<?php echo($password); ?>">
<input type=submit value="submit">
</form>

</body>

</html>
The input values will display the username and password if they have been previously submitted via the form, otherwise they will display nothing.
jamrop
Forum Commoner
Posts: 80
Joined: Fri May 16, 2003 5:38 pm

Post by jamrop »

hey thanks

i have tried using that, but i get Warning: Cannot add header information

If the infomation is correct, i want it to load another page to say well done, but at present with out the isset, i get a well done message and the info they filled in are still there. Well confused.

i have tried a header location instead of else echo "well done" but get warning again.

Help is very appriecated

many thanks
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Make sure the PHP is above the html and head tags for a start. Also I didn't test the script so it may not be 100% bug free.... it's just an example to show you how to do what you want to do.
jamrop
Forum Commoner
Posts: 80
Joined: Fri May 16, 2003 5:38 pm

Post by jamrop »

is it possible to not do a self post and instead go to a e.g. checkreg.php in the form action. then do all the php validation checks and if anything is wrong, it shows the message and a link button to go back to the register form page and have all the data that a user had entered kept in the form fields instead of it being blank??

many thanks
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

You will need to either a:use a Session and store the data in there to send back to the form page.. or b: send the form variables back to the form page using form.php?username=bob etc.
RFairey
Forum Commoner
Posts: 52
Joined: Fri Jun 06, 2003 5:23 pm

Post by RFairey »

Self posting is the way to do things like this - the cannot add header error is a different problem - you are trying to use header(Location= or start a session or something like that, after you've sent someHTML to the browser. If you want redirects, either rethink the way your pages are laid out, or use javascript
Post Reply