简体   繁体   中英

jQuery Mobile PHP Form - Get Checked Radio Button Value

I am using a jQuery Mobile Form script I found online. Everything is working fine, except the input="radio" buttons. I am not a jQuery expert by any means, so I am hoping someone here can help me.

THE SETUP

The form script contains 3 files: send.php, contact.js, and the mobile markup. Examples of the problematic pieces of code are below:

Mobile Markup:

<div data-role="fieldcontain">
  <fieldset data-role="controlgroup" data-type="horizontal">
   <legend>Doctor Type:</legend>
   <input type="radio" name="doctortype" id="dmd" value="dmd"/>
    <label for="dd">D.D.</label>
   <input type="radio" name="doctortype" id="dds" value="dds" />
    <label for="do">D.O.</label>
  </fieldset>
</div>

Contact.js

$('#send-feedback').live("click", function() {
var url = 'api/send.php';
var error = 0;
var $contactpage = $(this).closest('.ui-page');
var $contactform = $(this).closest('.contact-form');
$('.required', $contactform).each(function (i) {
    if ($(this).val() === '') {
        error++;
    }
}); // each
if (error > 0) {
        alert('Please fill in all the mandatory fields. Mandatory fields are marked with an asterisk *.');
} else {
    var doctortype = $contactform.find('input[name="doctortype"]').val();
    var firstname = $contactform.find('input[name="firstname"]').val();
    var surname = $contactform.find('input[name="surname"]').val();
    var dob = $contactform.find('input[name="dob"]').val();
    var zip = $contactform.find('input[name="zip"]').val();
    var how = $contactform.find('select[name="how"]').val();
    var email = $contactform.find('input[name="email"]').val();
    var message = $contactform.find('textarea[name="message"]').val();  

    //submit the form
    $.ajax({
        type: "GET",
        url: url,
        data: {doctortype:doctortype, firstname:firstname, surname:surname, dob:dob, zip:zip, how:how, email:email, message:message},
        success: function (data) {
            if (data == 'success') {
                // show thank you
                $contactpage.find('.contact-thankyou').show();
                $contactpage.find('.contact-form').hide();
            }  else {
                alert('Unable to send your message. Please try again.');
            }
        }
    }); //$.ajax

}
return false;
});

Send.php

<?php
header('content-type: application/json; charset=utf-8');

if (isset($_GET["firstname"])) {
$doctortype = strip_tags($_GET['doctortype']);
$firstname = strip_tags($_GET['firstname']);
$surname = strip_tags($_GET['surname']);
$dob = strip_tags($_GET['dob']);
$zip = strip_tags($_GET['zip']);
$how = strip_tags($_GET['how']);
$email = strip_tags($_GET['email']);
$message = strip_tags($_GET['message']);
$header = "From: ". $firstname . " <" . $email . ">"; 

$ip = $_SERVER['REMOTE_ADDR'];
$httpref = $_SERVER['HTTP_REFERER'];
$httpagent = $_SERVER['HTTP_USER_AGENT'];
$today = date("F j, Y, g:i a");    

$recipient = 'MYEMAIL@gmail.com';
$subject = 'Contact Form';
$mailbody = "
Doctor Type: $doctortype
First Name: $firstname
Last Name: $surname
Date of Birth: $dob
Zip Code: $zip
How Did You Learn About Us: $how
Message: $message

IP: $ip
Browser info: $httpagent
Referral: $httpref
Sent: $today
";
$result = 'success';

if (mail($recipient, $subject, $mailbody, $header)) {
    echo json_encode($result);
}
}
?>

THE RUB

The form works fine by itself. User fills out info, clicks "Send", and I receive an email with the information. However, I am not able to get the value of the checked radio to parse and send.

I've spent numerous hours trying to get the checked radio value to pass through the necessary steps.

And therein lies the problem. I've looked at countless similar posts on SO and other various places online, including the jQuery Mobile documentation. To no avail.

Any help is much appreciated!

What is your jQuery doing? It seems unnecessary to do any jQuery/JavaScript. The form should send the value of your radio button to $_GET['doctortype'] without it (assuming your form method is get).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM