1

This is my first attempt at AJAX and am unable to retrieve the output from the data.php file.

Here is the code so far, not all code is included, just what is applicable:

index.php

<article class="post">
    <form name="form"><input type="hidden" name="postid" value="<?php echo $value['post_id'];?>" />
   </article>

<div id="main-post">
<div id="gotpostid">some text</div>
</div>

.js file

$(".cover-img").click(function(){
    $("#main-post").fadeIn(1000);
    $.post("data.php", {postid: form.postid.value},
        function(output){
              $("#gotpostid").html(output).show();
    });   
 });

data.php

<?php

    echo 'got this from data.php';
?>

the data.php file is in the same directory as the .js.

Currently when I click on the image it shows the "main-post" div but the "gotpostid" within it still only displays the "some text" and doesn't replace it with the output text from the php file.

This is the result of adding the following code:

.fail(function(x,y,z){ $("#gotpostid").html(x + "<br />" + y + "<br />" + z)})

[object Object] error Not Found

I found the error mostly thanks to Kevin B's suggestion for adding the .fail().

The url wasn't correct for the .php file, it should have been js/data.php.

4
  • might want to take a look at this. Commented Jun 8, 2012 at 18:53
  • Your code looks fine. What is it doing/not doing? Commented Jun 8, 2012 at 18:57
  • @Kevin B Hi, I added an explanation to the bottom. Commented Jun 8, 2012 at 19:00
  • 2
    Modify your $.post to $.post(url,{...},function(...){...}).fail(function(x,y,z){ $("#gotpostid").html(x + "<br />" + y + "<br />" + z)}) and post the response of it in your question. (note: all i added was .fail(handler)) Commented Jun 8, 2012 at 19:03

2 Answers 2

1

Add a error method to your ajax handler to know if something unexpected is happening

$(".cover-img").click(function(){
    $("#main-post").fadeIn(1000);
    var jqxhr = $.post("/echo/html/", {postid: form.postid.value},
        function(output){
              $("#gotpostid").html(output).show();
    })
    .error( function()
    {
        console.log( "error" );      
    })

    return false;
 });​
Sign up to request clarification or add additional context in comments.

Comments

0

Install FireBug if not already done, and do a console.log(output) inside you response function. Do you see your response?

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.