When i call function with alert is working parfectly :
function post() {
var cvs = $('#client-nbr').val();
var cs = $('#cs').val(); $.post('a.php' {postcvs:cvs,postch1:cs});
alert("The function work");
}
And the html form like this :
<form action="b.php" method="post" onsubmit="return post()">
The problem is when i remove alert from function the $.post doesnt work :
function post() {
var cvs = $('#client-nbr').val();
var cs = $('#cs').val(); $.post('a.php' {postcvs:cvs,postch1:cs});
}
Pls help :)
Since
post()
doesn't have areturn
statement, theonsubmit
function ends up returningundefined
.Since it doesn't return
false
, it doesn't cancel the normal behaviour of the form. Consequently, the form submits and unloads the page before the Ajax request resolves.Putting in an
alert
delays the page unload until you click OK, which gives time for the Ajax request to resolve.return false
from thepost
function to cancel the normal submission of the form instead.