AJAX Submit to PHP still loading page after preventDefault()
Posted
by
dannyburrows
on Stack Overflow
See other posts from Stack Overflow
or by dannyburrows
Published on 2013-11-07T21:35:03Z
Indexed on
2013/11/07
21:54 UTC
Read the original article
Hit count: 346
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")}
});
});
});
© Stack Overflow or respective owner