Jump to content

[SOLVED] Validation


gum1982

Recommended Posts

 

Hello i really need help i need to validate a form but i want one set of valdaition to be confirmed before it even runs the next bit of code. here is my code.

if(isset($_POST['submit'])){ 
    $error = ''; // set error variable to blank
  if(trim($_POST[firstname]) == '') {
         $error.="Please enter you firstname<br />";
              }
  if(trim($_POST[lastname]) == '') {
         $error.="Please enter you lastname<br />";
              }
  if(trim($_POST[email])==''){
    $error.="An email address is required!<br />"; 
  }
      else {
        if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST[email])) { 
        $error.="The e-mail you entered was not in the proper format!";       
        }
    }
   if(trim($_POST[custom_CompanyName]) == '') {
         $error.="Please enter your company name<br />";
              }
   if(trim($_POST[custom_CompanyAddress1]) == '') {
         $error.="Please enter your company address<br />";
              }

if(trim($_POST[custom_Postcode]) == '') {
    $error.="Please enter your postcode<br />";
              }else{
                 if(!eregi("^([Gg][ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2})$", $_POST[custom_Postcode])) {
        $error="Please enter a valid postcode!";
    }
}
      if(trim($_POST[custom_Telephone]) == '') {
         $error.="Please enter your telephone number<br />";
              }else{
                 if(!eregi("^[0-9\ ]+$", $_POST[custom_Telephone])) {
                     $error="Please enter only numbers!";
                 }
                 
              }

   if(!isset($_POST['check'])) {
$error = "The checkbox isn't checked";
}


// I WANT THIS BIT OF CODE TO RUN BUT ONLY AFTER THE REST HAS VALIDATED, SORRY TO JUST DROP LINES OFF CODE IN I'VE TRIED SO MANY DIFFERENT METHODS AND I AM GETTING FRUSTRATED, CURRENTLY IT JUST INSTANTLY REDIRECTS AFTER SUBMIT TO PAYPAL. BUT I ONLY WANT THIS TO HAPPEN AFTER THE REST OFF THE FIELDS HAVE VALIDATED CAN SOMEONE TELL ME THE BEST METHOD TO DO THIS PLEASE.

   if(isset($_POST[code])==''){
    header('Location: [url=https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_s-xclick&[email protected]&item_name=Busines+Health+Check&hosted_button_id=1078955&firstname=']https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_s-xclick&[email protected]&item_name=Busines+Health+Check&hosted_button_id=1078955&firstname='[/url]);
   }else{
      header('location: [url=http://example.com]http://example.com[/url]);
   }
   


}

Link to comment
https://forums.phpfreaks.com/topic/181099-solved-validation/
Share on other sites

You could use elseif() or check to see if $error == ''

 

something like this (not tested)

<?php

if(isset($_POST['submit'])){
    
    $error = ''; // set error variable to blank

    if(trim($_POST[firstname]) == '') {
         $error.="Please enter you firstname<br />";
    }
    elseif(trim($_POST[lastname]) == '') {
         $error.="Please enter you lastname<br />";
    } 
    elseif(trim($_POST[email])==''){
        $error.="An email address is required!<br />"; 
    }
    elseif(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST[email])) { 
        $error.="The e-mail you entered was not in the proper format!";       
    }
    elseif(trim($_POST[custom_CompanyName]) == '') {
         $error.="Please enter your company name<br />";
    }
    elseif(trim($_POST[custom_CompanyAddress1]) == '') {
         $error.="Please enter your company address<br />";
    }
    elseif(trim($_POST[custom_Postcode]) == '') {
        $error.="Please enter your postcode<br />";
    }
    elseif(!eregi("^([Gg][ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2})$", $_POST[custom_Postcode])) {
        $error="Please enter a valid postcode!";
    }
    elseif(trim($_POST[custom_Telephone]) == '') {
        $error.="Please enter your telephone number<br />";
    }
    elseif(!eregi("^[0-9\ ]+$", $_POST[custom_Telephone])) {
        $error="Please enter only numbers!";
    }
    elseif(!isset($_POST['check'])) {
        $error = "The checkbox isn't checked";
    }
    else {

        // I WANT THIS BIT OF CODE TO RUN BUT ONLY AFTER THE REST HAS VALIDATED, SORRY TO JUST DROP LINES OFF CODE IN I'VE TRIED SO MANY DIFFERENT METHODS AND I AM GETTING FRUSTRATED, CURRENTLY IT JUST INSTANTLY REDIRECTS AFTER SUBMIT TO PAYPAL. BUT I ONLY WANT THIS TO HAPPEN AFTER THE REST OFF THE FIELDS HAVE VALIDATED CAN SOMEONE TELL ME THE BEST METHOD TO DO THIS PLEASE.
    
       if(isset($_POST[code])==''){
            header('Location: http://example.com');
       }else{
            header('location: http://example.com');
       }
   
   }


}

?>

 

And as this means you will only have one error message up at a time you could change the code to the following (again, not tested).

<?php

if(isset($_POST['submit'])){

    if(trim($_POST[firstname]) == '') {
         $error = "Please enter you firstname";
    }
    elseif(trim($_POST[lastname]) == '') {
         $error = "Please enter you lastname";
    } 
    elseif(trim($_POST[email])==''){
        $error = "An email address is required!"; 
    }
    elseif(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST[email])) { 
        $error = "The e-mail you entered was not in the proper format!";       
    }
    elseif(trim($_POST[custom_CompanyName]) == '') {
         $error = "Please enter your company name";
    }
    elseif(trim($_POST[custom_CompanyAddress1]) == '') {
         $error = "Please enter your company address";
    }
    elseif(trim($_POST[custom_Postcode]) == '') {
        $error = "Please enter your postcode";
    }
    elseif(!eregi("^([Gg][ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2})$", $_POST[custom_Postcode])) {
        $error = "Please enter a valid postcode!";
    }
    elseif(trim($_POST[custom_Telephone]) == '') {
        $error = "Please enter your telephone number";
    }
    elseif(!eregi("^[0-9\ ]+$", $_POST[custom_Telephone])) {
        $error = "Please enter only numbers!";
    }
    elseif(!isset($_POST['check'])) {
        $error = "The checkbox isn't checked";
    }
    
    
    if(!isset($error)) {

        // I WANT THIS BIT OF CODE TO RUN BUT ONLY AFTER THE REST HAS VALIDATED, SORRY TO JUST DROP LINES OFF CODE IN I'VE TRIED SO MANY DIFFERENT METHODS AND I AM GETTING FRUSTRATED, CURRENTLY IT JUST INSTANTLY REDIRECTS AFTER SUBMIT TO PAYPAL. BUT I ONLY WANT THIS TO HAPPEN AFTER THE REST OFF THE FIELDS HAVE VALIDATED CAN SOMEONE TELL ME THE BEST METHOD TO DO THIS PLEASE.
    
       if(isset($_POST[code])==''){
            header('Location: http://example.com');
       }else{
            header('location: http://example.com');
       }
   
   }

}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/181099-solved-validation/#findComment-955539
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.