Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
163
rated 0 times [  168] [ 5]  / answers: 1 / hits: 14603  / 11 Years ago, thu, february 6, 2014, 12:00:00

I want to submit a form without refreshing the page, from what I have read it should work with ajax, what am i doing wrong?



it all works with the php and stuff when I do this:



document.getElementById(msg_form).submit();


But I want it to submit without refreshing the page.



Part of Html:



<form name=msg_form_name id=msg_form class=email action=mailer.php>
<p>Your E-mail:</p>
<input id=email_form name=email type=text />
<p>Amount:</p>
<input id=amount_form name=amount class=amount_num type=text maxlength=5 />
<div id=msg_txt_lenght>characters left: 38</div>
<p>Message:</p>
<input id=message_form name=message class=message_form_lim type=text >
<input type=hidden id=storeUrl_id name=storeUrl_form value=Nan></form>
</form>


Part of javascript:



$('#msg_form').submit(function (e) {
e.preventDefault();

$.ajax({
type: 'post',
url: 'mailer.php',
data: $('form').serialize(),
success: function () {
alert('form was submitted');
}
});

return false;
});


Thanks in advance.



EDIT: might have fixed it had it on submit but didn't send a submit


More From » php

 Answers
14

Solved it just replaced:



$('#msg_form').submit(function (e) {
e.preventDefault();

$.ajax({
type: 'post',
url: 'mailer.php',
data: $('#msg_form').serialize(),
success: function () {
alert('form was submitted');
}
});

return false;
});


with this instead



$.post('mailer.php', $('#msg_form').serialize())

[#47975] Thursday, February 6, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
carl

Total Points: 633
Total Questions: 105
Total Answers: 105

Location: Fiji
Member since Wed, Jul 14, 2021
3 Years ago
;