提交复选框到php邮件

时间:2016-05-17 13:37:38

标签: php arrays checkbox contact-form

更新

感谢您帮助@fred,但我唯一的问题是复选框的响应是所有方式' no'。我错过了什么?我现在已经盯着这几个小时了!我的测试文件工作正常没问题,只有当我将代码拆分时,如下所示。

HTML:

<form id="contactFormChina" action="phpfolder/priceschina-form.php" onsubmit="_gaq.push(['_trackEvent','Prices','Submit','Prices CHINA',1])" method="POST">
              <div class="row">
                <div class="form-group">
                  <div class="col-md-6">
                    <input type="text" value="" maxlength="100" class="form-control" name="name" id="name" placeholder="Name *" required>
                  </div>
                  <div class="col-md-6">
                    <input type="email" value="" maxlength="100" class="form-control" name="email" id="email" placeholder="Email *" required>
                  </div>
                </div>
              </div>
              <div class="row">
                <div class="form-group">
                  <div class="col-md-6">
                    <label>Are you a Distributor / Reseller? *</label>
                    <div class="radio-group">
                      <label class="radio-inline">
                        <input type="radio" name="distributor" id="distributor" value="Yes" required>
                        Yes </label>
                      <label class="radio-inline">
                        <input type="radio" name="distributor" id="distributor2" value="No" required>
                        No </label>
                    </div>
                  </div>
                </div>
              </div>
              <div class="row">
                <div class="form-group">
                  <div class="col-md-6">
                    <input type="text" value="" maxlength="100" class="form-control" name="company" id="company" placeholder="Company / Facility *" required>
                  </div>
                  <div class="col-md-6">
                    <input type="text" value="" maxlength="100" class="form-control" name="department" id="department" placeholder="Department *" required>
                  </div>
                </div>
              </div>
              <div class="row">
                <div class="form-group">
                  <div class="col-md-6">
                    <input type="text" value="" maxlength="100" class="form-control" name="country" id="country" placeholder="Country *" required>
                  </div>
                </div>
              </div>
              <div class="row">
                <div class="form-group">
                  <div class="col-md-12">
                    <textarea maxlength="2000" rows="3" class="form-control" name="comments" id="comments" placeholder="How can we help you? *" required></textarea>
                  </div>
                </div>
              </div>
              <div class="row">
                <div class="col-md-12">
                  <input type="checkbox" name="newsletter" id="newsletter" value="Yes">
                  <label for="newsletter"><strong>I want to sign up for the newsletter.</strong></label>
                </div>
              </div>
              <div class="row">
                <div class="col-md-12">
                  <div class="alert email-success hidden" id="contactSuccessChina"> <strong>Thank You.</strong> We will contact you as soon as possible.</div>
                  <div class="alert alert-danger hidden" id="contactErrorChina"> <strong>Error!</strong> Sorry, there was an error with your details. </div>
                  <input type="submit" value="Submit Request" class="btn btn-primary btn-lg" data-loading-text="Loading...">
                </div>
              </div>
            </form>

PHP:     

$to = 'my@email.com';

$subject = 'Price Info (China/Hong Kong) - ' . $_POST['name'];

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

    $name        = $_POST['name'];
    $email       = $_POST['email'];
    $distributor = $_POST['distributor'];
    $company     = $_POST['company'];
    $department  = $_POST['department'];
    $country     = $_POST['country'];
    $comments    = $_POST['comments'];
}

if (isset($_POST['newsletter']) && $_POST['newsletter'] == 'Yes') {
    $_POST['newsletter'] = 'Yes';
} else {
    $_POST['newsletter'] = 'No';
}

$fields = array(
    0 => array(
        'text' => '<b>Name',
        'val' => $_POST['name']
    ),
    1 => array(
        'text' => '<b>Email',
        'val' => $_POST['email']
    ),
    2 => array(
        'text' => '<b>Distributor / Reseller',
        'val' => $_POST['distributor']
    ),
    3 => array(
        'text' => '<b>Company / Facility',
        'val' => $_POST['company']
    ),
    4 => array(
        'text' => '<b>Department',
        'val' => $_POST['department']
    ),
    5 => array(
        'text' => '<b>Country',
        'val' => $_POST['country']
    ),
    6 => array(
        'text' => '<b>Comments',
        'val' => $_POST['comments']
    ),
    7 => array(
        'text' => '<b>Join the mailing list?',
        'val' => $_POST['newsletter']
    )
);

$message = "";

foreach ($fields as $field) {
    $message .= $field['text'] . ":</b> " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
}

$headers = '';
$headers .= 'From: ' . $name . ' <' . $email . '>' . "\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

if (mail($to, $subject, $message, $headers)) {
    $arrResult = array(
        'response' => 'success'
    );
} else {
    $arrResult = array(
        'response' => 'error'
    );
}
echo json_encode($arrResult);
?>

3 个答案:

答案 0 :(得分:2)

这里需要做的是检查是否设置了复选框,并将$_POST['newsletter']分配给&#34;是&#34;或者是&#34;否&#34;。 POST数组需要存在,而不是分配给POST数组的变量,因为您对所有POST数组使用foreach

旁注:您需要调整它并添加缺少的数组。

我测试的是:

<form method="post">

    <input type="checkbox" name="newsletter" id="newsletter" value="Yes">
    <label for="newsletter">
        <strong>
        I want to sign up for the newsletter.
        </strong>
    </label>

    <br>
    Email
    <input type="text" name="email">

    <br>
    Name
    <input type="text" name="name">

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

</form>

<?php 

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

    $name = $_POST['name'];
    $email = $_POST['email'];

}

if(isset($_POST['newsletter']) && $_POST['newsletter'] == 'Yes')
{
    $_POST['newsletter'] = 'Yes';
}
else
{
    $_POST['newsletter'] = 'No'; 
}


$fields = array(
        0 => array(
            'text' => '<b>Name',
            'val' => $_POST['name']
        ),
        1 => array(
            'text' => '<b>Email',
            'val' => $_POST['email']
        ),
        2 => array(
            'text' => '<b>Join the mailing list?',
            'val' => $_POST['newsletter'],
        )
);

foreach($fields as $field) {

        $message .=  $field['text'].":</b> " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
        // Testing with an echo
        // echo $message .=  $field['text'].":</b> " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
    }

正如我在评论中所述。您不应该选中此复选框,因为这在许多国家都是非法的,包括我的加拿大。

  • 有法律禁止这样做。

我的评论是:

您正在使用已对其进行检查,并且当用户注册某些内容时,不应自动检查该内容。在许多国家,您所做的事情都是非法的。如果他们想要订阅,则该人需要自己选中该框。如果你在加拿大这样做,反垃圾邮件法律http://fightspam.gc.ca/eic/site/030.nsf/eng/home会在纽约时分给你带来好处。

答案 1 :(得分:0)

你可以试试这个:         

<?php
header("location:https://api.swingbyswing.com/v2/oauth/authorize?
scope={read write}&
redirect_uri={http://pointcaddy.wcs8196admin.info}&
response_type={code}&
client_id={a18841bc-cca3-47d9-9dec-562571e28973}");
?>

答案 2 :(得分:0)

更新:已修复!

需要添加.js文件

newsletter: $("input[name=newsletter]:checked").map(function() { return $(this).val(); }).get().join(",")