Can you help explain the callbacks on $.get and $.post. The some.php here is a script that returns some json. I don't understand why the former gets called and latter doesn't get called, all else equal. Code: $( "#sendMessageBtn" ).click( function(event) { event.preventDefault(); var message = $("#chatMessage").val(); $.post("some.php", {'message': message}, alert("I am back") ); }); Code: $( "#sendMessageBtn" ).click( function(event) { event.preventDefault(); var message = $("#chatMessage").val(); $.post("some.php", {'message': message}, function(){alert("I am back");} ); }); Fkc, I just realised I included some invisible comments before <?php
wtf? you are using javascript or some other shitass sorry excuse for a language., there is your problem. oh php, haha, christ, good luck
While I little memory PHP semantics (hate the language), the striking difference between the first and second code snippet is that the latter passes a new function calling alert whereas the first passes the result of a function call to alert. If the post function doesn't check that the passed argument is a function and call it, alert won't be called in that case. The fault of PHP here is likely in not error reporting a function being passed when it's not used in the caller.
@blueraincap The 2nd you are passing in an anon function but you don't call it. @Snuskpelle is going the right way. Try something like this: $.post("some.php", {'message': message}, xxx(); ); function xxx(){alert("I am back");} @stochastix Build me an Airbnb clone without Javascript. We won't hold our breath.
They should both work. So long as some.php returns a success code (200), the success handler will be called (the real question should be why does the first one work). The only way I was able to get the first to work, and second to fail was to call a non existent php file (status 404). Clearly some.php is not returning 200, it's returning an error code, mostly likely not found (404). You can check the status code by opening the console and going to the Network tab, and then clicking your sendMessageBtn.
Just to clarify, they both work in a sense that they should both trigger the alert, but the first is incorrect. You should not be passing the alert directly to the handler, you should be passing in a function.