1

So I've followed all your advice about making an jQuery or Javascript AJAX request to a php file on a server.

The problem I'm running in to is that my response is this

Fatal error: Array callback has to contain indices 0 and 1 in

So here is my jQuery

$(document).ready(function(){

    $(".ex .select").click(function(){
    $("body").data("id", $(this).parent().next().text());

Those lines serve to capture a variable and save it as data on the body element, so I can use it later.

But I want to send that same variable to PHP file, to add to a complex API request - eventually to return the results, create $_SESSION variables and so forth

    var pageId = {
        id: $("body").data("id"),
    };

But let's avoid that as a problem and just make my array simple like

var pageId = {
        id: "12345",
    };

    $.ajax({
        type: 'GET',
        url: 'test.php',
        data: pageId,
        datatype: 'json',
        cache: false,
        success: function (data, status) {
            alert("Data: " + data + "\nStatus: " + status);
        }
    });
});
});

And for now at least I was hoping to keep my PHP file simple for testing my grasp of the concepts here

<?php

$id = $_GET('id');

echo json_encode($id);

?>

I'm expecting the response

Data: 12345

Status: Success

But I get the Error Message above.

2 Answers 2

2

You need to change $_GET('id') to $_GET['id'] and in order to send status, you need to add status to an array just like below and try to parse JSON on your response.

<?php

    $id = $_GET['id'];
    $result = array("id"=>$id, "Status"=>"Success");

    echo json_encode($result);

?>

And access in jquery using . like if you use data variable in success then

success:function(data){
    alert(data.Status);
}
Sign up to request clarification or add additional context in comments.

Comments

0

change $id = $_GET('id'); to $id = $_GET['id'];

because $_GET, not a function it's an array type element.

1 Comment

And since its encoding to json and the ajax datatype is json, would he also need to use jQuery.parseJSON() on data?

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.