Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.