Reputation: 1246
I have an array in jQuery that I am trying to convert to a PHP array by using Post:
$.post("http://www.samepage.com", {final_slip: JSON.stringify(final_slip)});
When I pass this dynamically created array "final_slip" into it :
[final_bet_game { final_user_id="1", final_game_id="1", final_game_type="spread_2",final_price="10", final_odds="1.8"}, final_bet_game { final_user_id="2", final_game_id="3", final_game_type="spread_2",final_price="1", final_odds="2.8"}, final_bet_game { final_user_id="3", final_game_id="14", final_game_type="spread_32",final_price="140", final_odds="1.8"}, final_bet_game { final_user_id="4", final_game_id="1", final_game_type="spread_2",final_price="10", final_odds="2.8"}, ]
I get this php outputted :
$data = $_POST['final_slip'];
print_r ( $data);
[{\"final_user_id\":\"1\",\"final_game_id\":\"1\",\"final_game_type\":\"spread_2\",\"final_price\":\"211\",\"final_odds\":\"1.8\"},{\"final_user_id\":\"1\",\"final_game_id\":\"2\",\"final_game_type\":\"spread_2\",\"final_price\":\"212\",\"final_odds\":\"1.8\"},{\"final_user_id\":\"1\",\"final_game_id\":\"parlay\",\"final_game_type\":\"\",\"final_price\":\"021\",\"final_odds\":\"\"}]
I tried to use the json_decod, but Im not getting any results. How can I get this to a usable php array? Would I be better off using ajax?
Upvotes: 0
Views: 78
Reputation: 1246
The problem was the backslashes in the outout needed to be stripped out:
$result = $_POST['json_string'];
$test = urldecode(str_replace("\\","",$result));
$test2 = (json_decode($test,true));
print_r($test2);
Upvotes: 0
Reputation: 57388
$.post("http://www.samepage.com", {myJsonString: JSON.stringify(myJsonString)});
But when I then try to access it in PHP I am not seeing any results.
And that tells you that you have another problem - PHP is not reporting errors. You want to turn error_reporting
on.
In this case the likely cause is that your data is arriving into the $_POST
array, and $myJsonString
is not defined (automatic definition of parameters has been deprecated since PHP 5.3.0, and no longer available since PHP 5.4.0).
You should either do
if (array_key_exists('myJsonString', $_POST)) {
$myJsonString = $_POST['myJsonString'];
} else {
die("myJsonString not in _POST");
}
or try straight
$result = json_decode($_POST['myJsonString'], true);
Upvotes: 3