PHP JSON 数据传递

标签 php jquery json

我有一个像这样的html html

<input type="checkbox" class="pushchecks" value="JAN" >
<input type="checkbox" class="pushchecks" value="FEB" >
<input type="checkbox" class="pushchecks" value="MAR" >

我需要获取选中的复选框值并以JSON格式传递到另一个页面

JQUERY

$('body').on('click','.pushchecks',function(){           
        var bills = {};  var i=0;                   
        $(".checks:checked").each(function(){   bills = $(this).val();  i++; });         
        bills = JSON.stringify(bills); 
        $.load('process.php', { checkedbills:bills }, '.list',function(){});
});  

流程.php

 $billsAry  =   json_decode($_REQUEST['checkedbills']); 

如果我选中 JAN 和 MAR 复选框,那么我需要在 process.php 页面中显示 JAN 和 MAR

最佳答案

只需对 Louys Patrice Bessette 代码进行一些编辑即可实现此功能。请注意,这里的请求是 POST request

test.html

<input type="checkbox" class="pushchecks" value="JAN" >
<input type="checkbox" class="pushchecks" value="FEB" >
<input type="checkbox" class="pushchecks" value="MAR" >

<!--to readable output from the php file-->
<pre class="result"><pre>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<script>
$('body').on('click','.pushchecks',function(){           
         // An array.
         var bills=[];

  // Loop trought all .pushchecks checked.
  // Edited to be .pushchecks instead of .checks
  $(".pushchecks:checked").each(function(){

    // Add this value in array.
    bills.push( $(this).val() );
  });

  // Stingify the array. Would convert the array to json object
  bills = JSON.stringify(bills);

  // Ajax request to load process.php result in the pre tags above.
  // Use $('*') to select all elements in the DOM
  $('.result').load('process.php', { checkedbills:bills }, '.list',function(){});
});
</script>

process.php

<?php

 $billsAry  =   json_decode($_REQUEST['checkedbills']);
 print_r($billsAry);  //The response and output to the request 
?>

关于PHP JSON 数据传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44486071/

相关文章:

php - 预览 PHP 中的电子邮件开发

jQuery $.value on select > 没有 value 属性的选项

python - 在Python中读取大型JSON文件

json - Spring Boot 中同一实体的多个自定义序列化程序

javascript - 如何在 Jquery 和 PhantomJS 中选择 Javascript 生成的字段?

json - 如何在Go中解析json?

javascript - Wordpress - 翻译 javascript 或 jquery 中的字符串

PHP + MySQL + CodeIgniter : Check if current row exists

php - 无法发送 session cookie - header 已发送

jQuery 效果、动画和 CSS