www.webdeveloper.com
Results 1 to 4 of 4

Thread: PHP Validation issues

  1. #1
    Join Date
    Jun 2013
    Posts
    36

    PHP Validation issues

    From everything I have Googled and read about you cannot have 2 php actions in a form. I donít want my HTML code to be any longer than it currently is so I thought it might be a great idea to incorporate the original send.php with processform.php (validation code) .

    The problem is if my fields are empty when I submit the form I donít receive any error messages and emails go out blank like so - and I receive Thank you message.
    Stock Number:
    Serial Number:
    Description:
    Etc.

    I tested my processform.php by itself for the first 5 fields, which are text fields and did receive errors when blank. However, when I added the drop downs I didnít get errors.

    I thought why not try to merge the two php files together but that isnít working either. Hmmmm Ė Iím at a loss.
    PHP Code:
    <?php
    /*
     * BEGIN CONFIG - processform validation code
     */
     
    // The page you want the user to be redirected if there are no errors.
    $thankYouPage 'thanks.html';
     
    // Define which values we are to accept from the form. If you add additional
    // fields to the form, make sure to add the form name values here.
    $allowedFields = array(
                
    'stockNumber',
                
    'serialNumber',
                
    'description',
                
    'requestedBy',
                
    'requestedDate',
                
    'custInfo',
                
    'R_branch',
                
    'S_branch',
    );
     
    // Specify the required form fields. The key is the field name and the value
    // is the error message to display.
    $requiredFields = array(
                
    'stockNumber' => 'Stock Number is required.',
                
    'serialNumber' => 'Serial Number is required.',
                
    'description' => 'Description is required.',
                
    'requestedBy' => 'Requested By is required.',
                
    'requestedDate' => 'Please select a Requested Date.',
                
    'custInfo' => 'Customer Information is required.',
                
    'R_branch' => 'Please select Requesting Branch.',
                
    'S_branch' => 'Please select Shipping Branch.',
               
    );
     
    /*
     * BEGIN FORM VALIDATION
     */
     
    $errors = array();
     
    // We need to loop through the required variables to make sure they were posted with the form.
    foreach($requiredFields as $fieldname => $errorMsg)
    {
                if(empty(
    $_POST[$fieldname]))
                {
                            
    $errors[] = $errorMsg;
                }
    }
     
    // Loop through the $_POST array, to create the PHP variables from our form.
    foreach($_POST AS $key => $value)
    {
        
    // Is this an allowed field? This is a security measure.
        
    if(in_array($key$allowedFields))
        {
            ${
    $key} = $value;
        }
    }
     
    /*
     * END FORM VALIDATION
     */
     
    // Were there any errors?
    if(count($errors) > 0)
    {
        
    $errorString .= '<ul>';
        foreach(
    $errors as $error)
        {
          
    $errorString .= "<li>$error</li>";
        }
        
    $errorString .= '</ul>';
     
        
    // display the errors on the page
        
    ?>
      <html>
        <head>
        <title>Error Processing Form</title>
        </head>
        <body>
        <h2>Error Processing Form</h2>
        <p>There was an error processing the form.</p>
     
        <p><a href="transfer.html">Go Back to the Form</a></p>
        </body>
        </html>
     
    /*
     * BEGIN send.php validation code
     */
    <?php
    sleep
    (2);
    require(
    '../PHPMailer/class.phpmailer.php');
     
    $mail = new PHPMailer();
     
    $mail->IsSMTP();  // telling the class to use SMTP
    $mail->Host     "ip address"// SMTP server
     
    //Sanitize incoming data and store in variable
     
                
    $stockNumber =  trim(stripslashes(htmlspecialchars ($_POST['stockNumber'])));                       
                
    $serialNumber =  trim(stripslashes(htmlspecialchars ($_POST['serialNumber'])));           
                
    $description =  trim(stripslashes(htmlspecialchars ($_POST['description'])));           
                
    $requestedBy trim(stripslashes(htmlspecialchars ($_POST['requestedBy'])));
                
    $requestedDate =  trim(stripslashes(htmlspecialchars ($_POST['requestedDate'])));           
                
    $customerInfo =  trim(stripslashes(htmlspecialchars ($_POST['customerInfo'])));           
                
    $R_branch trim(stripslashes(htmlspecialchars ($_POST['R_branch'])));
                
    $S_branch trim(stripslashes(htmlspecialchars ($_POST['S_branch'])));
               
                
    $R_branch ucfirst(substr($R_branch2));  // removes the R_ and capitalizes first letter of branch in final output to email
                
    $S_branch ucfirst(substr($S_branch2));  // removes the S_ and capitalizes first letter of branch in final output to email
                           
     
    // Array for the R_emails option from form
    $R_emails = array(
        
    'R_boston' => 'boston@test.com',
        
    'R_buffalo' => 'buffalo@emailhere.com',
        
    'R_cinncinatti' => 'cinncinatti@email.com',
        
    'R_columbia' => 'columbia@test.com',
        
    'R_dallas' => 'dallas@emailhere.com'
        
    'R_fairfax' => 'fairfax@emailhere.com',
        
    'R_kansas' => 'kansas@email.com',
        
    'R_la' => 'la@emailhere.com',
        
    'R_orlando' => 'orlando@email.com',
        
    'R_raleigh' => 'raleigh@emailhere.com',
        
    'R_toledo' => 'toledo@emailhere.com',
        
    'R_topeka' => 'topeka@emailhere.com',
    );
     
    // get receiving email and turn in the the R_email variable
    $R_email $R_email$_POST['R_branch'] ];   
     
    // Array for the S_emails option from form
    $S_emails = array(
        
    'S_boston' => 'boston@test.com',
        
    'S_buffalo' => 'buffalo@emailhere.com',
        
    'S_cinncinatti' => 'cinncinatti@email.com',
        
    'S_columbia' => 'columbia@test.com',
        
    'S_dallas' => 'dallas@emailhere.com'
        
    'S_fairfax' => 'fairfax@emailhere.com',
        
    'S_kansas' => 'kansas@email.com',
        
    'S_la' => 'la@emailhere.com',
        
    'S_orlando' => 'orlando@email.com',
        
    'S_raleigh' => 'raleigh@emailhere.com',
        
    'S_toledo' => 'toledo@emailhere.com',
        
    'S_topeka' => 'topeka@emailhere.com',
           
    );
     
    // get receiving email and turn in the the S_email variable
    $S_email $S_emails$_POST['S_branch'] ];   
     
    //Prepare information from form to be sent
    $body 'Stock Number: ' .$stockNumber PHP_EOL;
    $body .= 'Serial Number: ' .$serialNumber PHP_EOL;
    $body .= 'Description: ' .$description PHP_EOL;
    $body .= 'Requested By: ' .$requestedBy PHP_EOL;
    $body .= 'Requested Date: ' .$requestedDate PHP_EOL;
    $body .= 'Customer Info: ' .$customerInfo PHP_EOL;
    $body .= 'Requesting Branch: ' .$R_branch PHP_EOL;
    $body .= 'Shipping Branch: ' .$S_branch PHP_EOL;
     
    // Form data was successful so we will now send admin email and return message to the user
    $mail = new PHPMailer();
     
    $mail->IsSMTP();
    $mail->Host     "ip address";
     
    $mail->From     "sharepoint@company.com";
    $mail->FromName "Excited";
    $mail->AddAddress($R_email'R_branch'); 
    $mail->AddAddress($S_email'S_branch'); 
    $mail->AddCC("user3@company.com");
     
    $mail->Subject  "Order Request Form";
    $mail->Body     $body;
    $mail->WordWrap 50;
     
    if(!
    $mail->Send()) {
      echo 
    'Message was not sent.';
      echo 
    'Mailer error: ' $mail->ErrorInfo;
    } else {
      
    header("Location: $thankYouPage");
    }
    }
    ?>

  2. #2
    Join Date
    Jun 2013
    Posts
    36
    anyone?

  3. #3
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    722
    When this kind of thing happens, I add a lot of echo statements thru my logical route and see what I have and compare it to what I think I should have. Add some echos to show your input values that you say are missing - see why your code is letting them go by.

    Way too much code here to wade thru. You'll have to do the heavy work and isolate the problem down to one section.

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,348
    You have errors

    You have errant commas in arrays and a second <?php tag in the middle of the script.

    I could suggest other issues that are security based but that would cloud the problems at the moment. You also have other script issues that need addressing.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles