0

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?

6
  • 3
    You're already using AJAX. $.post() is a jQuery shorthand AJAX method. Commented Dec 9, 2015 at 18:40
  • 3
    that's totally invalid json. what exactly is in final_slip before you stringify it? Commented Dec 9, 2015 at 18:41
  • 1
    Ok how can I decode this string that is outbputed? Commented Dec 9, 2015 at 18:41
  • 4
    You cannot, until you create valid JSON. Commented Dec 9, 2015 at 18:41
  • 1
    The final_slip is the second code block in my question. Commented Dec 9, 2015 at 18:41

2 Answers 2

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

Comments

0

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);

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.