简体   繁体   中英

PHP Code for a large HTML Order Form to submit data to an email address

I have a large order form in html,and I want to be able to email the data to an email address. I have the below PHP script to send it, but it keeps going to a blank white screen or errors. Can anyone help me find what I am missing or need to change.

<?php
if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "myemail@gmail.com";
    $email_subject = "Dealer Order";


     $midnight = $_POST['midnight'];
     $midFiveSqFt = $_POST['midFiveSqFt'];
     $midThreeSqFt = $_POST['midThreeSqFt'];
     $midTotalSqFt = $_POST['midTotalSqFt'];
     $midPrice = $_POST['midPrice'];
         $midLineTotal = $_POST['midLineTotal'];

        $daynight = $_POST['daylight'];
    $dayFiveSqFt = $_POST['dayFiveSqFt'];
    $dayThreeSqFt = $_POST['dayThreeSqFt'];
    $dayTotalSqFt = $_POST['dayTotalSqFt'];
    $dayPrice = $_POST['dayPrice'];
    $dayLineTotal = $_POST['dayLineTotal'];

    $earthtone = $_POST['earthtone'];
    $earthFiveSqFt = $_POST['earthFiveSqFt'];
    $earthThreeSqFt = $_POST['earthThreeSqFt'];
    $earthTotalSqFt = $_POST['earthTotalSqFt'];
    $earthPrice = $_POST['earthPrice'];
    $earthLineTotal = $_POST['earthLineTotal'];

    $mixedBlend = $_POST['mixedBlend'];
    $mbFiveSqFt = $_POST['mbFiveSqFt'];
    $mbThreeSqFt = $_POST['mbThreeSqFt'];
    $mbTotalSqFt = $_POST['mbTotalSqFt'];
    $mbPrice = $_POST['mbPrice'];
    $mbLineTotal = $_POST['mbLineTotal'];

    $starryNight = $_POST['starryNight'];
    $snTotalSqFt = $_POST['snTotalSqFt'];
    $snPrice = $_POST['snPrice'];
    $snLineTotal = $_POST['snLineTotal'];

    $dayNight = $_POST['dayNight'];
    $dnTotalSqFt = $_POST['dnTotalSqFt'];
    $dnPrice = $_POST['dnPrice'];
    $dnLineTotal = $_POST['dnLineTotal'];

    $caramelstone = $_POST['caramelstone'];
    $carTotalSqFt = $_POST['carTotalSqFt'];
    $carPrice = $_POST['carPrice'];
    $carLineTotal = $_POST['carLineTotal'];

    $harvestBlend = $_POST['harvestBlend'];
    $hbTotalSqFt = $_POST['hbTotalSqFt'];
    $hbPrice = $_POST['hbPrice'];
    $hbLineTotal = $_POST['hbLineTotal'];

    $winterBlend = $_POST['winterBlend'];
    $wbTotalSqFt = $_POST['wbTotalSqFt'];
    $wbPrice = $_POST['wbPrice'];
    $wbLineTotal = $_POST['wbLineTotal'];

    $autumnBlend = $_POST['autumnBlend'];
    $abTotalSqFt = $_POST['abTotalSqFt'];
    $abPrice = $_POST['abPrice'];
    $abLineTotal = $_POST['abLineTotal'];

    $tiletotal = $_POST['tiletotal'];
    $tiledel = $_POST['tiledel'];

    $oneWick = $_POST['oneWick'];
    $owPrice = $_POST['owPrice'];
    $owLineTotal = $_POST['owLineTotal'];

    $twoWick = $_POST['twoWick'];
    $twPrice = $_POST['twPrice'];
    $twLineTotal = $_POST['twLineTotal'];

    $threeWick = $_POST['threeWick'];
    $thwPrice = $_POST['thwPrice'];
    $thwLineTotal = $_POST['thwLineTotal'];

    $sixx8tray = $_POST['sixx8tray'];
    $sixtryPrice = $_POST['sixtryPrice'];
    $sixtryLineTotal = $_POST['sixtryLineTotal'];

    $sevenx10tray = $_POST['sevenx10tray'];
    $svntryPrice = $_POST['svntryPrice'];
    $svntryLineTotal = $_POST['svntryLineTotal'];

    $silverStopper = $_POST['silverStopper'];
    $ssPrice = $_POST['ssPrice'];
    $ssLineTotal = $_POST['ssLineTotal'];

    $knife = $_POST['knife'];
    $kPrice = $_POST['kPrice'];
    $kLineTotal = $_POST['kLineTotal'];

    $spectotal = $_POST['spectotal'];
    $specdel = $_POST['specdel'];

    $over150_1 = $_POST['over150_1'];

    $grandtot = $_POST['grandtot'];

    $email_message = "Form details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Midnight: ".clean_string($midnight)."\n";
    $email_message .= "midFiveSqFt: ".clean_string($midFiveSqFt)."\n";
    $email_message .= "midThreeSqFt: ".clean_string($midThreeSqFt)."\n";
    $email_message .= "midTotalSqFt: ".clean_string($midTotalSqFt)."\n";
    $email_message .= "midPrice: ".clean_string($midPrice)."\n";
    $email_message .= "midLineTotal: ".clean_string($midLineTotal)."\n";

**(I know I need to finish the above for all the types)**


// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  

}
?>

I tested your code and have come to the following conclusion.

You have no variable set to $_POST['email'] so that will explain the blank screen, and there is no else conditional statement at the end/outside your code to show/tell you otherwise.

