graham23s Posted December 23, 2007 Share Posted December 23, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/82923-solved-error-on-submitting-information/ Share on other sites More sharing options...
PHP_PhREEEk Posted December 23, 2007 Share Posted December 23, 2007 Javascript form validation PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/82923-solved-error-on-submitting-information/#findComment-421723 Share on other sites More sharing options...
Psycho Posted December 23, 2007 Share Posted December 23, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/82923-solved-error-on-submitting-information/#findComment-421729 Share on other sites More sharing options...
papaface Posted December 23, 2007 Share Posted December 23, 2007 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); } Quote Link to comment https://forums.phpfreaks.com/topic/82923-solved-error-on-submitting-information/#findComment-421730 Share on other sites More sharing options...
PHP_PhREEEk Posted December 23, 2007 Share Posted December 23, 2007 i normally grab all the POST data then if its empty say so I took from that that the OP meant error-checking 'on the fly' within the browser, not by refreshing the page and then generating the errors. My bad if that wasn't correct... PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/82923-solved-error-on-submitting-information/#findComment-421731 Share on other sites More sharing options...
asmith Posted December 23, 2007 Share Posted December 23, 2007 PhREEEk is right . he means without refreshing the page . that's the thing php can't do ! Quote Link to comment https://forums.phpfreaks.com/topic/82923-solved-error-on-submitting-information/#findComment-421741 Share on other sites More sharing options...
papaface Posted December 23, 2007 Share Posted December 23, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/82923-solved-error-on-submitting-information/#findComment-421742 Share on other sites More sharing options...
PHP_PhREEEk Posted December 23, 2007 Share Posted December 23, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/82923-solved-error-on-submitting-information/#findComment-421745 Share on other sites More sharing options...
graham23s Posted December 23, 2007 Author Share Posted December 23, 2007 Thanks a lot guys i think i know what i'm doing now:) Graham Quote Link to comment https://forums.phpfreaks.com/topic/82923-solved-error-on-submitting-information/#findComment-421760 Share on other sites More sharing options...
Psycho Posted December 23, 2007 Share Posted December 23, 2007 In any event, JavaScript should not be relied upon as the only means of validation. Quote Link to comment https://forums.phpfreaks.com/topic/82923-solved-error-on-submitting-information/#findComment-421861 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.