ajax调用上的PHP sum复选框值

时间:2016-03-04 10:13:57

标签: php jquery html json ajax

我想通过ajax请求来核对复选框值。

[1] pry(main)> require "stripe"
=> true
[2] pry(main)> Stripe.api_key = "sk_test_..."
=> "sk_test_..."
[3] pry(main)> params = {"number"=>"4242424242424242", "cvc"=>"123", "expiry_month"=>"12", "expiry_year"=>""}
=> {"number"=>"4242424242424242",
 "cvc"=>"123",
 "expiry_month"=>"12",
 "expiry_year"=>""}
[4] pry(main)> token = Stripe::Token.create(:card => {
[4] pry(main)*     :number => params["number"],
[4] pry(main)*     :exp_month => params["expiry_month"],
[4] pry(main)*     :exp_year => params["expiry_year"],
[4] pry(main)* :cvc => params["cvc"]})
Stripe::CardError: Your card's expiration year is invalid.
from /usr/local/lib/ruby/gems/2.3.0/gems/stripe-1.36.0/lib/stripe.rb:310:in `handle_api_error'

我正在使用ajax表格序列化:

<form1>
<input type='checkbox' name='check_box[]' value='value01'>
</form1>
<form2>
<input type='checkbox' name='check_box[]' value='value02'>
</form2>
<form3>
<input type='checkbox' name='check_box[]' value='value03'>
</form3>

和php文件内容是:

 data: $(this.form).serialize(),
            url: 'file.php',
            dataType: 'json',
             ....

但是ajax会返回错误吗?可能因为是多种形式吗?

1 个答案:

答案 0 :(得分:2)

<form name="form1" id="form1">
<input type='checkbox' name='check_box[]' class="cb" value='1'>
</form>

<form name="form1" id="form1">
<input type='checkbox' name='check_box[]' class="cb" value='2'>
</form>

<form name="form1" id="form1">
<input type='checkbox' name='check_box[]' class="cb" value='3'>
</form>

你的jquery脚本: -

$(document).ready(function(){
    $(".cb").click(function(){

        var values = new Array();
        $.each($("input[name='check_box[]']:checked"), function() {
          values.push($(this).val());

        });

        //alert(values);

        $.ajax({url: "urphppage.php", 
        type:'POST',
        data:{ 'cbs':values},
        success: function(result){

             resp = JSON.parse(result);

             alert(resp.sum)

        }

        });
    });
});

你的php页面: -

$msg = array();

$sum=0;



foreach($_POST['cbs'] as $shipping) {

    $sum +=  $shipping;

}

$msg['sum'] = $sum;

echo json_encode($msg);