PHP JSON数据传递
我有一个像这样的html html process.phpPHP JSON数据传递,php,jquery,json,Php,Jquery,Json,我有一个像这样的html html process.php $billsAry = json_decode($_REQUEST['checkedbills']); 如果我选中JAN和MAR复选框,那么我需要在process.php页面中显示JAN和MAR 问题是,您的循环只保留上次检查的值 使用arry,可以确保收集所有值 $('body').on('click','.pushchecks',function(){ // An array. var bills=[];
$billsAry = json_decode($_REQUEST['checkedbills']);
如果我选中JAN和MAR复选框,那么我需要在process.php页面中显示JAN和MAR
问题是,您的循环只保留上次检查的值
使用arry,可以确保收集所有值
$('body').on('click','.pushchecks',function(){
// An array.
var bills=[];
// Loop trought all .pushchecks checked.
$(".checks:checked").each(function(){
// Add this value in array.
bills.push( $(this).val() );
});
// Stingify the array.
bills = JSON.stringify(bills);
console.log(bills); // Would print : {["JAN",MAR"]}
// Ajax.
$.load('process.php', { checkedbills:bills }, '.list',function(){});
});
只需对Louys Patrice Bessette代码进行一些编辑就可以了。请注意,尽管这里的请求是一个 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>
$('body')。在('click','pushchecks',函数()上{
//数组。
var票据=[];
//循环trought所有。检查推送检查。
//编辑为.pushchecks而不是.checks
$(“.pushchecks:checked”).each(函数(){
//在数组中添加此值。
bills.push($(this.val());
});
//Stingify数组。将数组转换为json对象
bills=JSON.stringify(bills);
//加载process.php的Ajax请求会导致上面的pre标记。
//使用$('*')选择DOM中的所有元素
$('.result').load('process.php',{checkedbills:bills}',.list',function(){});
});
process.php
我认为您应该序列化该选项。例如,使用$.load('process.php?month=jan')
调用您的页面,在php中使用$\u GET variable那么您的实际问题是什么?表示复选框值通过JSON传递到另一个流程页面
$('body').on('click','.pushchecks',function(){
// An array.
var bills=[];
// Loop trought all .pushchecks checked.
$(".checks:checked").each(function(){
// Add this value in array.
bills.push( $(this).val() );
});
// Stingify the array.
bills = JSON.stringify(bills);
console.log(bills); // Would print : {["JAN",MAR"]}
// Ajax.
$.load('process.php', { checkedbills:bills }, '.list',function(){});
});
<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>
<?php
$billsAry = json_decode($_REQUEST['checkedbills']);
print_r($billsAry); //The response and output to the request
?>