AJAX Submit to PHP still loading page after preventDefault()
- by dannyburrows
I have a webpage that I am using .ajax to send variables to a php script. The php page is loading into the browser as if I was navigating to it. The script is loading the data correctly, so my issue is stopping the navigation and keeping the user on the original page. The code for my form is here:
echo "<form method='post' action='addTask.php' id='myform'>\n";
echo "<input name='addtask' id='addtask' maxlength='64'/><br/>\n";
echo "<input type='submit' name='submit' id='submit' value='Add Task'/>\n";
echo "</form>\n";
The code for my jquery is here:
$(function(){
$('#myform').submit(function(e){
e.stopPropagation();
$.ajax({
url: 'addTask.php',
type: 'POST',
data: {},
success: alert("Success")
});
});
});
I have tried: e.preventDefault(), e.stopPropagation() and return false. Any help is appreciated.
$("#submit").click(function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: "addtask.php",
data: { }
})
.done(function() {
alert( "success" );
})
.fail(function() {
alert( "error" );
});
and
$(function(){
$('#myform').submit(function(e){
e.preventDefault();
$.ajax({
url: 'addTask.php',
type: 'POST',
data: {},
success: function(){alert("Success")}
});
});
});