Jump to content

Displaying php form validation errors like error[] and echoing them out


OldWest

Recommended Posts

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>

 

Link to comment
Share on other sites

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=""

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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();
}
?>

 

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.