【问题标题】:Only one value from checkbox being sent in PHP mailPHP 邮件中仅发送复选框中的一个值
【发布时间】:2015-08-07 15:36:40
【问题描述】:

此联系表单仅将复选框列表中的一项发送到我的电子邮件。我想要它,以便将检查的所有内容发送给我,列表以逗号分隔(完成:Natural Oil light、Natural oil natural...)

我已经包含了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>

发送.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();
}
?>

【问题讨论】:

    标签: php html email post checkbox


    【解决方案1】:

    对于多元素更改函数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;
    }
    

    【讨论】:

    • 谢谢...这工作!!!如何在此处添加逗号? @Sourabh 下面的帖子似乎没有奏效。
    • $data=implode(',',$data);改变它
    • 谢谢你的朋友。你真棒。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 我上面用的是Qaisar Satti的代码,你上面写的代码好像不合适。还有其他想法吗?
      • 不使用 check_input 可以吗,如果可以,请尝试从 $_POST 中删除该函数,如果它有效,那么我将告诉您有关如何使用 chek_input 函数的另一种解决方案。
      • 我想保留 check_input
      猜你喜欢
      • 1970-01-01
      • 2014-07-09
      • 1970-01-01
      • 2015-09-13
      • 2014-06-22
      • 1970-01-01
      • 1970-01-01
      • 2018-08-17
      • 2014-02-23
      相关资源
      最近更新 更多