小编典典

提交表单而无需重新加载页面

html

我有一个分类广告网站,在显示广告的页面上,我正在创建“向朋友发送提示”表单…

因此,任何想要的人都可以将广告提示发送给某些朋友的电子邮件地址。

我猜该表格必须提交到php页面,对吗?

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

提交表单时,页面被重新加载…我不想要…

有什么办法可以使其不重新加载并仍然发送邮件?最好没有ajax或jquery …

谢谢


阅读 377

收藏
2020-05-10

共1个答案

小编典典

您需要提交ajax请求来发送电子邮件,而无需重新加载页面。

您的代码应类似于以下内容:

$('#submit').click(function() {
    $.ajax({
        url: 'send_email.php',
        type: 'POST',
        data: {
            email: 'email@example.com',
            message: 'hello world!'
        },
        success: function(msg) {
            alert('Email Sent');
        }               
    });
});

该表格将在后台提交到该send_email.php页面,该页面需要处理请求并发送电子邮件。

2020-05-10