0

I have a PHP file called terminal_tester.php which runs a number of terminal actions and creates json data at the end using

echo json_encode($jsonData);

The data looks like this

{"source":"Betting Tips","published":"2015-05-20 15:20:22;","status":true,"eventIDs":["27448131","27448900"],"TipsTB":"TIP 1 MLADENOVIC TO BEAT RISKE\",\"TIP 2 DOLGOPOLOV TO BEAT GULBIS\"]","TipsTW":"[]"}

I now want to populate my HTML file with this data, but am having trouble understanding the correct format for the Ajax data input. I am trying the below in the script area of my html file

function callbackData(){
    return $.ajax({
        dataType: 'JSON',
        url: 'terminal_tester.php',
        type: 'GET',
        cache: false,
        data: jsonData
    });
};

callbackData().success(function (data) {
    document.getElementById("phpReturn2").innerHTML = jsonData
    document.getElementById("phpReturn3").innerHTML = eventIds
    document.getElementById("phpReturn4").innerHTML = published
});

but I'm not getting any response. I've searched and I think the problem lies in the data: area of the ajax request but am also confused by the need of a GET command in the PHP file. Could somebody explain how to correctly structure the ajax request?

EDIT

terminal_tester.php has quite a few functions which come together at the end to build the json data, the final part of the php file looks like this

      $jsonData = createJson($eventIds, $TipsTB, $TipsTW, $status);
      echo json_encode($jsonData);
      $fp = fopen('results.json', 'w');
      fwrite($fp, json_encode($jsonData));
      fclose($fp);
2
  • what code is written in terminal_tester.php Commented May 20, 2015 at 14:48
  • I will edit the question to show it... Commented May 20, 2015 at 14:52

2 Answers 2

2

First, I think your json data is incorrect. It should be like this-

{"source":"Betting Tips","published":"2015-05-20 15:20:22","status":true,"eventIDs":["27448131","27448900"],"TipsTB":["TIP 1 MLADENOVIC TO BEAT RISKE","TIP 2 DOLGOPOLOV TO BEAT GULBIS"],"TipsTW":"[]"}

Second, normal jquery ajax syntax is -

$.ajax({
    dataType: 'JSON',  //This means data which come back from terminal_tester.php should be in json format.
    url: 'terminal_tester.php',
    type: 'GET',  // If you are using get request then you should get data by $_GET[]
    cache: false,
    data: {"jsonData":jsonData}, // Edited this from your code.
    success:function(data){ //This data is coming from terminal_tester.php
        alert(data.result);
    }
});

In terminal_tester.php, it should be like this-

if(isset($_GET['jsonData'])){
    $jsonData = $_GET['jsonData']; // GET array (Edited)
    // your operation with $jsonData

    // In the end, below json will be get in success data.
    echo json_encode(array('result'=>true));

}

Hope this helps you!!

Sign up to request clarification or add additional context in comments.

8 Comments

Wow, thanks so much for the time taken to respond.. I don't have time now to try it but will do so later tonight. Thanks again...
Hi mohit, I copied and pasted both the javascript and the php into my files but the alert is not firing when I refresh the HTML page. When I run the php file on its own it is not echoing out the json data which leaves me to believe the if(asset($GET['jsonData'])) is not firing as true. I'm still a bit confused as to why it is not working... Thanks
First, are you using jquery liberary? Second, uncomment dataType:"JSON" & alert(data) than check what is coming. It should be one json string. If html is coming with json string than you need to write isset code at the top of the file. I have edited json_decode line.
in script- var jsonData = {"source":"Betting Tips","published":"2015-05-20 15:20:22","status":true,"eventIDs":["27448131","27448900"],"TipsTB":["TIP 1 MLADENOVIC TO BEAT RISKE","TIP 2 DOLGOPOLOV TO BEAT GULBIS"],"TipsTW":"[]"};
it will coz something other than json is coming in data, try commenting dataType:"JSON" and write alert(data) . I believe you can fix this.
|
0

$.ajax().success() has a data parameter for accessing the data sent back from your GET request. eventIds and published are both properties of data.

callBackData().success(function (data) {

    document.getElementById("phpReturn2").innerHTML = jsonData;
    document.getElementById("phpReturn3").innerHTML = data.eventIds;
    document.getElementById("phpReturn4").innerHTML = data.published;
});

1 Comment

So I need a GET request line in the php file..? I saw somewhere that echo json_encode worked instead....?

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.