在PHP邮件

时间:2015-05-26 05:53:21

标签: php html email post checkbox

此联系表单仅将复选框列表中的一项发送到我的电子邮箱。我想拥有它,以便将所有被检查的信息发送给我,并以逗号分隔列表(完成:天然油灯,天然油,...)

我已经包含了contact.html和send.php页面,我想知道是否有人可以帮助指导我正确的方向或只是修复代码。

contact.html:

<form action="send.php" method="post">

    <h3>Pick Your Oak:</h3>
        <input type="checkbox" name="oak[]" value="Certified French Oak" id="certified-french-oak"><label for="certified-french-oak">Certified French Oak</label><br><br>
        <input type="checkbox" name="oak[]" value="European Oak" id="european-oak"><label for="european-oak">European Oak</label><br><br>
        <input type="checkbox" name="oak[]" value="European White Oak" id="european-white-oak"><label for="european-white-oak">European White Oak</label><br><br>
        <input type="checkbox" name="oak[]" value="American Oak" id="american-oak"><label for="american-oak">American Oak</label><br><br>
        <input type="checkbox" name="oak[]" value="American White Oak" id="american-white-oak"><label for="american-white-oak">American White Oak</label><br><br>
        <input type="checkbox" name="oak[]" value="ThreeFourths Engineered" id="three-fourths-engineered"><label for="three-fourths-engineered">3/4" Engineered</label><br><br>

    <h3>Grade?</h3>
        <input type="checkbox" name="grade[]" value="Prime" id="prime"><label for="prime">Prime</label><br><br>
        <input type="checkbox" name="grade[]" value="Rustic" id="rustic"><label for="rustic">Rustic</label><br><br>

    <h3>Finish?</h3>
        <input type="checkbox" name="finish[]" value="Natural Oil light" id="natural-oil-light"><label for="natural-oil-light">Natural Oil light</label><br><br>
        <input type="checkbox" name="finish[]" value="Natural Oil natural" id="natural-oil-natural"><label for="natural-oil-natural">Natural Oil natural</label><br><br>
        <input type="checkbox" name="finish[]" value="Natural Oil white" id="natural-oil-white"><label for="natural-oil-white">Natural Oil white</label><br><br>
        <input type="checkbox" name="finish[]" value="Urethane Lacquer" id="urethane-lacquer"><label for="urethane-lacquer">Urethane Lacquer</label><br><br>
        <input type="checkbox" name="finish[]" value="Texturing Wire Brush" id="texturing-wire-brush"><label for="texturing-wire-brush">Texturing Wire Brush</label><br><br>
        <input type="checkbox" name="finish[]" value="Texturing Hand Scrape" id="texturing-hand-scrape"><label for="texturing-hand-scrape">Texturing Hand Scrape</label><br><br>
        <input type="checkbox" name="finish[]" value="Texturing Antique Distress" id="texturing-antique-distress"><label for="texturing-antique-distress">Texturing Antique Distress</label><br><br>
        <input type="checkbox" name="finish[]" value="Texturing Heavy Chatter Distress" id="texturing-heavy-chatter-distress"><label for="texturing-heavy-chatter-distress">Texturing Heavy Chatter Distress</label><br><br>
        <input type="checkbox" name="finish[]" value="Texturing Circle Saw Marks" id="texturing-circlresaw-marks"><label for="texturing-circlresaw-marks">Texturing Circle Saw Marks</label><br><br>

    <h3>Name</h3>
        <input type="text" name="yourname" id="yourname" placeholder="Full Name"><br>
        <input type="email" name="email" id="email" placeholder="Email"><br>

        <input type="Submit" class="button solid gold small center" style="height:40px;">FINISH!</a><br>
</form>

send.php:

<?php
/* Set e-mail recipient */
$myemail  = "myemail@gmail.com";

/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "You did not enter your name");
$email    = check_input($_POST['email']);
$oak = check_input($_POST['oak']);
$grade = check_input($_POST['grade']);
$finish = check_input($_POST['finish']);
$subject = "New Sample Request";

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    show_error("The e-mail address you entered is not valid.");
}

/* Let's prepare the message for the e-mail */
$message = "Hello!

A sample request form has been submitted by:

Name: $yourname
E-mail: $email

Oak types: $oak
Grades: $grade
Finishes: $finish

End of message
";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: sent.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>
<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="utf-8">

  <title>Error</title>

<script>
function goBack() {
    window.history.back()
}
</script>
  </head>

  <body>

<h4>Wait, an Error has Occurred</h4>
    <p>Please correct the following error:<br />
    <?php echo $myError; ?></div>

    <br />

    <p><button onclick="goBack()">Go Back</button>

  </body>

</html>
<?php
exit();
}
?>

3 个答案:

答案 0 :(得分:2)

多元素更改功能check_input

function check_input($data, $problem='')
{
if(is_array($data))
{
$data=implode(' ',$data);
}
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

答案 1 :(得分:0)

这里只传递一个值$oak = check_input($_POST['oak']);

使用$ oak []发送多个文本。

答案 2 :(得分:0)

使用这部分代码,以便在send.php中通过commma获取所有复选框值:

$oak = check_input($_POST['oak']);
$oak = implode("," $oak);
$grade = check_input($_POST['grade']);
$grade = implode("," $grade);
$finish = check_input($_POST['finish']);
$grade = implode("," $finish);