php form self post
Posted: Sat Jul 19, 2003 7:22 am
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
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> </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
?>
?>