Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过ajax、php、jquery传递checkbox的值_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 通过ajax、php、jquery传递checkbox的值

Javascript 通过ajax、php、jquery传递checkbox的值,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,你好。伙计们,我有个问题。 我需要通过ajax将复选框值发送到php文件 我的问题 无论是否检查,我都希望在这两种情况下传递值。 如果选中,则应通过value=“true” 如果不是check,则只传递内容中的值。通过ajax jquery 注意 我知道jquery(AJAX)文件中的代码应该用if条件更新,但我不知道怎么做 更新---- 我已经通过以下回答解决了支票条件的问题 但是 在wallajax.php文件中使用if(isset($\u POST[“content”]) 然后,我通过aj

你好。伙计们,我有个问题。 我需要通过ajax将复选框值发送到php文件

我的问题 无论是否检查,我都希望在这两种情况下传递值。 如果选中,则应通过
value=“true”
如果不是check,则只传递内容中的值。通过ajax jquery

注意

我知道jquery(AJAX)文件中的代码应该用
if条件更新,但我不知道怎么做

更新----

我已经通过以下回答解决了支票条件的问题

但是

在wallajax.php文件中使用
if(isset($\u POST[“content”])
然后,我通过ajax获得内容的价值,它也在页面中重现其价值

但是当我使用
if(isset($\u POST[“content”])和&isset($\u POST[“check”])时,
没有检索到值,因此第页上没有显示返回值

我不明白,问题出在哪里,是在ajax脚本上还是在wallajax.php文件上

我的密码

<script type="text/javascript">
$(function() {

$("#px").click(function() {
    var status2 = $("#status2").val();
     var checkit= $("#checkit").is(':checked');
       var dataString = 'content=' + status2 + '&check=' + checkit;
        $.ajax({
        type: "POST",
        url: "wallajax.php",
        data: dataString,
        cache: false,
        success: function(resol) {
        $('#wallposts').prepend(resol);
        console.log(resol)
              }
        }); return false;
    });

});
</script>
index.php

<textarea name="content" id="status2"></textarea>
<input name="check" id="checkit" type="checkbox" value="true">check it</input>
<div id="px">Post</div>

检查一下
邮递
而不是

$("#checkit").val();
使用


我建议

var data = {'content': status2, 'check': checkit};
而不是

var dataString = 'content=' + status2 + '&check=' + checkit;

您有两个问题,您的数据字符串在变量之间不包含“&”,并且复选框值未被正确捕获,下面修复了这两个问题:

<script type="text/javascript">
$(function() {

$("#px").click(function() {
    var status2 = $("#status2").val();
     var checkit= $("#checkit").prop('checked');
       var dataString = 'content=' + status2 + '&check=' + checkit;
        $.ajax({
        type: "POST",
        url: "wallajax.php",
        data: dataString,
        cache: false,
        success: function(resol) {
        $('#wallposts').prepend(resol);
        console.log(resol)
              }
        }); return false;
    });

});
</script>
将始终返回true,获取数据的更好方法是在表单上使用jquery的.serialize()方法,尽管我在html中没有看到表单元素,但我假设有一个

var dataString = $('yourForm').serialize();

谢谢,您可以查看wallajax.php页面吗?因为在控制台中传递了值,但我认为在wallajax.php中没有检索
var dataString = 'content=' + status2 + '&check=' + checkit;
<script type="text/javascript">
$(function() {

$("#px").click(function() {
    var status2 = $("#status2").val();
     var checkit= $("#checkit").prop('checked');
       var dataString = 'content=' + status2 + '&check=' + checkit;
        $.ajax({
        type: "POST",
        url: "wallajax.php",
        data: dataString,
        cache: false,
        success: function(resol) {
        $('#wallposts').prepend(resol);
        console.log(resol)
              }
        }); return false;
    });

});
</script>
isset($_POST['check']) && isset($_POST['content']) 
var dataString = $('yourForm').serialize();