将带有JSON的Jquery数组传递给PHP

时间:2022-10-16 12:49:14

hey guys i read some of the other posts and tried alot but its still not working for me. when i alert the array i get all the results on the first site but after sending the data to php i just get an empty result. any ideas?

嘿伙计们,我读了一些其他帖子并尝试了很多,但它仍然不适合我。当我提醒数组我得到了第一个网站上的所有结果,但在将数据发送到PHP后,我得到一个空的结果。有任何想法吗?

$(document).ready(function() {
$('#Btn').click(function() {
    var cats = [];
    $('#cats input:checked').each(function() {
        cats.push(this.value);
    });

    var st = JSON.stringify(cats);
   $.post('foo.php',{data:st},function(data){cats : cats});
   window.location = "foo.php";     
   }); 
});

Php

$data = json_decode($_POST['data']);

THANK YOUU

my array looks something like this when i alert it house/flat,garden/nature,sports/hobbies this are a couple of results the user might choose (from checkboxes). but when i post it to php i get nothing. when i use request marker (chrome extension) it shows me something likethat Raw data cats=%5B%22house+themes%22%2C%22flat+items%22%5D

当我提醒房屋/公寓,花园/自然,运动/爱好时,我的阵列看起来像这样,这是用户可能选择的几个结果(来自复选框)。但当我把它发布到PHP我什么都没得到。当我使用请求标记(镀铬扩展)时,它向我显示了一些原始数据cat =%5B%22house + themes%22%2C%22flat + items%22%5D

i also tried this way-- still no results

我也试过这种方式 - 仍然没有结果

$(document).ready(function() {
$('#Btn').click(function() {
    var cats = [];
    $('#cats input:checked').each(function() {
        cats.push(this.value);
        alert(cats);

    $.ajax({

    type: 'POST',
    url: "foo.php",
    data: {cats:  JSON.stringify(cats)},
    success: function(data){
     alert(data);
     }
   });
  }); 
   window.location = "foo.php";
  }); 
}); 

php:

$json = $_POST['cats'];
$json_string = stripslashes($json);
$data = json_decode($json_string, true);
echo "<pre>";
print_r($data);

its drives me crazy

它让我疯狂

4 个解决方案

#1


1  

Take this script: https://github.com/douglascrockford/JSON-js/blob/master/json2.js

拿这个脚本:https://github.com/douglascrockford/JSON-js/blob/master/json2.js

And call:

var myJsonString = JSON.stringify(yourArray);

so now your code is

所以现在你的代码是

$(document).ready(function() {
$('#Btn').click(function() {
    var cats = [];
    $('#cats input:checked').each(function() {
        cats.push(this.value);
    });
   var st = JSON.stringify(cats);
   $.post('foo.php',{data:st},function(data){cats : cats});
 //  window.location = "foo.php";    // comment this by this page redirect to this foo.php
   });
   }); 

//and if uou want toredirect then use below code

//如果你想要重定向,那么使用下面的代码

-------------------------------------------------
     $.post('foo.php',{data:st},function(data){
       window.location = "foo.php"; 
     });
---------------------------------------------------

Php

$data = json_decode($_POST['data']);

#2


1  

var ItemGroupMappingData = []

Or 

var ItemGroupMappingData =
{
"id" : 1,
"name" : "harsh jhaveri",
"email" : "test@test.com"
}

 $.ajax({
            url: 'url link',
            type: 'POST',
            dataType: "json",
            data: ItemGroupMappingData,
            success: function (e) {
// When server send response then it will be comes in as object in e. you can find data //with e.field name or table name
            },
            error: function (response) {
                //alert(' error come here ' + response);
                ExceptionHandler(response);
            }
        });

#3


0  

Try this :-

试试这个 :-

    $data = json_decode($_POST['data'], TRUE);

#4


0  

I think you should move the "window.location = " to the post callback, which means it should wait till the post finshed and then redirect the page.

我认为你应该将“window.location =”移动到帖子回调,这意味着它应该等到帖子finshed然后重定向页面。

$.post('foo.php', {
  data : st
}, function(data) {

   window.location = "foo.php";
});

#1


1  

Take this script: https://github.com/douglascrockford/JSON-js/blob/master/json2.js

拿这个脚本:https://github.com/douglascrockford/JSON-js/blob/master/json2.js

And call:

var myJsonString = JSON.stringify(yourArray);

so now your code is

所以现在你的代码是

$(document).ready(function() {
$('#Btn').click(function() {
    var cats = [];
    $('#cats input:checked').each(function() {
        cats.push(this.value);
    });
   var st = JSON.stringify(cats);
   $.post('foo.php',{data:st},function(data){cats : cats});
 //  window.location = "foo.php";    // comment this by this page redirect to this foo.php
   });
   }); 

//and if uou want toredirect then use below code

//如果你想要重定向,那么使用下面的代码

-------------------------------------------------
     $.post('foo.php',{data:st},function(data){
       window.location = "foo.php"; 
     });
---------------------------------------------------

Php

$data = json_decode($_POST['data']);

#2


1  

var ItemGroupMappingData = []

Or 

var ItemGroupMappingData =
{
"id" : 1,
"name" : "harsh jhaveri",
"email" : "test@test.com"
}

 $.ajax({
            url: 'url link',
            type: 'POST',
            dataType: "json",
            data: ItemGroupMappingData,
            success: function (e) {
// When server send response then it will be comes in as object in e. you can find data //with e.field name or table name
            },
            error: function (response) {
                //alert(' error come here ' + response);
                ExceptionHandler(response);
            }
        });

#3


0  

Try this :-

试试这个 :-

    $data = json_decode($_POST['data'], TRUE);

#4


0  

I think you should move the "window.location = " to the post callback, which means it should wait till the post finshed and then redirect the page.

我认为你应该将“window.location =”移动到帖子回调,这意味着它应该等到帖子finshed然后重定向页面。

$.post('foo.php', {
  data : st
}, function(data) {

   window.location = "foo.php";
});