2

For some reason, jQuery isn't loading my php file. The button is clicked and the page just refreshes. I have verified jQuery is working and the click function is working as well. Once it gets to $.post it doesn't seem to call that file and go through the pause and echo the result. The directory is correct for the php file. I am at a loss. Any ideas?

// HTML Code    
<input id="doit" class="medium blue button" type="submit" name="doit" value="Sign in">

//jQuery code:
$('#doit').click(function(){
    $('#fullscreen').show();
    $.post('./delete-misc/test-ajax-code.php', function(data){
         $('#fullscreen').hide();
         alert(data);
    })
});

// Php code (test-ajax-code.php)
<?php
    sleep(10);
    echo "Done";
?>
5
  • 2
    Try $.post('/delete-misc/test-ajax-code.php', ... without a dot in the beginning. Commented May 4, 2011 at 7:09
  • Does it show the alert(data). Try to add return false; before the }); to prevent the form submitting. Commented May 4, 2011 at 7:11
  • @bazmegakapa - That did it! All documentation I saw said to put that there. Any logical reason removing it fixed the problem? Commented May 4, 2011 at 13:35
  • @Jason I will add it as an answer then :). Commented May 4, 2011 at 14:00
  • @bazmegakapa - Great, marked as the answer. Thanks again! Commented May 4, 2011 at 14:11

6 Answers 6

3

Try to remove the dot from the beginning of the URL.

$.post('/delete-misc/test-ajax-code.php', ...

I cannot give you a detailed explanation on how a dot in the URL path works exactly due to lack of experience, but I will make this a community wiki in order to let someone explain it.

EDIT: I asked a question about this topic, the answers are great: What does a dot mean in a URL?

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

Comments

2

Change your button type.
So, instead of this:

<input id="doit" ... type="submit" ... />

You should have this:

<input id="doit" ... type="button" ... />

Why? Cause a submit button will trigger a form submit, which goes to another page. But a button does nothing, well only the things you programmed (with js) to do.

Another solution is to prevent default behaviour of button click in javascript, so you should have:

$('#doit').click(function(){
    $('#fullscreen').show();
    $.post('./delete-misc/test-ajax-code.php', function(data){
         $('#fullscreen').hide();
         alert(data);
    });
    return false; //This does the trick!!!
});

Why? returning false inside a jquery event 'cancels' default behaviour, so form doesn't get submitted

Hope this helps. Cheers

3 Comments

Changing it to button does keep the page from refreshing but still doesn't load the test-ajax-code.php file. What happens now is it just shows #fullscreen div and hangs up there, never returning the php echo and then hiding #fullscreen.
So you have another problem else. Does your 'alert' execute?
No, the alert was not executing.
1

Try to change to type button:

<input id="doit" class="medium blue button" type="button" name="doit" value="Sign in">

Comments

1

You need to return false in your submit handler. If you don't, the form will submit as it normally does after your handler returns.

Comments

0

You just forgot the ; after to end your $.post ;).

Try this, it personnaly works :

            $('#doit').click(function(){
                $.post('./delete-misc/test-ajax-code.php', function(data){
                     alert(data);
                });
            });

Comments

0

remove the name attribute for the submit button and try. I had a similar issue some time back, deleting the name attribute was the trick. (as I submitted the posted data through jQuery, I did not need the name attribute).

I still wonder why it worked back then?

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.