如何使用javascript在PHP中发送单选按钮值?

2022-08-30 16:12:37

我已经在这里呆了一个星期,阅读了所有的stackoverflow和谷歌,我无法解决这个问题。每次测试它时,无论我单击哪一个,我都会获得第一个单选按钮。

我在电子邮件中得到的是:

Name: test
Email: test@live.com
phone: 9545027557
type: Residential

网页:

<form name="controls" id="controls"  novalidate>
    <div class="control-group">
        <div class="controls">
            <input type="text" class="form-control" 
                   placeholder="Full Name" id="name" required
                   data-validation-required-message="Please enter your name" />
              <p class="help-block"></p>
        </div>
    </div>  
    <div class="control-group">
        <div class="controls">
            <input type="email" class="form-control" placeholder="Email" 
                   id="email" required
                   data-validation-required-message="Please enter your email" />
        </div>
    </div>  

    <div class="control-group">
        <div class="controls">
            <div class="btn-group" data-toggle="buttons" id="optionu" style="padding-left:25px"> 
                <label class="btn btn-primary active">
                    <input type="radio" class="form-control" id="optionu" name="optionu"
                           value="Residential" checked > Residential 
                </label>
                <label class="btn btn-primary ">
                    <input type="radio" class="form-control" id="optionu" name="optionu"
                           value="Commercial" > Commercial
                </label>
                <label class="btn btn-primary ">
                    <input type="radio" class="form-control" id="optionu" name="optionu"
                           value="Handyman" > Handyman
                </label>
            </div>
        </div>
    </div>
    <p></p>

    <div id="success"> </div> <!-- For success/fail messages -->
    <button type="submit" class="btn btn-danger pull-right">Request</button><br />
</form>

contact_me.js:

var name = $("input#name").val();  
var email = $("input#email").val(); 
var phone = $("input#phone").val(); 
var optionu = $("input#optionu").val();
var firstName = name; // For Success/Failure Message
// Check for white space in name for Success/Fail message
if (firstName.indexOf(' ') >= 0) {
    firstName = name.split(' ').slice(0, -1).join(' ');
}        
$.ajax({
    url: "bin/contact_me.php",
    type: "POST",
    data: {name: name, email: email,  optionu: optionu,  phone: phone},
    cache: false,
    success: function() {  
        // Success message
        $('#success').html("<div class='alert alert-success'>");
        $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
            .append( "</button>");
        $('#success > .alert-success')
            .append("<strong>Your message has been sent. </strong>");
        $('#success > .alert-success')
            .append('</div>');

contact_me.php:

$name = $_POST['name'];
$optionu_value = $_POST['optionu'];
$phone = $_POST['phone'];
$email_address = $_POST['email'];

// create email body and send it
$to = 'test@gmail.com'; // put your email
$email_subject = "Contact form submitted by:  $name";
$email_body = "You have received a new message. \n\n".
              " Here are the details:\n \nName: $name \n  ".
              "Email: $email_address\n phone: $phone\n  type: $optionu";

答案 1

首先,您使用相同的id 4次。ID在文档中必须是唯一的,您不能将相同的ID设置为4个元素。另外,我认为你从单选按钮获得价值的方式是错误的。

这或多或少是你如何从单选按钮获得价值:(来自另一个SO问题的例子 )

  var optionu = $('input[name=optionu]:checked', '#controls').val()

同时从所有输入[无线电]元素中删除id。

<label class="btn btn-primary active">
   <input type="radio" class="form-control" name="optionu" value="Residential" checked /> Residential 
 </label>
 <label class="btn btn-primary ">
   <input type="radio" class="form-control" name="optionu" value="Commercial" /> Commercial
 </label>
 <label class="btn btn-primary ">
   <input type="radio" class="form-control" name="optionu" value="Handyman" /> Handyman
 </label>

答案 2

您必须找到无线电的已检查值。您只是获得了第一个无线电元素的值。

我如何知道通过jQuery选择了哪个单选按钮?

编辑:您也不能对每个单选按钮使用相同的 ID。您必须为其指定一个类名,或者使用jQuery按名称引用按钮。