Jump to content

[SOLVED] Error on submitting information


graham23s

Recommended Posts

Hi Guys,

 

i have seen on a good few websites a neat style they do, say for example you leave out a required text field the input area turns red to show you, that its a required field, i normally grab all the POST data then if its empty say so, how is that achieved?

 

any advice would be great

 

cheers

 

Graham

Link to comment
Share on other sites

Well, JS validation is fine, but should not be relied upon since users can turn it off. Here is an example of how it can be done.

 

<?php

if (isset($_POST['submit'])) {
  $error = false;

  if ($_POST['name']=='') {
    $field_error['name'] = false;
    $error = true;
  }

  if ($_POST['email']=='') {
    $field_error['email'] = false;
    $error = true;
  }

  if (!$error) {
    //process form & redirect
  }

}

echo "<form>\n";
$color=($field_error['name']===false)?'#ff0000':'#000000';
echo "<input type=\"text\" name=\"name\" value=\"".$_POST['name']."\" style=\"background-color:$color\">\n";
$color=($field_error['email']===false)?'#ff0000':'#000000';
echo "<input type=\"text\" name=\"email\" value=\"".$_POST['email']."\" style=\"background-color:$color\">\n";
echo "<input type=\"submit\" value=\"true\" name=\"Submit\">\n";
echo "</form>\n";

?>

 

This is not tested, but you should get the idea. I just slapped this together, so I am sure there is a more efficient method.

Link to comment
Share on other sites

Well, JS validation is fine, but should not be relied upon since users can turn it off. Here is an example of how it can be done.

 

<?php

if (isset($_POST['submit'])) {
  $error = false;

  if ($_POST['name']=='') {
    $field_error['name'] = false;
    $error = true;
  }

  if ($_POST['email']=='') {
    $field_error['email'] = false;
    $error = true;
  }

  if (!$error) {
    //process form & redirect
  }

}

echo "<form>\n";
$color=($field_error['name']===false)?'#ff0000':'#000000';
echo "<input type=\"text\" name=\"name\" value=\"".$_POST['name']."\" style=\"background-color:$color\">\n";
$color=($field_error['email']===false)?'#ff0000':'#000000';
echo "<input type=\"text\" name=\"email\" value=\"".$_POST['email']."\" style=\"background-color:$color\">\n";
echo "<input type=\"submit\" value=\"true\" name=\"Submit\">\n";
echo "</form>\n";

?>

 

This is not tested, but you should get the idea. I just slapped this together, so I am sure there is a more efficient method.

Yeah thats a better and more reliable way to do it. If you do that, make sure you use trim() because there may be whitespace in the value.

 

foreach ($_POST as $key => $value)
{
$_POST[$key] = trim($value);
}

Link to comment
Share on other sites

PhREEEk is right . he means without refreshing the page . that's the thing php can't do !

Then it shouldn't be in this section of the forum.

 

???

 

If it's just a question, and he doesn't know the answer, then this is the perfect spot for the initial post. If he wants more details on how to actually code it, then he should post to the JS board...

 

PhREEEk

Link to comment
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.