weetabix Posted December 4, 2007 Share Posted December 4, 2007 Ok, I have some code and I'm using echo to display an error message... Here's en example if(is_numeric($_POST['staffid'])){ }else{ echo 'Please enter a numeric staff id'; show_form(); exit; } now I want the echo to appear in a cell within a table. Is that possible? If yes how can it be done? Thanks again, awesome site Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 4, 2007 Share Posted December 4, 2007 It's hard to help you with your vague explanation. I'm assuming the table you want to display the error message in is in the show_form() function? If so, you can make the message a parameter of the function. Post your code to the show_form() function and explain more exactly what you want. Quote Link to comment Share on other sites More sharing options...
weetabix Posted December 4, 2007 Author Share Posted December 4, 2007 show_form() just displays the form again with a validation error message, i'm not really aware of any better ways to do it. I thought about setting a flag and something like an array to list the errors (if multiple) but I don't know how to implement it. The show_form() code is like this. I didn't copy the whole thing cause its just a repeatance of cells. What i want it to do, is add another box on the bottom stating something like "Error message: " and then list the error that happened when the user tried to send invalid data. Checks are applied later on. function show_form(){ ?> <form action="addStaff.php" method="post"> <table width="50%" height="54%" cellspacing="1"> <tr> <td height="59" width="476" colspan="4"> <p align="center">Add a new staff member</td> </tr> <tr> <td height="44" width="76">Staff ID:</td> <td height="44" width="128"><input type = "Text" name="staffid"></td> <td height="44" width="119">First Name</td> <td height="44" width="135"><input type = "Text" name="firstname"></td> </tr> <tr> Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted December 4, 2007 Share Posted December 4, 2007 <?php function show_form() { if(is_numeric($_POST['staffid'])){ // do something exit; }else{ echo 'Please enter a numeric staff id'; show_form(); exit; } } ?> <?php show_form(); ?> <form action="addStaff.php" method="post"> <table width="50%" height="54%" cellspacing="1"> <tr> <td height="59" width="476" colspan="4"> <p align="center">Add a new staff member</td> </tr> <tr> <td height="44" width="76">Staff ID:</td> <td height="44" width="128"><input type = "Text" name="staffid"></td> <td height="44" width="119">First Name</td> <td height="44" width="135"><input type = "Text" name="firstname"></td> </tr> <tr> Place the show_form() function where ever you want it to display at. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 4, 2007 Share Posted December 4, 2007 Okay, you can do something like this with your function <?php function show_form($errors = FALSE){ ?> <form action="addStaff.php" method="post"> <table width="50%" height="54%" cellspacing="1"> <tr> <td height="59" width="476" colspan="4"> <p align="center">Add a new staff member</td> </tr> <?php if (isset($errors)){ echo '<tr><td><b>ERRORS</b><p>'; foreach ($errors as $error){ echo '-'.$error.'<br>'; } echo '</td></tr>'; } ?> <tr> <td height="44" width="76">Staff ID:</td> <td height="44" width="128"><input type = "Text" name="staffid"></td> <td height="44" width="119">First Name</td> <td height="44" width="135"><input type = "Text" name="firstname"></td> </tr> <tr> ?> You can now pass an array of errors to your function, and it will display them if there are any. Here is an example of how you would call the function in your case <?php if(is_numeric($_POST['staffid'])){ }else{ $error[] = "Please enter a numeric staff id"; $error[] = "You can do as many errors as you want"; show_form($error); exit; } ?> Quote Link to comment Share on other sites More sharing options...
weetabix Posted December 4, 2007 Author Share Posted December 4, 2007 I tried it but it doesnt work... :/ Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 4, 2007 Share Posted December 4, 2007 I tried it but it doesnt work... :/ Well, what happened? What didn't work about it? Quote Link to comment Share on other sites More sharing options...
weetabix Posted December 4, 2007 Author Share Posted December 4, 2007 It does respond but only lists one of the errors. Now it's actually moved on to the next level, to get all the errors in the input form displayed Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 4, 2007 Share Posted December 4, 2007 Now it's actually moved on to the next level, to get all the errors in the input form displayed I'm not sure what you mean by that. Post the code your using. Quote Link to comment Share on other sites More sharing options...
weetabix Posted December 4, 2007 Author Share Posted December 4, 2007 What I mean is that I'm now creating an array of errors which will be listed once the form function is called again and the form reloads. Once this is done it's not actually displaying all the errors apart from one. Shall I call the function once all the checks are applied on to the data an my array is not empty ? My(your) code is just like you posted it, but I just had this idea i told you about... Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 4, 2007 Share Posted December 4, 2007 So the errors display fine the first time, but when you recall the function to reload the page it only displays one error when there are more? Could you post the code to where you recall the function the second time? I'm having trouble figuring out what your trying to say...so I may be way off. Quote Link to comment Share on other sites More sharing options...
weetabix Posted December 4, 2007 Author Share Posted December 4, 2007 Yup, here it is <?php function show_form($errors = FALSE){ ?> <form action="addStaff.php" method="post"> <table width="50%" height="54%" cellspacing="1"> <tr> <td height="59" width="476" colspan="4"> <p align="center">Add a new staff member</td> </tr> <tr> <td height="44" width="76">Staff ID:</td> <td height="44" width="128"><input type = "Text" name="staffid"></td> <td height="44" width="119">First Name</td> <td height="44" width="135"><input type = "Text" name="firstname"></td> </tr> <tr> <td height="41" width="76">Username:</td> <td height="41" width="128"><input type = "Text" name="username"></td> <td height="41" width="119">Last Name:</td> <td height="41" width="135"><input type = "Text" name="surname"></td> </tr> <tr> <td height="43" width="76">Password:</td> <td height="43" width="128"><input type = "Text" name="password"></td> <td height="43" width="119">Address:</td> <td height="43" width="135"><input type = "Text" name="address"></td> </tr> <tr> <td height="40" width="76">Telephone:</td> <td height="40" width="128"><input type = "Text" name="telephone"></td> <td height="40" width="119">Postcode:</td> <td height="40" width="135"><input type = "Text" name="postcode"></td> </tr> <tr> <td height="42" width="76">Email:</td> <td height="42" width="128"><input type = "Text" name="email"></td> <td height="42" width="119">Hours Per week:</td> <td height="42" width="135"><input type = "Text" name="workhrs"></td> </tr> <tr> <td height="36" width="76">Position ID:</td> <td height="36" width="128"><input type = "Number" name = "positionid"></td> <td height="36" width="119">Role:</td> <td height="36" width="135"><input type = "Text" name="role"></td> </tr> <?php if (isset($errors)){ echo '<td><b>ERRORS OCCURRED: </b><p>'; foreach ($errors as $error){ echo '-'.$error.'<br>'; } echo '</td>'; } ?> </table> <input type= "Submit" name="submit" value= "Submit"> </form> <?PHP } And the code for the somewhat validation I made if(is_numeric($_POST['staffid'])){ }else{ $error[] = 'Please enter a numeric staff id'; show_form($error); exit; } //Username must not be left empty if(!empty($_POST['username'])){ }else{ $error[] = 'You need to enter a username'; show_form($error); exit; } //Password must not be left empty if(!empty($_POST['password'])){ }else{ $error[] = 'You need to enter a username'; show_form($error); exit; } //Telephone number must not be empty and be numeric if(is_numeric($_POST['telephone']) || !empty($_POST['telephone'])){ }else{ $error[] = 'Please enter a numeric telephone number'; show_form($error); exit; } //Position ID must be entered if(is_numeric($_POST['positionid'])){ }else{ $error[] = 'Please enter a numeric position id'; show_form($error); exit; } //First name shall be completed if(!empty($_POST['firstname'])){ }else{ $error[] = 'Please enter your first name'; show_form($error); exit; } //Last name shall be completed if(!empty($_POST['surname'])){ }else{ $error[] = 'Please enter your last name'; show_form($error); exit; } //Address as well if(!empty($_POST['address'])){ }else{ $error[] = 'Please enter your address'; show_form($error); exit; } //And postcode if(!empty($_POST['postcode'])){ }else{ $error[] = 'Please enter your postcode'; show_form($error); exit; } //Working hours must be numeric and not null if(is_numeric($_POST['workhrs']) || !empty($_POST['workhrs'])){ }else{ $error[] = 'Please enter numeric working hours'; show_form($error); exit; } //E-Mail must be of a proper format if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])){ }else{ $error[] = 'Please enter a valid email address'; show_form($error); exit; } Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 4, 2007 Share Posted December 4, 2007 You had A LOT of unneeded else if statements. You were also calling the function every single time you added an error...which won't work. Change your code to this <?php if (!is_numeric($_POST['staffid'])) $error[] = 'Please enter a numeric staff id'; //Username must not be left empty if (empty($_POST['username'])) $error[] = 'You need to enter a username'; //Password must not be left empty if (empty($_POST['password'])) $error[] = 'You need to enter a username'; //Telephone number must not be empty and be numeric if (!is_numeric($_POST['telephone']) || empty($_POST['telephone'])) $error[] = 'Please enter a numeric telephone number'; //Position ID must be entered if (!is_numeric($_POST['positionid'])) $error[] = 'Please enter a numeric position id'; //First name shall be completed if (empty($_POST['firstname'])) $error[] = 'Please enter your first name'; //Last name shall be completed if (empty($_POST['surname'])) $error[] = 'Please enter your last name'; //Address as well if (empty($_POST['address'])) $error[] = 'Please enter your address'; //And postcode if (empty($_POST['postcode'])) $error[] = 'Please enter your postcode'; //Working hours must be numeric and not null if (!is_numeric($_POST['workhrs']) || empty($_POST['workhrs'])) $error[] = 'Please enter numeric working hours'; //E-Mail must be of a proper format if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])) $error[] = 'Please enter a valid email address'; if (!empty($error)){ show_form($error); exit; } ?> Quote Link to comment Share on other sites More sharing options...
weetabix Posted December 4, 2007 Author Share Posted December 4, 2007 Thanks...Fixed now 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.