What you "probably" meant to check was to see if the "submit" button was set, as in if(isset($_POST['submit'])) { or add $email=$_POST['email']; under your conditional statement if you want to keep that statement intact.

I added this line at the end of your code: (to test)

else { echo "Sorry, Email variable is not set."; }

And if you were to change this line (to test) if(isset($_POST['email'])) { to if(!isset($_POST['email'])) { and run/execute it without your form, you will see that the mail will be sent, however it may end up in your SPAM box as it did for me, because there was no Email associated with the From:

NB: You are also suppressing errors using the @ symbol for your @mail() function.

So either add $email=$_POST['email']; after your opening conditional statement, or change it to if(isset($_POST['submit'])) { if you're checking with a "named" submit button.

Ie:

<input type="submit" name="submit" value="Submit">

<?php
// possible fix - uncomment
// $email=$_POST['email'];

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

// delete the above line and uncomment this line if checking submit instead
// if(isset($_POST['submit'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "myemail@gmail.com";
    $email_subject = "Dealer Order";


     $midnight = $_POST['midnight'];
     $midFiveSqFt = $_POST['midFiveSqFt'];
     $midThreeSqFt = $_POST['midThreeSqFt'];
     $midTotalSqFt = $_POST['midTotalSqFt'];
     $midPrice = $_POST['midPrice'];
         $midLineTotal = $_POST['midLineTotal'];

        $daynight = $_POST['daylight'];
    $dayFiveSqFt = $_POST['dayFiveSqFt'];
    $dayThreeSqFt = $_POST['dayThreeSqFt'];
    $dayTotalSqFt = $_POST['dayTotalSqFt'];
    $dayPrice = $_POST['dayPrice'];
    $dayLineTotal = $_POST['dayLineTotal'];

    $earthtone = $_POST['earthtone'];
    $earthFiveSqFt = $_POST['earthFiveSqFt'];
    $earthThreeSqFt = $_POST['earthThreeSqFt'];
    $earthTotalSqFt = $_POST['earthTotalSqFt'];
    $earthPrice = $_POST['earthPrice'];
    $earthLineTotal = $_POST['earthLineTotal'];

    $mixedBlend = $_POST['mixedBlend'];
    $mbFiveSqFt = $_POST['mbFiveSqFt'];
    $mbThreeSqFt = $_POST['mbThreeSqFt'];
    $mbTotalSqFt = $_POST['mbTotalSqFt'];
    $mbPrice = $_POST['mbPrice'];
    $mbLineTotal = $_POST['mbLineTotal'];

    $starryNight = $_POST['starryNight'];
    $snTotalSqFt = $_POST['snTotalSqFt'];
    $snPrice = $_POST['snPrice'];
    $snLineTotal = $_POST['snLineTotal'];

    $dayNight = $_POST['dayNight'];
    $dnTotalSqFt = $_POST['dnTotalSqFt'];
    $dnPrice = $_POST['dnPrice'];
    $dnLineTotal = $_POST['dnLineTotal'];

    $caramelstone = $_POST['caramelstone'];
    $carTotalSqFt = $_POST['carTotalSqFt'];
    $carPrice = $_POST['carPrice'];
    $carLineTotal = $_POST['carLineTotal'];

    $harvestBlend = $_POST['harvestBlend'];
    $hbTotalSqFt = $_POST['hbTotalSqFt'];
    $hbPrice = $_POST['hbPrice'];
    $hbLineTotal = $_POST['hbLineTotal'];

    $winterBlend = $_POST['winterBlend'];
    $wbTotalSqFt = $_POST['wbTotalSqFt'];
    $wbPrice = $_POST['wbPrice'];
    $wbLineTotal = $_POST['wbLineTotal'];

    $autumnBlend = $_POST['autumnBlend'];
    $abTotalSqFt = $_POST['abTotalSqFt'];
    $abPrice = $_POST['abPrice'];
    $abLineTotal = $_POST['abLineTotal'];

    $tiletotal = $_POST['tiletotal'];
    $tiledel = $_POST['tiledel'];

    $oneWick = $_POST['oneWick'];
    $owPrice = $_POST['owPrice'];
    $owLineTotal = $_POST['owLineTotal'];

    $twoWick = $_POST['twoWick'];
    $twPrice = $_POST['twPrice'];
    $twLineTotal = $_POST['twLineTotal'];

    $threeWick = $_POST['threeWick'];
    $thwPrice = $_POST['thwPrice'];
    $thwLineTotal = $_POST['thwLineTotal'];

    $sixx8tray = $_POST['sixx8tray'];
    $sixtryPrice = $_POST['sixtryPrice'];
    $sixtryLineTotal = $_POST['sixtryLineTotal'];

    $sevenx10tray = $_POST['sevenx10tray'];
    $svntryPrice = $_POST['svntryPrice'];
    $svntryLineTotal = $_POST['svntryLineTotal'];

    $silverStopper = $_POST['silverStopper'];
    $ssPrice = $_POST['ssPrice'];
    $ssLineTotal = $_POST['ssLineTotal'];

    $knife = $_POST['knife'];
    $kPrice = $_POST['kPrice'];
    $kLineTotal = $_POST['kLineTotal'];

    $spectotal = $_POST['spectotal'];
    $specdel = $_POST['specdel'];

    $over150_1 = $_POST['over150_1'];

    $grandtot = $_POST['grandtot'];

    $email_message = "Form details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Midnight: ".clean_string($midnight)."\n";
    $email_message .= "midFiveSqFt: ".clean_string($midFiveSqFt)."\n";
    $email_message .= "midThreeSqFt: ".clean_string($midThreeSqFt)."\n";
    $email_message .= "midTotalSqFt: ".clean_string($midTotalSqFt)."\n";
    $email_message .= "midPrice: ".clean_string($midPrice)."\n";
    $email_message .= "midLineTotal: ".clean_string($midLineTotal)."\n";


// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  

}

else { echo "Sorry, Email variable is not set."; }

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM