简体   繁体   中英

How to send html form input fields to email using php

I need help sending over form input information via PHP code to my email.I have included my PHP code but the input fields are not getting sent over to the email.Please advise.

    <form method="Post" id="form1" action="form-to-email.php" >
















    <legend>Time:11:30</legend>



    <fieldset id="fullname">




    <label for="name">Name</label>
    <input type="text" name="name" id="name" size="20" maxlength="50" />
    </fieldset>



    <fieldset id="NumberofAdults">

    <label for="Adults"># Adults</label>
    <input type="text" name="#ofadults" id="Adults" size="5" maxlength="2" />

    </fieldset>



    <fieldset id="Numberofkids">

    <label for="kids"># Kids</label>
    <input type="text" name="kids" id="kids" size="5" maxlength="2" />

    </fieldset>

    <fieldset id="infants">

        <label for="numberofinfants">Infants</label>
        <input type="text" name="numberofinfants" id="numberofinfants" size="5" maxlength="2"/>


    </fieldset>




        <fieldset id="tablenumber">

        <label for="table">Table #</label>
        <input type="text" name="table" id="table" size="5" maxlength="2"/>




        </fieldset>



        <fieldset id="Checknumber">

        <label for="check">Check #</label>
        <input type="text" name="check" id="check" size="5" maxlength="5"/>


        </fieldset>



        <fieldset id="M">

        <label for="misc">Misc.</label>
        <input type="text" name="Misc" id="Misc" size="75" maxlength="100"/>


        </fieldset>
        <fieldset id="email">

    <label for="Email">Email</label>
    <input type="text" name="Email" id="Email" size="5" maxlength="45" />

    </fieldset>


    <fieldset id="Total">
        <label for="adulttotal">Total Adults</label>
        <input type="text" id="adulttotal" value="0" name="adulttotal" readonly/>
        <br/>
        <label for="kidstotal">Total Kids</label>
        <input type="text" id="kidstotal" value="0" name="kidstotal" readonly/>
        <br/>
        <label for="infanttotal">Total Infants</label>
        <input type="text" id="infanttotal" value="0" name="infantstotal" readonly />
        </fieldset>





    <legend>Time:12:00</legend>



    <fieldset id="fullname">




    <label for="Name">Name</label>
    <input type="text" name="name" id="name" size="20" maxlength="50" />
    </fieldset>



    <fieldset id="NumberofAdults">

    <label for="#ofadults"># Adults</label>
    <input type="text" name="#ofadults" id="Adults" size="5" maxlength="2" />

    </fieldset>



    <fieldset id="Numberofkids">

    <label for="kids"># Kids</label>
    <input type="text" name="kids" id="kids" size="5" maxlength="2" />

    </fieldset>


    <fieldset id="infants">

        <label for="numberofinfants">Infants</label>
        <input type="text" name="numberofinfants" id="numberofinfants" size="5" maxlength="2"/>


    </fieldset>




        <fieldset id="tablenumber">

        <label for="table">Table #</label>
        <input type="text" name="table" id="table" size="5" maxlength="2"/>




        </fieldset>



        <fieldset id="Checknumber">

        <label for="check">Check #</label>
        <input type="text" name="check" id="check" size="5" maxlength="5"/>


        </fieldset>



        <fieldset id="M">

        <label for="misc">Misc.</label>
        <input type="text" name="Misc" id="Misc" size="75" maxlength="100"/>


        </fieldset>

        <fieldset id="email">

    <label for="Email">Email</label>
    <input type="text" name="Email" id="Email" size="5" maxlength="45" />

    </fieldset>


    <fieldset id="Total">
        <label for="adulttotal">Total Adults</label>
        <input type="text" id="adulttotal" value="0" name="adulttotal" readonly/>
        <br/>
        <label for="kidstotal">Total Kids</label>
        <input type="text" id="kidstotal" value="0" name="kidstotal" readonly/>
        <br/>
        <label for="infanttotal">Total Infants</label>
        <input type="text" id="infanttotal" value="0" name="infantstotal" readonly />
        </fieldset>



    <legend>Time:12:30</legend>



    <fieldset id="fullname">




    <label for="Name">Name</label>
    <input type="text" name="name" id="name" size="20" maxlength="50" />
    </fieldset>



    <fieldset id="NumberofAdults">

    <label for="#ofadults"># Adults</label>
    <input type="text" name="#ofadults" id="Adults" size="5" maxlength="2" />

    </fieldset>



    <fieldset id="Numberofkids">

    <label for="kids"># Kids</label>
    <input type="text" name="kids" id="kids" size="5" maxlength="2" />

    </fieldset>


    <fieldset id="infants">

        <label for="numberofinfants">Infants</label>
        <input type="text" name="numberofinfants" id="numberofinfants" size="5" maxlength="2"/>


    </fieldset>




        <fieldset id="tablenumber">

        <label for="table">Table #</label>
        <input type="text" name="table" id="table" size="5" maxlength="2"/>




        </fieldset>



        <fieldset id="Checknumber">

        <label for="check">Check #</label>
        <input type="text" name="check" id="check" size="5" maxlength="5"/>


        </fieldset>



        <fieldset id="M">

        <label for="misc">Misc.</label>
        <input type="text" name="Misc" id="Misc" size="75" maxlength="100"/>


        </fieldset>

    <fieldset id="email">

    <label for="Email">Email</label>
    <input type="text" name="Email" id="Email" size="5" maxlength="45" />

    </fieldset>


    <fieldset id="Total">
        <label for="adulttotal">Total Adults</label>
        <input type="text" id="adulttotal" value="0" name="adulttotal" readonly/>
        <br/>
        <label for="kidstotal">Total Kids</label>
        <input type="text" id="kidstotal" value="0" name="kidstotal" readonly/>
        <br/>
        <label for="infanttotal">Total Infants</label>
        <input type="text" id="infanttotal" value="0" name="infantstotal" readonly />
        </fieldset>







    <legend>Time:1:00</legend>



    <fieldset id="fullname">




    <label for="Name">Name</label>
    <input type="text" name="name" id="name" size="20" maxlength="50" />
    </fieldset>



    <fieldset id="NumberofAdults">

    <label for="#ofadults"># Adults</label>
    <input type="text" name="#ofadults" id="Adults" size="5" maxlength="2" />

    </fieldset>



    <fieldset id="Numberofkids">

    <label for="kids"># Kids</label>
    <input type="text" name="kids" id="kids" size="5" maxlength="2" />

    </fieldset>


    <fieldset id="infants">

        <label for="numberofinfants">Infants</label>
        <input type="text" name="numberofinfants" id="numberofinfants" size="5" maxlength="2"/>


    </fieldset>




        <fieldset id="tablenumber">

        <label for="table">Table #</label>
        <input type="text" name="table" id="table" size="5" maxlength="2"/>




        </fieldset>



        <fieldset id="Checknumber">

        <label for="check">Check #</label>
        <input type="text" name="check" id="check" size="5" maxlength="5"/>


        </fieldset>



        <fieldset id="M">

        <label for="Misc">Misc.</label>
        <input type="text" name="Misc" id="Misc" size="75" maxlength="100"/>


        </fieldset>

        <fieldset id="email">

    <label for="Email">Email</label>
    <input type="text" name="Email" id="Email" size="5" maxlength="45" />

    </fieldset>


    <fieldset id="Total">
        <label for="adulttotal">Total Adults</label>
        <input type="text" id="adulttotal" value="0" name="adulttotal" readonly/>
        <br/>
        <label for="kidstotal">Total Kids</label>
        <input type="text" id="kidstotal" value="0" name="kidstotal" readonly/>
        <br/>
        <label for="infanttotal">Total Infants</label>
        <input type="text" id="infanttotal" value="0" name="infantstotal" readonly />
        </fieldset>










    <legend>Time:1:30</legend>



    <fieldset id="fullname">




    <label for="Name">Name</label>
    <input type="text" name="name" id="name" size="20" maxlength="50" />
    </fieldset>



    <fieldset id="NumberofAdults">

    <label for="#ofadults"># Adults</label>
    <input type="text" name="#ofadults" id="Adults" size="5" maxlength="2" />

    </fieldset>



    <fieldset id="Numberofkids">

    <label for="kids"># Kids</label>
    <input type="text" name="kids" id="kids" size="5" maxlength="2" />

    </fieldset>


    <fieldset id="infants">

        <label for="numberofinfants">Infants</label>
        <input type="text" name="numberofinfants" id="numberofinfants" size="5" maxlength="2"/>


    </fieldset>




        <fieldset id="tablenumber">

        <label for="table">Table #</label>
        <input type="text" name="table" id="table" size="5" maxlength="2"/>




        </fieldset>



        <fieldset id="Checknumber">

        <label for="check">Check #</label>
        <input type="text" name="check" id="check" size="5" maxlength="5"/>


        </fieldset>



        <fieldset id="M">

        <label for="misc">Misc.</label>
        <input type="text" name="Misc" id="Misc" size="75" maxlength="100"/>


        </fieldset>

        <fieldset id="email">

    <label for="Email">Email</label>
    <input type="text" name="Email" id="Email" size="5" maxlength="45" />

    </fieldset>


    <fieldset id="Total">
        <label for="adulttotal">Total Adults</label>
        <input type="text" id="adulttotal" value="0" name="adulttotal" readonly/>
        <br/>
        <label for="kidstotal">Total Kids</label>
        <input type="text" id="kidstotal" value="0" name="kidstotal" readonly/>
        <br/>
        <label for="infanttotal">Total Infants</label>
        <input type="text" id="infanttotal" value="0" name="infantstotal" readonly />
        </fieldset>






    <legend>Time:2:00</legend>



    <fieldset id="fullname">




    <label for="name">Name</label>
    <input type="text" name="name" id="name" size="20" maxlength="50" />
    </fieldset>



    <fieldset id="NumberofAdults">

    <label for="#ofadults"># Adults</label>
    <input type="text" name="#ofadults" id="Adults" size="5" maxlength="2" />

    </fieldset>



    <fieldset id="Numberofkids">

    <label for="kids"># Kids</label>
    <input type="text" name="kids" id="kids" size="5" maxlength="2" />

    </fieldset>


    <fieldset id="infants">

        <label for="numberofinfants">Infants</label>
        <input type="text" name="numberofinfants" id="numberofinfants" size="5" maxlength="2"/>


    </fieldset>




        <fieldset id="tablenumber">

        <label for="table">Table #</label>
        <input type="text" name="table" id="table" size="5" maxlength="2"/>




        </fieldset>



        <fieldset id="Checknumber">

        <label for="check">Check #</label>
        <input type="text" name="check" id="check" size="5" maxlength="5"/>


        </fieldset>



        <fieldset id="M">

        <label for="Misc">Misc.</label>
        <input type="text" name="Misc" id="Misc" size="75" maxlength="100"/>


        </fieldset>

        <fieldset id="email">

    <label for="Email">Email</label>
    <input type="text" name="Email" id="Email" size="5" maxlength="45" />

    </fieldset>


    <fieldset id="Total">
        <label for="adulttotal">Total Adults</label>
        <input type="text" id="adulttotal" value="0" name="adulttotal" readonly/>
        <br/>
        <label for="kidstotal">Total Kids</label>
        <input type="text" id="kidstotal" value="0" name="kidstotal" readonly/>
        <br/>
        <label for="infanttotal">Total Infants</label>
        <input type="text" id="infanttotal" value="0" name="infantstotal" readonly />
        </fieldset>












      <p>


      <input id="Cancel" type="reset" value="Cancel"/>
      <input id="Submit" type="Submit" name="submit1" value="Submit the Form"/>
      </p>

</form>

CSS File

     /* Footer Styles */
footer {
    border - top: 1px solid red;
    background - color: rgb(255, 191, 165);
    clear: left;
    margin - top: 20px;
    padding: 10px 0px;
}
header {
    float: left;
    width: 100 % ;
    background: url(Bethwoodlogo.jpg) bottom right no - repeat; - o - background - size: contain; - moz - background - size: contain; - webkit - background - size: contain;
    background - size: contain;
    background - color: rgb(255, 191, 165);
    border - bottom: 1px solid red;
}
header img {
    display: block;
    float: left;
    width: 100 % ;
}
footer address {
    color: rgb(233, 69, 0);
    font - style: normal;
    font - size: 0.8em;
    text - align: center;
}
form {
    border: solid rgb(135, 206, 250) 5px;
    width: 100 % ;
    length: 100 % ;
}
label {
    width: 100 % ;
    length: 100 % ;
    Background - color: rgb(135, 206, 250);
    clear: both;
    border: solid #666 1px; float:left; text-align:center; }    # fullname {
        float: left;
        margin: 10x 0px 10px 2.5 % ;
        width: 10 % ;
    }#
    NumberofAdults {
        width: 5 % ;
        float: left;
    }#
    Numberofkids {
        width: 5 % ;
        float: left;
    }#
    infants {
        width: 5 % ;
        float: left;
    }#
    Checknumber {
        float: left;
        width: 5 % ;
    }#
    M {
        float: left;
    }#
    tablenumber {
        width: 5 % ;
        float: left;
    }
    legend {
        background - color: rgb(233, 69, 0);
        color: white;
        padding: 3px 0px;
        text - indent: 5px;
        width: 100 % ;
    }
    input {
        float: left;
        display: block;
    }#
    Cancel {
        p {
            float: left;
        }

Javascript file

    $('form fieldset').not('#Total').delegate('input', 'focus', foo);


function foo(e) {

    if ($(this).is(':last-child')) {

        e.stopPropagation();



        $('<br/>').insertAfter($(this));
        var cloneLabel = $(this).prev().clone();
        var cloneInput = $(this).clone().val("");

        cloneLabel.insertAfter($(this).next());
        cloneInput.insertAfter($(this).next().next());

        removeEmpty($(this));

        total($(this).parent().nextAll('#Total:first'));
    }

}

function removeEmpty(obj) {
    obj.parent().siblings('fieldset').not('#Total').each(function () {
        if ($(this).children('input').length > 1) {

            var count = 1;
            var removeFirst = 0;
            $(this).children('input').each(function () {

                if ($(this).val().length > 0 && removeFirst == 0) {
                    if ($(this).parent().children('input:first').val().length == 0) {
                        $(this).parent().children('input:first').prev().remove();
                        $(this).parent().children('input:first').next().remove()
                        $(this).parent().children('input:first').remove();
                        removeFirst = 1;
                    }
                }

                if ($(this).val().length == 0 && count != 1) {


                    $(this).prev().prev().remove();
                    $(this).prev().remove();
                    $(this).remove();

                }

                count = 2;

            })
        }
    })

}

function total(obj) {

    var totalAdults = 0,
        totalKids = 0,
        totalInfants = 0;

    obj.prevAll('#NumberofAdults:first').children('input').each(function () {
        if (!isNaN(parseInt($(this).val(), 10))) totalAdults += parseInt($(this).val(), 10)

    });

    obj.prevAll('#Numberofkids:first').children('input').each(function () {
        if (!isNaN(parseInt($(this).val(), 10))) totalKids += parseInt($(this).val(), 10)
    });

    obj.prevAll('#infants:first').children('input').each(function () {

        if (!isNaN(parseInt($(this).val(), 10))) totalInfants += parseInt($(this).val(), 10)
    });

    obj.children('#adulttotal').val(totalAdults);
    obj.children('#kidstotal').val(totalKids);
    obj.children('#infanttotal').val(totalInfants);
}


$('#Cancel').click(function (e) {
    e.preventDefault();
    $('#form1')[0].reset();
    removeEmpty($(this));

});

$('#Submit').click(function (e) {
    e.preventDefault();
    removeEmpty($(this));
    $('#form1').children('#Total').each(function () {
        total($(this));
    });
    $('#form1').submit();
});

PHP file

  <?php
if(!isset($_POST['submit1']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$Adults = $_POST['#ofadults'];
$kids = $_POST['kids'];
$numberofinfants = $_POST['numberofinfants'];
$table = $_POST['table'];
$check = $_POST['check'];
$Misc = $_POST['Misc'];
$adulttotal = $_POST['adulttotal'];
$kidstotal= $_POST['kidstotal'];
$infanttotal = $_POST['infantstotal'];
$Email = $_POST['Email'];


if(IsInjected($Email))
{
    echo "Bad email value!";
    exit;
}

$to = 'jac79@njit.edu';
$subject = 'sending html form in message';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: test@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<h1>add form html</h1>'; // add the form html
$message .= '</body></html>';
//done. redirect to thank-you page.
header('Location: thank-you.html');




// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}

?> 

Your <form> , lacking a method="post" , sends as GET .

Thus, lines like $name = $_POST['name']; will fail to get anything out of $_POST , since it's empty.

You should strongly consider using something like http://swiftmailer.org/ instead of mail() - it'll handle header injections much more reliably than your attempt at an IsInjected function.

Develop your emailer using html css files. After that conver the external css to inline, because external css doesnot work on external css. js Script does not work with email because the gmail or any mail service provider will trim that from code to secure the code or prevent from spam. use the following code to mail

 $to = 'test@example.com'; $subject = 'sending html form in message'; $headers = "From: " . strip_tags($_POST['req-email']) . "\\r\\n"; $headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\\r\\n"; $headers .= "CC: test@example.com\\r\\n"; $headers .= "MIME-Version: 1.0\\r\\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\\r\\n"; $message = '<html><body>'; $message .= '<h1>add form html</h1>'; // add the form html $message .= '</body></html>'; mail($to, $subject, $message, $headers); 

http://quandaflow.com/

ceejayoz是正确的,而且您在mail()调用中也缺少$ from字段= $ email_from。

This a sample of a currently working contact form hope it helps!

First in your html add this to your form tag

HTML

<form method="POST" id="form1" action="form-to-email.php">
</form>

The action let's the form process, even if javascript is disabled or jQuery is not active, specifing POST in method changes from the default GET to POST

Javascript

  $(document).ready(function() {
    $('#form1').submit(function(e) {
      $.ajax({
        type: 'POST',
        url: 'form-to-email.php',
        data: $(this).serialize(),
        dataType: "json"
      })
      .done(function(msg) {
        if (msg.success == true) {
          console.log(msg)
        }
        else {
          console.log(msg)
        }
      })
      e.preventDefault();
    });
  });

PHP

<?php
  $name = $_POST['name'];
  $email = $_POST['email'];
  $msg = $_POST['message'];

  // Array to hold validation errors and response data.
  $errors = array();
  $data = array();

 // Validation
 $nospace_name = trim($_POST['name']);
 $nospace_email = trim($_POST['email']);
 $nospace_message = trim($_POST['message']);

 if (empty($nospace_name))
   $errors['name'] = "Name field is required.";

 if (empty($nospace_email))
   $errors['email'] = "Email field is required.";

 if (empty($nospace_message))
   $errors['message'] = "I would love to see your message.";

 if (!empty($nospace_email) && !preg_match("^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$^", $nospace_email))
   $errors['bad_email'] = "Please enter a valid email address.";

 if (!empty($errors)) {
   $data['success'] = false;
   $data['errors']  = $errors;
 }
 else {
   $to = "info@example.com";
   $subject = "Website Contact Form: " . $name;

   $headers  = "From: noreply@example.me\r\n";
   $headers .= 'MIME-Version: 1.0' . "\r\n";
   $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
   $headers .= "X-Priority: 1\r\n";
   $headers .= "Reply-To: " . $email;

   // add structure to email
   $message = '<html><body>';
   $message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
   $message .= "<tr><td><strong>Email:</strong> </td><td>" . $email . "</td></tr>";
   $message .= "<tr><td><strong>Message:</strong> </td><td>" . $msg . "</td></tr>";
   $message .= "</table>";
   $message .= "</body></html>";

   $mailSent = mail($to, $subject, $message, $headers);

   if (!$mailSent) {
     $errors['message'] .= "Something went wrong...Please try again later";
     $data['success'] = false;
     $data['errors']  = $errors;
   }
   else {
    $data['success'] = true;
    $data['message'] = "Thank you for contacting me, I\'ll get back to you soon!";
  }
 }

 // return all our data to an AJAX call
 echo json_encode($data);
?>

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