无需重新加载页面即可提交表单 [英] Submit form without page reloading
问题描述
我有一个分类广告网站,在展示广告的页面上,我正在创建向朋友发送提示"表格...
所以任何想要的人都可以将广告的提示发送给一些朋友的电子邮件地址.
我猜表单必须提交到 php 页面对吗?
提交表单时,页面会重新加载...我不希望那样...
有什么办法可以让它不重新加载并仍然发送邮件?最好没有 ajax 或 jquery...
您需要提交 ajax 请求才能在不重新加载页面的情况下发送电子邮件.看看http://api.jquery.com/jQuery.ajax/>
你的代码应该是这样的:
$('#submit').click(function() {$.ajax({url: 'send_email.php',类型:'POST',数据: {电子邮件:'email@example.com',消息:你好世界!"},成功:功能(味精){alert('邮件已发送');}});});
表单将在后台提交到 send_email.php
页面,该页面将需要处理请求并发送电子邮件.
I have a classifieds website, and on the page where ads are showed, I am creating a "Send a tip to a friend" form...
So anybody who wants can send a tip of the ad to some friends email-adress.
I am guessing the form must be submitted to a php page right?
<form name="tip" method="post" action="tip.php">
Tip somebody:
<input
name="tip_email"
type="text"
size="30"
onfocus="tip_div(1);"
onblur="tip_div(2);"
/>
<input type="submit" value="Skicka Tips" />
<input type="hidden" name="ad_id" />
</form>
When submitting the form, the page gets reloaded... I don't want that...
Is there any way to make it not reload and still send the mail? Preferrably without ajax or jquery...
You'll need to submit an ajax request to send the email without reloading the page. Take a look at http://api.jquery.com/jQuery.ajax/
Your code should be something along the lines of:
$('#submit').click(function() {
$.ajax({
url: 'send_email.php',
type: 'POST',
data: {
email: 'email@example.com',
message: 'hello world!'
},
success: function(msg) {
alert('Email Sent');
}
});
});
The form will submit in the background to the send_email.php
page which will need to handle the request and send the email.
这篇关于无需重新加载页面即可提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!