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')
})
3
  • Check this link stackoverflow.com/questions/18697034/… Commented May 12, 2015 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 Commented May 12, 2015 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? Commented May 12, 2015 at 20:44

1 Answer 1

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

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.