Jump to content

not top, beside!


asmith

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/78672-not-top-beside/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/78672-not-top-beside/#findComment-398080
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.