0

I am having trouble getting hold of which checkbox has been selected when using JSON.

This is my HTML

<form id="patronReg" action="" method="get" onsubmit="jsquicksub(this); return false;">
        <input type="checkbox" name="__gold" value="gold" id="patron-gold"/><strong>Gold patrons </strong>$3,000 (+GST for businesses)<br />
        <input type="checkbox" name="__silver" value="silver" id="patron-silver" /><strong>Silver patrons </strong>$1,500 (+GST for businesses)<br />
        <input type="checkbox" name="__families" value="families" id="patron-families" /><strong>Families and individual </strong>$500<br />

My JSON javascript file looks like this

function jsquicksub(po_form) {

if (gb_ajaxbusy)
    return false;

gb_ajaxbusy = true;


var lo_request = {};
lo_request.g = $(po_form).find('#patron-gold').val();
lo_request.s = $(po_form).find('#patron-silver').val();
lo_request.f = $(po_form).find('#patron-families').val();


$(po_form).fadeTo(200, 0.3);

$.getJSON('/patron-form-process', lo_request, function(po_result) {

    gb_ajaxbusy = false;
    $(po_form).fadeTo(0, 1);

    if (!po_result.success) {
        $('#errorReport').show();
        document.getElementById('errorReport').innerHTML = po_result.content;
        //alert(po_result.content);
        return false;
    }else{
        setTimeout(function() { 
                $.modal.close();
        }, 2500);
    }
    // replace form with thankyou message
    $(po_form).replaceWith(po_result.content);
    // empty errrReport element
    $('#errorReport').hide();

}); }

And my php form processing file is this

// function gets passed values after validation and returns a message to the user on screen
function jsonreturn($pb_success, $ps_content = null) {

$lo_result = (object) array('success'=>$pb_success, 'content'=>$ps_content);

ob_clean();
echo json_encode($lo_result);
exit;

} // end jsonreturn()
// membership type

$ps_gold = isset($_GET['g'])? $_GET['g']: null;
$ps_families = isset($_GET['f'])? $_GET['f']: null;
$ps_families = isset($_GET['f'])? $_GET['f']: null;

if ($ps_gold == 'gold') {
    $ps_membership = 'Gold patron';
}
elseif ($ps_silver == 'silver') {
    $ps_membership = 'Silver patron';
}
elseif ($ps_families == 'families') {
    $ps_membership = 'Families and individual';
}
else {
    $ps_membership = '';
}

So my question is, how to I get the selected checkbox value to the php file, when it goes from the form to the javascript file then to php. It is probably a very stupid question! But I am very new to JSON. Once I get to the php file, I am using phpmailer to send all values from my form in an email. It is working great! Except for the checkbox part!

Thanks for your help!

4

1 に答える 1

0

これを処理する 1 つの方法は、Jquery Checked Selectorを使用して、選択されているチェックボックスを渡すことです。例えば:

// ...
var lo_request = {};
lo_request.g = $(po_form).find('#patron-gold').is(':checked');
lo_request.s = $(po_form).find('#patron-silver').is(':checked');
lo_request.f = $(po_form).find('#patron-families').is(':checked');

$.getJSON('/patron-form-process', lo_request, function(po_result) {
// ...

次に、PHP 側から:

$ps_gold = isset($_GET['g'])? $_GET['g'] === 'true' : false;
$ps_silver = isset($_GET['s'])? $_GET['s'] === 'true' : false;
$ps_families = isset($_GET['f'])? $_GET['f'] === 'true' : false;

if ($ps_gold) {
    $ps_membership = 'Gold patron';
}
elseif ($ps_silver) {
    $ps_membership = 'Silver patron';
}
elseif ($ps_families) {
    $ps_membership = 'Families and individual';
}
else {
    $ps_membership = '';
}

ただし、各オプションは相互に排他的であるため、決定のロジックはチェックボックスよりもラジオ ボタンに適しているように思われることを指摘して$ps_membershipおきます。そうは言っても、上記のソリューションは必要なことを行うはずです。

于 2013-02-26T05:00:41.477 に答える