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

share|improve this question
2  
Remove window.location = "foo.php"; & then try – Rohit Nov 13 '13 at 8:53
    
thank you for your help but this is not working as well unfortunately – Georg Kirschstein Nov 13 '13 at 9:35

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']);
share|improve this answer
    
thank you but its still not working the chrome extension shows me that this data gets submitted data=%5B%22other+themes%22%2C%22economy%2Fpolitics%22%2C%22c‌​hildren%2F+babys%2Ff‌​amilie%22%2C%22house‌​%2Fflats%2Fgarden%22‌​%5D but i cant echo anything and with var_dump i just get NUll – Georg Kirschstein Nov 13 '13 at 10:26
var ItemGroupMappingData = []

Or 

var ItemGroupMappingData =
{
"id" : 1,
"name" : "harsh jhaveri",
"email" : "[email protected]"
}

 $.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);
            }
        });
share|improve this answer
    
url: 'url link' Here you can pass your url as abc.aspx/savedata or whatever your page link and method name – harshboss Nov 13 '13 at 9:16

Try this :-

    $data = json_decode($_POST['data'], TRUE);
share|improve this answer

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";
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.