0

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

2
  • 2
    Remove window.location = "foo.php"; & then try Commented Nov 13, 2013 at 8:53
  • thank you for your help but this is not working as well unfortunately Commented Nov 13, 2013 at 9:35

4 Answers 4

1

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']);
Sign up to request clarification or add additional context in comments.

1 Comment

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%22children%2F+babys%2Ffamilie%22%2C%22house%2Fflats%2Fgarden%22%5D but i cant echo anything and with var_dump i just get NUll
1
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);
            }
        });

1 Comment

url: 'url link' Here you can pass your url as abc.aspx/savedata or whatever your page link and method name
0

Try this :-

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

Comments

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.

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.