OldWest Posted January 6, 2011 Share Posted January 6, 2011 Been screwing around on Google for about 3 hours trying to find a tutorial on what I am trying to do with absolutely no luck! I am simply trying to get my test script to echo errors from an array when a form criteria does not validate. This is my final revision which is still not working! Can someone please tell me what I am doing wrong? No matter what I do, I can't get away from this error: Notice: Undefined variable: error in C:\wamp\www\php\form_validation.php on line 13 <?php $o = $error[]; // test echo $o; // test if(!preg_match('/[^0-9A-Za-z]/',$_POST['first_name'])) { $error[] = "Please enter a valid First Name"; ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> First Name:<br /> <input name="first_name" type="text" size="50" maxlength="50" /><br /><br /> <input type="submit" /><br /><br /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/223552-displaying-php-form-validation-errors-like-error-and-echoing-them-out/ Share on other sites More sharing options...
Anti-Moronic Posted January 6, 2011 Share Posted January 6, 2011 Are you sure that's all the code? I'm not sure it has pasted well. Instead of form action="<?php echo $_SERVER['PHP_SELF']; ?>" You can just do: form action="" Quote Link to comment https://forums.phpfreaks.com/topic/223552-displaying-php-form-validation-errors-like-error-and-echoing-them-out/#findComment-1155587 Share on other sites More sharing options...
OldWest Posted January 6, 2011 Author Share Posted January 6, 2011 Yes. That's all of the code. Im not trying to do rocket science here. Just trying to figure out how to get an array echoing errors like I suggest. Are you sure that's all the code? I'm not sure it has pasted well. Instead of form action="<?php echo $_SERVER['PHP_SELF']; ?>" You can just do: form action="" Quote Link to comment https://forums.phpfreaks.com/topic/223552-displaying-php-form-validation-errors-like-error-and-echoing-them-out/#findComment-1155589 Share on other sites More sharing options...
OldWest Posted January 6, 2011 Author Share Posted January 6, 2011 I realized I left out a bracket, but of course that solved nothing in relation to getting my error[] array to echo. Still hacking away at this trying variations... Any tips or advice is much appreciated.. Its as if the array is not holding the data. Quote Link to comment https://forums.phpfreaks.com/topic/223552-displaying-php-form-validation-errors-like-error-and-echoing-them-out/#findComment-1155592 Share on other sites More sharing options...
Pikachu2000 Posted January 6, 2011 Share Posted January 6, 2011 print_r($error); You can't simply echo an entire array. Quote Link to comment https://forums.phpfreaks.com/topic/223552-displaying-php-form-validation-errors-like-error-and-echoing-them-out/#findComment-1155683 Share on other sites More sharing options...
Dragosvr92 Posted January 6, 2011 Share Posted January 6, 2011 You may use HTML5 To Validate forms but it wont work on older browsers Quote Link to comment https://forums.phpfreaks.com/topic/223552-displaying-php-form-validation-errors-like-error-and-echoing-them-out/#findComment-1155742 Share on other sites More sharing options...
OldWest Posted January 6, 2011 Author Share Posted January 6, 2011 Thanks for the tips. I am still working on this, but I was able to get this far with some results. I still have some mixed results though. What I don't understand is why preg_match seems to return TRUE if the results are met, and my errors are thrown then.. Does it seem like I am doing this efficiently? <?php if(isset($_POST['set_test'])) { if(preg_match('/[^A-Za-z]/',$_POST['first_name'])) { $error[] = "Please enter a valid First Name"; } else { $error[] = ""; } if(preg_match('/[^A-Za-z]/',$_POST['last_name'])) { $error[] = "Please enter a valid Last Name"; } else { $error[] = ""; } echo $error[0]; echo $error[1]; } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> First Name:<br /> <input name="first_name" type="text" size="50" maxlength="50" /><br /><br /> Last Name:<br /> <input name="last_name" type="text" size="50" maxlength="50" /><br /><br /> <input name="set_test" type="hidden" value="" /> <input type="submit" /><br /><br /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/223552-displaying-php-form-validation-errors-like-error-and-echoing-them-out/#findComment-1155747 Share on other sites More sharing options...
OldWest Posted January 6, 2011 Author Share Posted January 6, 2011 I had a bunch of issue with my regex and script, but I think I am just about solved... Will post solved when there: <?php if(isset($_POST['set_test'])) { if(!preg_match("/^[A-Za-z' -]{1,50}$/",$_POST['first_name'])) { $error[] = "Please enter a valid First Name"; } else { $error[] = ""; } if(!preg_match("/^[A-Za-z' -]{1,50}$/",$_POST['last_name'])) { $error[] = "Please enter a valid Last Name"; } else { $error[] = ""; } if(is_array($error)) { foreach($error as $err_message) { echo $err_message . "<br />"; } } //exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/223552-displaying-php-form-validation-errors-like-error-and-echoing-them-out/#findComment-1155755 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.