3

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";
?>
kapa
  • 77,694
  • 21
  • 158
  • 175
Jason
  • 221
  • 3
  • 11

6 Answers6

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?

Community
  • 1
  • 1
kapa
  • 77,694
  • 21
  • 158
  • 175
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

Edgar Villegas Alvarado
  • 18,204
  • 2
  • 42
  • 61
1

Try to change to type button:

<input id="doit" class="medium blue button" type="button" name="doit" value="Sign in">
Alex R.
  • 4,664
  • 4
  • 30
  • 40
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.

Svish
  • 152,914
  • 173
  • 462
  • 620
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);
                });
            });
Timon. Z
  • 681
  • 3
  • 9
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?

Anji
  • 725
  • 1
  • 9
  • 27