Php Jquery POST不使用输入类型复选框

Php Jquery POST不使用输入类型复选框,php,jquery,Php,Jquery,我已经构建了一个在php中运行良好的订阅表单,现在我已经将jquery应用于它以避免页面刷新 HTML表单 <input name="newsletter" type="checkbox" id="checkbox" value="true"> <input name="offer" type="checkbox" id="checkbox" value="true"> 当使用php时,它工作正常,我可以更改订阅类型,但jquery在这里不工作,它不会更改任何内容,并且

我已经构建了一个在php中运行良好的订阅表单,现在我已经将jquery应用于它以避免页面刷新

HTML表单

<input name="newsletter" type="checkbox" id="checkbox" value="true">
<input name="offer" type="checkbox" id="checkbox" value="true">
当使用php时,它工作正常,我可以更改订阅类型,但jquery在这里不工作,它不会更改任何内容,并且无论我选中或取消选中哪个复选框,我都会从post数据中得到警告,即
type1=1type2=1

请查看并建议使用jquery执行此操作的任何可能方法


谢谢

我想你是在尝试这样的事情:

if(newsletterval){
    // use the string representation 'true' rather than a boolean value
    newsletter='true';
};

if(offerval){
    // use the string representation 'true' rather than a boolean value
    offer='true';
};

$.post('../../subscribe.php', {newsletter:newsletter, offer:offer}, function(data){
    alert(data);            
});
php代码:

<?php

if(isset($_POST['newsletter']) 
  // Check the value of `$POST['newsletter']` rather than using isset() twice
  && $_POST['newsletter'] === 'true'){
    $newsletter = true;
}

HTTP将请求作为字符串传输,因此Javascripts
true
!=PHP的
true
您需要在PHP中使用字符串
'true'
'false'


由于PHP会将任何值的字符串等同于true,因此当前代码正在寻找它将始终找到的布尔值
true
,因此您将始终执行if块的truthy部分,而不会执行
else

仅当变量设置为
isset($\u POST['offer'])==true
。do
$\u POST['offer']==1
如何检查
isset($\u POST['offer'])
是否有值为
true
false
if(isset($\u POST['newsletter'])&$\u POST['newsletter'==true)@chepe263我试过了,但没用。如果不是从jquery发送true,而是发送一个数字呢?谢谢解释,收到了。
if(newsletterval){
    // use the string representation 'true' rather than a boolean value
    newsletter='true';
};

if(offerval){
    // use the string representation 'true' rather than a boolean value
    offer='true';
};

$.post('../../subscribe.php', {newsletter:newsletter, offer:offer}, function(data){
    alert(data);            
});
<?php

if(isset($_POST['newsletter']) 
  // Check the value of `$POST['newsletter']` rather than using isset() twice
  && $_POST['newsletter'] === 'true'){
    $newsletter = true;
}