简体   繁体   中英

Pass Array FROM Jquery with JSON to PHP

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?

$(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

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

Take this script: 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']);
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);
            }
        });

尝试这个 :-

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM