最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Submit form without refreshing page ajax,php,javascript? - Stack Overflow

matteradmin11PV0评论

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

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

Share edited Feb 6, 2014 at 20:14 Patrik Fröhler asked Feb 6, 2014 at 19:30 Patrik FröhlerPatrik Fröhler 1,2911 gold badge12 silver badges39 bronze badges 7
  • 1 So whats the problem? Does it work? What doesn't work? – Jake N Commented Feb 6, 2014 at 19:35
  • Try POST in all caps...not sure if that is madatory but is good practice – VIDesignz Commented Feb 6, 2014 at 19:35
  • Do you have JQuery installed?? – VIDesignz Commented Feb 6, 2014 at 19:36
  • Target the form to serialize $('#msg_form').serialize(); – VIDesignz Commented Feb 6, 2014 at 19:38
  • I don't get the alert msg also and when It works and it post to the php I receive an email but, I don't get the email so for sure it don't work. yes I have JQuery installed. – Patrik Fröhler Commented Feb 6, 2014 at 19:40
 |  Show 2 more ments

1 Answer 1

Reset to default 4

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())

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far