Jump to content

loop through form fields returning session variable error message


old_foggie

Recommended Posts

Hello all,  I'm new to PHP and new to this forum (although I have benefitted from your help already -cheers!). However,  this time I cannot find the answer I need/recognise/understand..

 

I have a form and want to conduct tests on each field returning an error message as a session variable if the test fails. The test will be different for some of the fields, and the error message is specific to each field. If there is an error in any one of the fields I want to be redirected to a failure page where all of the error messages are displayed,  otherwise I am sent on to another page.

 

I have already written and tested a function to sanitise the incoming form data,  so that's not a problem - it's just how to loop through and test.

 

I can guess that there are many ways to do this but I need to understand why one option is better than another,  and follow the syntax used (it's all part of my steep learning curve)

 

The approach I have thought to use is to create an array holding the field name, the test and the message,  then loop through using foreach, applying the array values into the test and creating the error message....but it's not working for me.

 

The other method is to declare a variable $Stop='No' and if the loop identifies an error, part of the output is to change this to 'yes' and through that redirect to the error page.

 

I'd really welcome your advice and tuition....cheers.. my code so far is...

 

$Stop='No';
$StaffPassCheck=sanitisealphanum($_POST['PasswordCheck']);

$Errors[0]['value']= sanitisealphanum($_POST['FirstName']);
$Errors[0]['message']='Please re-enter your name';
$Errors[0]['test']=($StaffFname="");

$Errors[1]['value']= sanitisealphanum($_POST['Surname']);
$Errors[1]['message']='Please re-enter your surname';
$Errors[1]['test']=($StaffSname="");

$Errors[2]['value']= sanitisealphanum($_POST['Post']);
$Errors[2]['message']='You must select an option';
$Errors[2]['test']=($StaffPost="Select Value");

$Errors[3]['value']= sanitisealphanum($_POST['Username']);
$Errors[3]['message']='You must select an option';
$Errors[3]['test']=($StaffUser="");

$Errors[4]['value']= sanitisealphanum($_POST['Password']);
$Errors[4]['message']='Please re-enter your password';
$Errors[4]['test']=($StaffPass="");

$Errors[5]['value']= sanitisealphanum($_POST['PasswordCheck']);
$Errors[5]['message']='Sorry, your passwords do not match';
$Errors[5]['test']=($StaffPass===$StaffPassCheck);

foreach ($Errors as $key => $Value){
if ( $Errors['test']=true ){
$Stop='Yes';
return $_SESSION[$key]=$Value['message'];
}
}

if ($Stop='Yes'){
header('Location.test.php');
die();
}else{
header('Location.indexp.php');
}

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.