I have a page with a button and, when I click on this button, jquery-ajax executes the php code in an other file but the code not working as I would like.
The button code is very simple:
<button type="submit" onclick="runCode()"> Execute </button>
The problem is on jquery-ajax code because if I use this code:
$(document).ready(
function runCode(){
$.ajax({
url: "file.php",
success: function(msg){
alert( "Done.");
}
});
}
)
when I load the page, the code of file.php is execute but not clicking the button, in fact the button, after loading of the page, is not working.
Instead, if I use this code:
function runCode(){
$.ajax({
url: "file.php",
success: function(msg){
alert( "Done.");
}
});
}
the button works and I see the alert "Done" when I click on it but file.php is not executed.
Why in the first case the file is executed but button not working, while in the second case button works but php file is not executed? How to correct the code?