Hy! I am new to PHP and tried to perform some form validation. The code shall check if the form contains a value at all! It does not work out and I am not quite sure if this code could work at all! So- thanks for help! html> <head></head> <body> <?php if (!isset($_POST['submit'])) { ?> <form action= "evaluation.php"> <p>ID Number:<br><input name="ID" type="text" size="30" method= "post"></p> <input type="submit" value="submit"> </form> <?php } else { if (!isset($_POST['ID']) || trim($_POST['ID']) == '') { die("ERROR: please check your input again!!"); } ?> </body> </html> Code (markup):
Here you are (a simple one) <body> <?php //This will test if at least one field was filled $initial_state=TRUE; foreach($_POST as $key=>$value){ if(strlen(trim($value))){ $initial_state=FALSE; break; } } //This will validate the form values if(!$initial_state){ $errors = array (); if(!ereg('^[0-9]{4}$',trim($_POST['ID']))) $errors['ID Number']='Must be 4 digits'; if(!eregi('^[a-z]{6}$',trim($_POST['code']))) $errors['Code']='Must be 6 letters'; //this is where you test the values sent by the user } //This will print the table containing the erros if ($errors){ foreach($errors as $field=>$error) echo "$field: $error<br />\n"; } //We will display the form the first time or if there are errors in the values sent if ($initial_state || $errors){ ?> <form method="post" action= "test.php" style="border:solid 1px grey;width:200px;padding:20px;"> ID Number (4 digits):<br> <input name="ID" type="text" size="30" value="<?php echo $_POST['ID']?>"/> Code (6 letters):<br> <input name="code" type="text" size="30" value="<?php echo $_POST['code']?>" /> <input type="submit" value="submit" /> </form> <?php } else{ //For sent without errors echo "Success !!!"; } ?> </body> PHP:
Of course there are some bugs: try : "<b>This is a bug</b> (with the quote) in a field and hit submit, Solution: skip the html special characters (< > ' " & /) with a the php string function "htmlsecialchars()".
method=post goes in the form attribute not the input attribute. It should read... <form action= "evaluation.php" method="post"> That's probably why your code doesn't work.
Hy! Thanks for your answers! Through your help I learn a lot! With my code I get now (after changing it according to ecentricNick) following message: Parse error: syntax error, unexpected $end in C:\Programme\Zend\Apache2\htdocs\user_validation\form.php on line 25 Line 25 is the last line! regards steyr
That means that php was expecting something...here the last "}" was missing <html> <head></head> <body> <?php if (!isset($_POST['submit'])) { ?> <form action= "evaluation.php"> <p>ID Number:<br> <input name="ID" type="text" size="30" method= "post"> </p> <input type="submit" value="submit"> </form> <?php } else { if (!isset($_POST['ID']) || trim($_POST['ID']) == ''){ die("ERROR: please check your input again!!"); } } ?> </body> </html> PHP:
Thanks for reply! How could I be so stupid to forget the }! But still it does not work properly! When I hit the submit button, even when the form contains no value the next page (evaluation.php) appears. So still not input validation! But I will work on it!
The error was that you forgot to name the <input submit> so this condition was always true !isset($_POST['submit']) PHP: <html> <head></head> <body> <?php if (!isset($_POST['submit'])) { ?> <form action= "test.php" method="post"> <p>ID Number:<br> <input name="ID" type="text" size="30"> </p> <input name="submit" type="submit" value="submit"> </form> <?php } else { if (strlen(trim($_POST['ID']) == 0)){ die("ERROR: please check your input again!!"); } } ?> </body> </html> PHP:
Hy! I could make the code work but not the way I want it. Now the code works but the form always calls itself- but when the input is correct it should submit the user input to an other file! But I am coming closer to solution! regards <body> <?php if (!isset($_POST['submit'])) { ?> <form action = '<?php $_SERVER['PHP_SELF'] ?>' method = 'post'> ID Number <br /> <input type = 'text' name = 'ID'> <br /> <input type = 'submit' name = 'submit' value = 'Save'> </form> <?php } else { // check for required data // die if absent if (!isset($_POST['da_id']) || trim($_POST['da_id']) == '') { die("ERROR: please insert a value"); } else { // display message echo "correct input"; }} ?> </body>
Hy! Just solved the whole thing! I just put together the form and the further processing of the data into one file- so it works fine! Thanks for your help again- it helped a lot!!!
If the input is correct, issue a header redirect to take them to the next page. You'll need to do the test earlier though, as it must be sent before any other output.