Jump to content

Recommended Posts

Use a foreach loop

foreach($_POST as $field_name => $field_value)
{
    if(empty($_POST[$field_name]) || !is_numeric($_POST[$field_name]))
    {
         $error[$field_name] = 'error message here'
     }
}

if(isset($error) && is_array($error))
{
     // display error here
}

Write a function for them, or just do a really long if statement, or you could run them through a foreach loop.

 

There really is no easy way to do it. Long forms are plain tedious.

 

<?php

function checkFields($field)
{
   if(isset($field) && $field != '' & is_numeric($field))
   {
        echo $field.' is not blank and is a number';
   }
   else
   {
      $error .= $field.' did not pass validation';
   }
}

checkFields($_POST['your_field_name']);

?>

 

Nate

 

wildteen88 beat me to the foreach thing... :)

Use a foreach loop

foreach($_POST as $field_name => $field_value)
{
    if(empty($_POST[$field_name]) || !is_numeric($_POST[$field_name]))
    {
         $error[$field_name] = 'error message here'
     }
}

if(isset($error) && is_array($error))
{
     // display error here
}

 

That does of course assume that you're wanting to check all your posted values. Even if that's the case, you'd need to exclude the submit button from the check:

 

<?php
foreach($_POST as $field_name => $field_value)
{
   if((empty($_POST[$field_name]) || !is_numeric($_POST[$field_name])) && $_POST[$field_name] != 'Submit')//or whatever you called it
   {
        $error[$field_name] = 'error message here'
    }
}

if(isset($error) && is_array($error))
{
    // display error here
}
?>

 

If it's not all of them, then either exclude others or set an array with a list of fields you do want to check --whichever is easiest.

ok so i cant really understand the second if statement, maybe you could explain what happens exactly there. secondly i am including this on the second page to which the first page including the form sends the data. now if there is an error i would like to redirect back to the 1st page after showing an error message, or indeed show it on the 1st page, and also populate the form with the data, how can i do that? thanks

The second statement is to determine if there was an error message set. If $error is set and $error is an array, then display that message.

 

I have found it easier to place the form and it's validation on one page. that way you don't have to worry about sessions to get data between the 2 to display an error message. As it stands, you will probably need to set the error messages in an array and then set that as a session to redirect them back to the page.

 

the header(); function is what you would use to redirect.

 

nate

A quick sample

 

<?php

function contact_form()  // place form in a function
{ 
global $error,$state_list; set some global vars
		if(isset($error)){echo '<div class="error" align="center">'.$error.'</div>';};// if $error is set show it
?>
<form>the form</form>
<?php } // end the function 

if(!$_POST['contact_form'])
{ 
  	// call the contact form function
contact_form();
}
else // else, the form has been submitted so we start processing it
{
// process form
}

?>

 

 

 

 

 

 

 

I forgot to mention that the reason I have the form as a function is because in my form processing I typically will do something like this during the validation before I submit to a db or email or such

 

<?php

if(isset($error))
{
   contact_form();
}
else
{
   //insert to db
   //email the results
   //write to file
   // etc
}
?>

 

This makes it easy to determine where a forms processing is happening... but it makes for lots of code on a page....

 

hope it helps..

 

Nate

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.