Php and Ajax updating 2 divs

Hello everyone…

I have been struggling with this all day long and simply cannot find the problem :frowning:

I have an Ajax post triggering a php script and want to return to values as the response to update the div boxes. For some reason the info wont display. When i console.log I get the object containing the array but no result.

Here is the code :

     $.ajax({
            type:"POST",
            url: "getlessoninfo.php",
            data: {lcode:lcode},       
            dataType: "json",      
            success: function(response) {
                var new_data = JSON.parse(response); 
                var instruct = new_data.response1;
                var video = new_data.response2;
                
                $('#instructions-info').html(instruct);
                $('#video-info').html(video);
            },
            error: function(response) {
                console.log(response);
            }
    });    

 ----------- PHP --------------

echo json_encode(array(
   'response1' => $instructions,
   'response2' => $video
));

Could you copy/paste what you get from console.log(new_data); after parsing the response?

Hi Jim

When I change the error portion of the code…

error: function(response) {
console.log(‘something went wrong’);
}

then the error is true…the message displays…which mean there was an error on returning of the data.

There is no success TRUE since none of the code inside the success function shows…no console logs

Ok, if you open dev tools in the browser (shortcut: F12) and go to the networks tab before firing the ajax request what can you see there? You should most likely get an http status code indicating some error.

The status is 200 which I believe is OK

So, number 1, why is there a dollar sign variable in this?
You need to send in a key value pair. The key so the script knows what to look for; the value so the script has the search string.

In your php script, rather than echoing the json string, do a print_r($_POST) and see what it is getting to work with. I have a thought it isn’t what you mean to be sending.

Hey - thanks for responding.

print_r $post returns lcode…which I only use to lookup a record in a table.
The lookup is successful. I want to return and update two divs with the new values

How do I return them in an array like I showed in my original code?

I can see the object contains the array in responseText

so what is the code to return what you want?

echo json_encode(array(
   'response1' => $instructions,
   'response2' => $video
));

Does $instructions and $video have the correct values?

If they do, then the issue is with

                $('#instructions-info').html(instruct);
                $('#video-info').html(video);

What does console.log(video) have in it?

Concole.log fails in the ajax success function…none of the code gets processed.
Inside ERROR function part…console.log(request.responseText);
gives me the output below:

{“response1”:“Learn how to draw an impossible triangle”,“response2”:"<iframe src=“https://player.vimeo.com/video/30” width=“640” height=“360” frameborder=“0” webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>"}

You can see responseText has values in both response1 and response2

It’s failing because you specify json, and what is returned isn’t valid json…

When I change it to the following I still get an error response.

$data = array(
“response1” => “testing 111”,
“response2” => “testing 222”
);

Error response:
{“response1”:“testing 111”,“response2”:“testing 222”}

Why is this not JSON ? Do you know how to fix it. I have used numerous examples online with code exactly like mine without success.

thanks

If json is specified, the response is parsed using jQuery.parseJSON before being passed, as an object, to the success handler. The parsed JSON object is made available through the responseJSON property of the jqXHR object.

Change the dataType to ‘text’ and try another test.

“text” wont work with JSON.parse… it gives errors.
If I have to take this out and also not use json_encode…
how do I retrieve the different array values…from php with javascript?

PM me the route you are using. URL everything I need to make a request.

how do i PM you - Im pretty new to this site. And thank you!

Solved…removed commented text and Json.parse statement

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service