> <script language="javascript" type="text/javascript" src="jq.js"></
> script>
> <script language="javascript" type="text/javascript">
> function submitting()
> {
>
> $.ajax({
> type: "POST",
> url: "some.php",
> data: "name=John&location=Boston",
> success: function(msg){
> alert( "Data Saved: " + msg );
> }
>
> });
> return true;
> }
> </script>
> <form action="myaction.php" method="post" onsubmit="return submitting
> ()">
> <input type="text" name="name"/><br/><br/>
> <input type="text" name="email"/><br/><br/>
> <input type="submit" name="mysub" value="Submit">
> </form>
>
> When i try to submit this form it directly goes to the action file of
> form that is "myaction.php" so without finishing the request sent to
> the some.php from the ajax call.
>
> To Resolve this i had put this attribute " async: false " in the
> above ajax call. that works however it takes a bit of time before the
> form starts submitting so this can be annoying for the users.
>
> In some.php i am retrieving the data and just sending an email. ALSO
> i don't wanna use javascript's submit() function.
A better way to re-write this is without the onsubmit inline event
handler. Remove that from the form tag and try this code:
$(document).ready(function() {
// bind submit event for your form
$('form').submit(function() {
// capture form data
var data = $(this).serialize();
// post data to server
$.ajax({
type: "POST",
url: "some.php",
data: data,
success: function(msg){
alert( "Data Saved: " + msg );
}
});
// prevent default browser submit behavior
return false;
});
});