Claite Posted February 11, 2009 Share Posted February 11, 2009 Not sure if this is the right section. I made a registry page that works just fine. But one thing is that when the person doesn't enter info into a field, I print a message saying "You haven't filled out all the fields.. yaadaa yaadaa" But all the forms are blank since the page reloaded. How do I make it so I can still display the message, but keep the fields they filled in still filled in. I'm using a register.html, which calls a register.php to process the data. If they didn't fill out a field, I include register.html with a variable $error that prints out the error if they didn't put in the info. Here's the register.html code: <?php echo $error ?> <form action="register.php" method ="post"> <b>Username:</b> <input type="text" name="username" maxlength="60"><br><br> <b>Password:</b> <input type="password" name="pass" maxlength="60"><br><br> <b>Age:</b> <input type="text" name="age" maxlength="3"><br><br> <b>Email:</b> <input type="text" name="email" maxlength="100"><br><br> <input type="submit" name="submit" value="Submit"> and here's the register.php code: if($username=='' | $pass == '' | $age == '' | $email == ''){ unset($username); unset($password); unset($email); unset($age); $error = '<b>You have left one of the datafields blank. Please fill out all the information</b><br><br>'; include 'register.html'; exit(); } Quote Link to comment Share on other sites More sharing options...
gunabalans Posted February 11, 2009 Share Posted February 11, 2009 <form method = "post" action ="register.php"> <div id="err" name="err"> throw error here </div> <p><input type="text" name="username" value ="" > <label for="username">User Name</label></p> <p><input type="password" name="pasword" value ="" > <label for="pasword">Password</label></p> <p><input type="text" name="age" value ="" > <label for="age">Age</label></p> <p><input type="text" name="email" value ="" > <label for="email">email</label></p> <p><input type="submit" name="send" value ="" > <label for="send"></label></p> <p><input type="reset" name="clear" value ="" > <label for="clear"></label></p> </form> Call a javascript function using ONCHANGE event do not allow to post when ther is a field left unfilled use return false; This code is generated from a small tool http://kilinjal.com/index.php?cat=fgen&flag=m Quote Link to comment Share on other sites More sharing options...
haku Posted February 11, 2009 Share Posted February 11, 2009 Javascript is no good because people can and will turn it off (I do myself, along with every other user of the firefox noscript addon). It is fine for adding function to a form, but shouldn't be relied upon as the only validation for a form. You can do this two ways. One way is to move your processing script to the top of the same page that the form is on. Then you just output the $_POST value into the value="" of the inputs. The other way is to leave your processing on two separate pages as you are doing now, but enter all the values into a $_SESSION variable, then use those values to populate the form when you go back to the form page. Quote Link to comment Share on other sites More sharing options...
Claite Posted February 11, 2009 Author Share Posted February 11, 2009 Haku, I'm getting a problem when I try it as you suggested. First. I had to convert my page to a .php to read the script of <?php ?> inside of the value quotes. This resulted in an error for my $error variable that I through when I do an include of the page, since it's undefined until they type something wrong. To solve this i put $error = ""; at the beginning of the page code, but now when it re-writes it in if($username=='' | $pass == '' | $age == '' | $email == ''){ $error = '<b>You have left one of the datafields blank. Please fill out all the information</b><br><br>'; include 'register.php'; exit(); } it gets RE-written again when doing the include. How do I make it so it doesn't re-initialize to "" when it re-loads the page? Or is there a better way to make it print the error? The same goes for the $_SESSION['username'] etc variables. I want to re-write them to nothing the first time in, then save them from then on after they enter something. I'm thinking along the lines of some if statement but I don't know how that'd work without initializing the variable before the statement. Maybe something like $failed = 0; if ($failed == 0) { $error = "" $_SESSION[.... .... .... $failed = 1; } but that'd also mess up when it includes the page. And just for reference, here is my form/error script: <?php echo $error ?> <form action="registerscript.php" method ="post"> <b>Username:</b> <input type="text" name="username" maxlength="60" value="<?php echo $_SESSION['username'] ?>"><br><br> <b>Password:</b> <input type="password" name="pass" maxlength="60" value="<?php echo $_SESSION['pass'] ?>"><br><br> <b>Age:</b> <input type="text" name="age" maxlength="3" value="<?php echo $_SESSION['pass'] ?>"><br><br> <b>Email:</b> <input type="text" name="email" maxlength="100" value="<?php echo $_SESSION['pass'] ?>"><br><br> <input type="submit" name="submit" value="Submit"> </form> God I hate notepad's formatting. Any suggestions on a better text editor? Preferably one with highlighting. EDIT: I deeply apologize for this going from a HTML help topic to a php one. If you want to move it to the PHP help section so I can get some feedback there, that'd be fine. Thank you! Quote Link to comment Share on other sites More sharing options...
haku Posted February 11, 2009 Share Posted February 11, 2009 I don't know how your scripts are all put together, so I really can't answer your questions. But here is a basic example of how I would do a page. <?php if(form has been submitted) { $errors = FALSE; if(check if there are any errors) { $errors = TRUE; $error_text = 'you have an error in ____. It is ____'; } } <doctype> <html> <head> // head stuff </head> <body> <?php if($errors) // this works because $errors will be either true or false { echo $error_text; } ?> <form method="post"> <input name="username"<?php if(isset($_POST['username']) && $_POST['username'] != '') { echo ' value="' . $_POST['username'] . '"'} ?> /> </form> </body>[/code] </html> This is extremely simplified, with lots of stuff left out, and I just used plain text in some parts, but if you look at it you should be able to get an idea of how you can structure your page. You basically want it in this order: php form processing -> doctype -> opening html tag ->head tags and contents -> opening body tag -> error messages -> form -> closing body tag and html tag. You can actually put the error messages wherever you want on the page, as long as they are in the body tags. Same with the form. But you should have all the processing at the very start before your doctype. Quote Link to comment Share on other sites More sharing options...
Claite Posted February 11, 2009 Author Share Posted February 11, 2009 Haku, Sorry, but I don't understand the need of a doctype. What exactly does it do? I'm pretty new to the HTML/PHP scene, so I don't really understand some of these things. Claite Quote Link to comment Share on other sites More sharing options...
Claite Posted February 11, 2009 Author Share Posted February 11, 2009 I can't find the edit button. It would seem that after a period of time you can't modify your post... strange. So I solved the problem. It would seem that !isset($submit) does not require $submit to be declared, so I can change it in the registerscript.php to a value, and then it'll come up as true. Here is why I have now in case anyone else runs into this issue: register.php <html> <head> <?php session_start(); if (!isset($submit)){ $error = ""; session_destroy(); } ?> stuff stuff stuff <?php echo $error ?> <form action="registerscript.php" method ="post"> <b>Username:</b> <input type="text" name="username" maxlength="60" value="<?php if(isset($submit)){ echo $_SESSION['username']; }?>" ><br><br> <b>Password:</b> <input type="password" name="pass" maxlength="60" value="<?php if(isset($submit)){ echo $_SESSION['pass']; } ?>"><br><br> <b>Age:</b> <input type="text" name="age" maxlength="3" value="<?php if(isset($submit)){ echo $_SESSION['age']; } ?>"><br><br> <b>Email:</b> <input type="text" name="email" maxlength="100" value="<?php if(isset($submit)){ echo $_SESSION['email']; } ?>"><br><br> <input type="submit" name="submit" value="Submit"> </form> registscript.php <?php stuff session_start(); session_regenerate_id(); $_SESSION['username'] = $username; $_SESSION['pass'] = $pass; $_SESSION['age'] = $age; $_SESSION['email'] = $email; session_write_close(); if($username=='' | $pass == '' | $age == '' | $email == ''){ $submit = 1; $error = '<b>You have left one of the datafields blank. Please fill out all the information</b><br><br>'; include 'register.php'; exit(); } more stuff ?> Quote Link to comment Share on other sites More sharing options...
haku Posted February 12, 2009 Share Posted February 12, 2009 A doctype establishes the rules that the browser uses to process the HTML. There are different types of web documents. HTML and XHTML are the main ones, but even within those there are different versions. If you don't have a doctype, the browser doesn't know what set of rules to process your page under, and has to guess. Different browsers guess different ways, so you end up with a lot of cross-browser compatibility issues. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.