将单选按钮中的值发布到电子邮件中

时间:2015-03-16 10:34:04

标签: javascript php html email post

我正在尝试从单选按钮向电子邮件发送值。这个想法是为了邀请,所以我正在寻找单选按钮值和电子邮件地址。

我到目前为止似乎成功地将值和电子邮件发送到我的收件箱,但是,无论用户选择什么,它似乎只发送第一个选项。

此外,在提交电子邮件和值时,我收到错误提示“发生错误(2)”,但值仍然在我的收件箱中结束,因此我不确定为什么会这样做。

<div id="invitation">
<div class="container invitation">
    <div id="successInv" class="alert alert-success invisible">
    <strong>Thank you!</strong> We'll be in touch with more details.
    </div>
    <div class="row-fluid">
        <div class="span7 center">
            <form class="inline-form">
                <input id="option1" type="radio" name="type" class="span1" value="op1" checked>
                <label for="option1">Option 1</label>
                <input id="option2" type="radio" name="type" class="span1" value="op2">
                <label for="option2">Option 2</label>
                <input id="option3" type="radio" name="type" class="span1" value="op3">
                <label for="option3">Option 3</label>
                <input type="email" name="email" id="invmail" class="span8" placeholder="Enter your email for an exclusive invitation" required />
                <button id="invitation" class="button button-inv">Request invitation</button>
            </form>
            <div id="err-inv" class="error centered">Please provide a valid email address.</div>
        </div>
    </div>
</div>

JS下面是:

 $('#invitation').click(function () {

var error = false;
    var emailCompare = /^([a-z0-9_.-]+)@([0-9a-z.-]+).([a-z.]{2,6})$/;
    var email = $('input#nlmail').val().toLowerCase();
    if (email == "" || email == " " || !emailCompare.test(email)) {
        $('#err-inv').show(500);
        $('#err-inv').delay(4000);
        $('#err-inv').animate({
            height: 'toggle'
        }, 500, function () {
        });
        error = true;
    }

    if (error === false) {
        $.ajax({
            type: 'POST',
            url: 'php/invitation.php',

            data: {
                email: $('#invmail').val(),
                type: $('#type').val()
            },
            error: function (request, error) {
                alert("An error occurred (1)");
            },
            success: function (response) {
                if (response == 'OK') {
                    $('#successInv').show();
                    $('#invmail').val('')
                } else {
                    alert("An error occurred (2)");
                }
            }
        });
        return false;
    }
    return false;
});
PHP下面是:

<?php
include 'functions.php';
    if (!empty($_POST)){
  $data['success'] = true;
  $_POST  = multiDimensionalArrayMap('cleanEvilTags', $_POST);
  $_POST  = multiDimensionalArrayMap('cleanData', $_POST);
  $emailTo ="yourmail@yoursite.co.uk";
  $emailFrom ="contact@yoursite.co.uk";
  $emailSubject = "New invitation request";
  $email = $_POST["email"];
  $type = $_POST["type"];
      if (!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email))
        $data['success'] = false;

  if($data['success'] == true){
    $message = "Email: $email - Type: $type";
    $headers = "MIME-Version: 1.0" . "\r\n"; 
    $headers .= "Content-type:text/html; charset=utf-8" . "\r\n"; 
    $headers .= "From: <$emailFrom>" . "\r\n";
    mail($emailTo, $emailSubject, $message, $headers);
    $data['success'] = true;
    echo json_encode($data);
  }
}

非常感谢任何帮助,以帮助我识别为什么它只发送一个原始检查的价值。

2 个答案:

答案 0 :(得分:1)

 <input class='radioval' type="radio" name="type" class="span1" value="op1">
 <label for="option1">Option 1</label>
   <input class='radioval' type="radio" name="type" class="span1" value="op2">
    <label for="option2">Option 2</label>
   <input class='radioval' type="radio" name="type" class="span1" value="op3">
     <label for="option3">Option 3</label>

在你的AJAX电话中,改变

data: {
                email: $('#invmail').val(),
                type: $('.radioval').is(':checked').val()
            },

答案 1 :(得分:0)

试试

success: function (data) {
                if (data.success) {
                    $('#successInv').show();
                    $('#invmail').val('')
                } else {
                    alert("An error occurred (2)");
                }
            }