3

For some reason I can't get this to work. All I want to do is that when a button is clicked, (via ajax) a php file is executed. Aka, if I weren't using ajax, we'd be looking at:

<a href="file.php">dfgdfg</a>

I want the file to be executed without leaving the page.

This is what I've got atm:

$(".vote-up").live('click', function(e) {
$.ajax("file.php", function(){
       alert("Executed file");
   });
});

That doesn't seem to be working. I'm just really confused how the jQuery ajax function functions in general, without dealing with anything remotely complicated.

Added question:

.ajax({
       url: 'includes/login-check-jquery.php',
       success: function (data) {
            if(data == 1){
                alert("Logged in!!!");
            }else{
                window.location = data;
            }
        error: function (xhr, status, error) {
        // executed if something went wrong during call
        if (xhr.status > 0) alert('Error: ' + status); // status 0 - when load is interrupted
        }
    });
});

In the above code, if the data returned equals "logged in" than a message box appears. Otherwise it'll redirect to the location (that is sent in data). For some reason this if statement isn't working, but the data is being returned as expected.

My PHP code is:

<?php  
if (!$user->logged_in){
    echo "../login/index.php";
}else{
    echo "1";
}

?>

Any ideas?

13
  • 1
    try replacing live with on because live is deprecated ! also check that the path to the php file is correct Commented Aug 9, 2012 at 2:33
  • This doesn't look wrong to me--are you trying to get a response from the PHP file or just set something off and forget about it? Commented Aug 9, 2012 at 2:35
  • 1
    I don't believe your anonymous function will be executed without getting a non-error status code from your web server. You can use something like Firebug or Chrome's built-in developer tools' network tab to see the status code from the request. Commented Aug 9, 2012 at 2:37
  • 1
    Look into window.location: developer.mozilla.org/en-US/docs/DOM/window.location Commented Aug 9, 2012 at 2:55
  • 1
    Check @therao's answer--and you might want to ensure you're either passing something back in plaintext or as JSON. Commented Aug 9, 2012 at 3:00

1 Answer 1

6

If you don't want to leave the page, do

$(".vote-up").live('click', function(e) {
    e.preventDefault();
    ...

And ajax could be updated, if desired to

$.ajax({
    url: 'file.php',
    success: function (data) {
        // this is executed when ajax call finished well
        alert('content of the executed page: ' + data);
    },
    error: function (xhr, status, error) {
        // executed if something went wrong during call
        if (xhr.status > 0) alert('got error: ' + status); // status 0 - when load is interrupted
    }
});

error callback part could be removed, if you go for simplicity and not usability and some more options could be added by referencing jquery ajax doc.

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

10 Comments

I'm a bit confused with this. Is data a variable that is assigned the value of whatever is returned by the php file?
From testing it, yes that is the case. Thanks :) This'll work :)
yes, it is. sorry for quick replies without really finishing reading ;)
All good. I understand it and I'm getting it working so all good :)
Put up a question at the end if you have any ideas :)
|

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.