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!
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
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:
<?php
if(isset($username) && isset($password))
{
if(strlen($username)!=0 && strlen($password)!=0)
{
//check username and password against database or something
//then bounce to another page using header('location:newpage.php')
}
}
//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.
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.
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.
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??
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.
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