javascript 不使用 PHP 提交联系表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10181715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:55:52  来源:igfitidea点击:

Submit Contact Form without using PHP

phpjavascriptformsfeedbackcontact-form

提问by

I'm still a student and today our lecturer told us that the only way in order to submit a contact us form without having to use the mailto: function is to use PHP.

我仍然是一名学生,今天我们的讲师告诉我们,提交联系我们表格而不必使用 mailto: 功能的唯一方法是使用 PHP。

I swear that last year another lecturer showed us a way using only javascript.

我发誓去年另一位讲师向我们展示了一种仅使用 javascript 的方法。

Is it possible to submit a feedback/contact form using a basic form and javascript ? If so, do you mind sharing a sample example ?

是否可以使用基本表格和 javascript 提交反馈/联系表格?如果是这样,您介意分享一个示例吗?

Thanks in advance

提前致谢

采纳答案by Brad

You can use JavaScript to redirect to mailto:with some parameters, but that isn't ideal. Also keep in mind that anything beyond an e-mail address in a mailto:URL is non-standard. While it will work for many browsers, it won't work for everything.

您可以使用 JavaScript 重定向到mailto:一些参数,但这并不理想。还要记住,mailto:URL 中电子邮件地址以外的任何内容都是非标准的。虽然它适用于许多浏览器,但它并不适用于所有浏览器。

The best way to send e-mail is to do it server-side, using PHP or something else.

发送电子邮件的最佳方式是在服务器端进行,使用 PHP 或其他方式。

回答by Billy

A form is created/submitted by the browser based on HTML. JavaScript is commonly used to enhance forms (in many different ways). Basic forms send their data as a POST request. To do anything useful with the data you need server-side code to handle the POST request (such as PHP).

表单是由浏览器基于 HTML 创建/提交的。JavaScript 通常用于增强表单(以许多不同的方式)。基本表单将其数据作为 POST 请求发送。要对数据执行任何有用的操作,您需要服务器端代码来处理 POST 请求(例如 PHP)。

回答by Jason Foglia

You can't submit a form to javascript and expect it to do anything. You need a server side script in order to receive a form for processing. Mailto is a browser/os specific method and is not necessarily going to invoke anything. Javscript can process your form before going to the server for further processing, but like I said you need to submit to a server.

您不能向 javascript 提交表单并期望它做任何事情。您需要一个服务器端脚本才能接收表单进行处理。Mailto 是浏览器/操作系统特定的方法,不一定要调用任何东西。Javscript 可以在进入服务器进行进一步处理之前处理您的表单,但就像我说的那样,您需要提交到服务器。

回答by Ahmet Can Güven

Javascript cant capture anything without PHP or browser support. You need a server side script to receive a form and process it. Javascript without PHP or another server side script can't capture form.

没有 PHP 或浏览器支持,Javascript 无法捕获任何内容。您需要一个服务器端脚本来接收表单并对其进行处理。没有 PHP 或其他服务器端脚本的 Javascript 无法捕获表单。

回答by Ivan Semkin

You can use a service like emailjs.com:

您可以使用emailjs.com 之类的服务:

1) Configure EmailJS:

1)配置EmailJS:

<script type="text/javascript" src="https://cdn.emailjs.com/sdk/2.2.4/email.min.js"></script>
<script type="text/javascript">
   (function(){
      emailjs.init("user_USERID");
   })();
</script>

2) Send the Email:

2)发送电子邮件:

var service_id = 'my_mandrill';
var template_id = 'feedback';
var template_params = {
    name: 'John',
    reply_email: '[email protected]',
    message: 'This is awesome!'
};

emailjs.send(service_id,template_id,template_params);

It has a limit of 200 free emails per month, which will suite a resume/portfolio website. reCaptcha is supported.

它有每月 200 封免费电子邮件的限制,适用于简历/作品集网站。支持 reCaptcha。

EmailJS supports different Email providers (including a custom SMTP server), and provides you with logs and statistics.

EmailJS 支持不同的电子邮件提供商(包括自定义 SMTP 服务器),并为您提供日志和统计信息。