4

I'm trying to pass a JavaScript array into a PHP script to then be put into a database but I'm having issues. I have used JSON.stringify on the array before sending. The request seems to work in that when stepping through the code debugging the php page is called but no data is passed into the POST global. I'm sure its something amateurish that I've missed but I'm struggling. This is the code:-

$.ajax({
    type: "POST",
    datatype: "json",
    url: "processdraw.php",
    data: {
        json: pDrawnTeams
    },
    contentType: "application/json; charset=utf-8",
    success: alert('worked')
})
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
sthubbins
  • 41
  • 2
  • Check this link http://stackoverflow.com/questions/18697034/how-to-pass-parameters-in-ajax-post – Jonathan Anctil May 12 '15 at 19:51
  • I've changed POST to GET & now it works. I thought i understood the difference between the two but i'm not sure why this has made the difference. Can anybody educate me? Ta – sthubbins May 12 '15 at 20:02
  • 1
    @sthubbins, when you say you "changed POST to GET", do you mean the type property of the AJAX call in the JavaScript code? If so, then you may not be checking the right global variable in the PHP code. Can you post the PHP code as well? – Lee Jenkins May 12 '15 at 20:44

1 Answers1

1

If you are not getting any error in your javascript, make sure that you are getting that parameter like this in your php target file:

$myJSON = $_POST['json']; // ['parameter name']

after that you will need to decode that json

$myData = json_decode($myJSON, true) // true is for retrieving as an array
Mollo
  • 723
  • 3
  • 7
  • 24