tags:

views:

62

answers:

3

What's wrong with this code? I'm not able to validate 'data':

$.post('http://localhost/do.php', function(data) 
{
  if (data == "success")
  {
    //do something... but this line never hit!
  }
});

I also tried,

alert(data);

but got an empty alert box!

the do.php on success, echo's "success"

echo "success";

ok.. here is the complete original code:

<script type="text/javascript">
function confirm_delete(id)
{
    var r=confirm("Are you sure you want to delete?");
    if (r==true)
    {
      var path = "http://localhost/site/index.php/delete/" + id;

     $.post(path, function(data) 
      {
            if (data=='success')
            {
            $('#'+id).remove();
            }
            else 
            {
            alert("Unable to delete, try again!");
            }
      });

    }else
    {
      //cancel
    }
}

//-->
</script>

In the HTML, there will be many posts with their respective id's in div, laid by php from database, somewhat like this:

<div id="1">
<div class='post'>Something</div>
<a href="#"><img src="styles/plugins/buttons/icons/cross.png" height="8" width="8" title="Remove" onclick="confirm_delete(1)"/></a>
</div>

<div id="2">
<div class='post'>Something</div>
<a href="#"><img src="styles/plugins/buttons/icons/cross.png" height="8" width="8" title="Remove" onclick="confirm_delete(2)"/></a>
</div>

<div id="3">
<div class='post'>Something</div>
<a href="#"><img src="styles/plugins/buttons/icons/cross.png" height="8" width="8" title="Remove" onclick="confirm_delete(3)"/></a>
</div>

In the php there is nothing now... it just prints success... i made it like that for testing.

echo "success";

When going to the linking directly, it printing "success". Is 'Data' a string? I mean is this correct?

if (data=='success')
A: 

are you printing/echo-ing the value of data in your do.php file ?

naiquevin
yes echo -ing "success"
esafwan
do you use firebug for debugging? if yes, what response you are getting in console ?
naiquevin
empty response!
esafwan
difficult to tell without more code. A wild guess, may be its not satisfying the if(success) condition. Does it echo "fail"; otherwise. Try printing something and exiting outside of this if block
naiquevin
A: 

It certainly looks as though jQuery is running into an AJAX error. If jQuery hits an error using one of those shorthand methods ($.get() $.set()) it fails silently. However, if you implement the ajaxError() method in jQuery, you can get at the error. I would suggest doing this before going any further.

Dan D.
firebug shows that its posting, but no return!
esafwan
Ah, that certainly is interesting. Maybe try just POSTing to `http://localhost/site/index.php` without `delete/" + id` on the end, to see if even that fails?
Dan D.
when i go to that url directly its printing success...is this if(data =="success")a correct way of checking? i mean, is data a string?
esafwan
What I meant, actually, is changing `var path = "http://localhost/site/index.php/delete/" + id;` to `var path = "http://localhost/site/index.php";` in your javascript and seeing if that works. Also, if you cannot get anything from data even when using alert() in the success script, something is wrong other than the if statement.
Dan D.
tried! Same, is there any other way to determine if the delete was a success?
esafwan
At this point, I'm really not sure what to do. There is absolutely no reason why it should not be working, as far as I can tell. Truly baffling.
Dan D.
A: 

Have you checked your web servers error log to make sure the server is not silently failing and logging the error instead of displaying it?

Klinky