asmith Posted November 24, 2007 Share Posted November 24, 2007 i have made a form look like this : (sum) <html> <body> <?php $form_block = "<form method=\"post\" action=\"te11.php\"> <p> name: </p> <input type=\"text\" name=\"name\" /> <input type=\"hidden\" name=\"check\" value=\"dd\" /> <input type=\"submit\" name=\"submit\" value=\"go!\" /> </form>"; if ($_POST[check] != "dd") // they need to see the form {echo $form_block;} else { if ($_POST[name] == "") { $name_error= "please enter your name"; echo $name_error; echo $form_block; } } ?> </body> </html> when i don't enter the name, the error message comes to the page top , i can do that for as many fields that may have error , that all the error messages would be written on top . my question is , how can i manage my code , to write each error BESIDE its fields, NOT ALL in the top of page. thanks Quote Link to comment https://forums.phpfreaks.com/topic/78672-not-top-beside/ Share on other sites More sharing options...
kenrbnsn Posted November 24, 2007 Share Posted November 24, 2007 You have to check for all errors before you display the form, saving any error messages in variables that are specific to each field. BTW, you really don't need a hidden field to tell when a user as submitted the form, just test to see whether the "submit" field is set. Here's some code that does what you want, based on your example: <?php $errors = array(); if (isset($_POST['submit'])) // form information sent foreach ($_POST as $fld => $val) switch ($fld) { case 'name': $errors[$fld] = 'name must be filled in'; break; // // put case's for other fields here. don't put one for the "submit" field or any other fields not required // } ?> <html> <body> <?php if (!isset($_POST['submit']) || !empty($errors)) { // display the form if the submit key wasn't pressed or there are error ?> <form method="post" action="> name: <input type="text" name="name" /> <?php if (isset($errors['name'])) echo ' <span style=color="red">' . $errors['name'] . '</span>';?> <input type="submit" name="submit" value="go!" /> </form> </body> </html> Ken Quote Link to comment https://forums.phpfreaks.com/topic/78672-not-top-beside/#findComment-398080 Share on other sites More sharing options...
asmith Posted November 24, 2007 Author Share Posted November 24, 2007 really sorry, can you give me an example with only one field ? (i can see only your code " <?php " ) Quote Link to comment https://forums.phpfreaks.com/topic/78672-not-top-beside/#findComment-398083 Share on other sites More sharing options...
kenrbnsn Posted November 24, 2007 Share Posted November 24, 2007 Look again. I hit the "post" button too soon and had to edit the posting to include the example. ken Quote Link to comment https://forums.phpfreaks.com/topic/78672-not-top-beside/#findComment-398086 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.