Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用php发送包含html表单数据的电子邮件_Javascript_Php_Html_Forms - Fatal编程技术网

Javascript 使用php发送包含html表单数据的电子邮件

Javascript 使用php发送包含html表单数据的电子邮件,javascript,php,html,forms,Javascript,Php,Html,Forms,我不知道php是如何工作的。我对每一个逻辑都使用JavaScript,但它只用于客户端,我认为php是用于服务器的。我收到一个405(方法不允许)错误 如何发送电子邮件(例如:example@gmail.com)和html表单中的输入数据 //**如果您需要它** //工作联系表 $(“#联系方式”)。提交(函数(){ var action=$(this.attr('action'); $(“#消息”).slideUp(750,函数(){ $(“#消息”).hide(); $('#submit

我不知道php是如何工作的。我对每一个逻辑都使用JavaScript,但它只用于客户端,我认为php是用于服务器的。我收到一个405(方法不允许)错误

如何发送电子邮件(例如:example@gmail.com)和html表单中的输入数据

//**如果您需要它**
//工作联系表
$(“#联系方式”)。提交(函数(){
var action=$(this.attr('action');
$(“#消息”).slideUp(750,函数(){
$(“#消息”).hide();
$('#submit')。在('.attr('disabled','disabled')之前;
$员额(行动{
名称:$('#name').val(),
电子邮件:$('#email').val(),
注释:$(“#注释”).val(),
},
功能(数据){
document.getElementById('message').innerHTML=data;
$('#message')。向下滑动('slideDown');
$('#cform img.contact loader').fadeOut('slow',function(){
$(this.remove())
});
$(“#提交”).removeAttr('disabled');
if(data.match('success')!=null)$('#cform').slideUp('slow');
}
);
});
返回false;
});
//示例starter JavaScript,用于在存在无效字段时禁用表单提交
(功能(){
“严格使用”
addEventListener('load',function()){
//获取要应用自定义引导验证样式的所有表单
var forms=document.getElementsByClassName('needs-validation')
//对它们进行循环并防止提交
Array.prototype.filter.call(表单,函数(表单){
表单.addEventListener('submit',函数(事件){
控制台日志(“测试”)
if(form.checkValidity()==false){
event.preventDefault()
event.stopPropagation()
}
form.classList.add('was-validated')
},错)
})
},错)
}())
//忽略CSS,没关系

每个
.php
文件都会以
打开

例如,您的HTML中也可以有一行PHP代码

<?php $title = 'Hello World'; ?>
关联数组类似于JavaScript中的对象。它们都有一个键和一个值。检查是否使用PHP中的函数设置了键

因为您允许用户发送数据,所以清理他们的数据很重要。它可能包含恶意代码,在您对其进行清理之前,应将其视为恶意代码。是一个PHP函数,可以为您实现这一点

现在您已经拥有并清理了数据,您可以使用
mail
功能向电子邮件地址发送邮件。
mail
函数根据邮件是否成功发送返回一个布尔值

祝你好运

<?php

/** 
 * Get all the send data from JS.
 * $_GET is a global PHP variable. It is an array
 * which contains the data sent with the GET method.
 */
$name     = isset($_GET['name']) ? $_GET['name'] : '';
$email    = isset($_GET['email']) ? $_GET['email'] : '';
$comments = isset($_GET['comments']) ? $_GET['comments'] : '';

/**
 * All data that a user can send must be treated as dirty.
 * Therefor it is important to filter all malicious characters
 * from the data. Otherwise you are open to vulnerabilities.
 */
$name     = filter_var($name, FILTER_SANITIZE_STRING);
$email    = filter_var($email, FILTER_SANITIZE_EMAIL);
$comments = filter_var($comments, FILTER_SANITIZE_FULL_SPECIAL_CHARS);

/**
 * Now the data is received and sanitized we can use it
 * to send an email with.
 */
$to      = $email; // The email address to send to.
$from    = 'sender@example.com'; // Your email address.
$subject = 'the subject'; 
$message = $comments; // I assume comments is the message. 
$headers = 'From: ' . $from . "\r\n" . 'Reply-To: ' . $from . "\r\n" . 'X-Mailer: PHP/' . phpversion(); 

/**
 * Send the email.
 * The mail() function returns a true or false based
 * on if the email was sent successfully.
 */
$mail_status = mail($to, $subject, $message, $headers);

/**
 * Send a message back to JS based on if the mail has beent sent or not.
 */
if ($mail_status === true) {
  return 'Mail has been sent';
} else {
  return 'Sending the mail has failed';
}

?>


@MarkOverton不,它不是。我已经看过这个帖子了。我的问题是,我收到一个405(不允许使用方法)错误,我不知道如何解决这个问题。然后,您发送
$.post
请求的地址不允许使用
post
方法。相反,请尝试
GET
方法,或允许
POST
。但是如果我们看不到您的PHP,就很难看到发生了什么。@EmielZuurbier谢谢您的提示。问题是,我以前从未使用过php。我不知道如何使用这个,但在其他一些stackoverflow.com网站上,他们说,我可以使用这个:
,这就是为什么我添加了js,所以你可以告诉我如何正确编写它。要添加的一件事。我仍然在控制台输出中得到了405(方法不允许)错误。这是一个错误,因为我使用VS Code Live Server还是我必须如何处理这个错误?不,这是因为您使用了$.post,正如名称所示,它使用post方法将数据从JS发送到PHP。改为对
GET
使用等效的jQuery函数。它甚至可能被称为
$。get
;)我将
$.post
更改为
$.get
,但仍然不起作用。我没有收到任何邮件。错误消息不再存在了,所以我认为这很好,现在我在vs代码中得到了另一个通知:
如果没有body或head标记,则无法进行实时重新加载。
。好吧,您必须在某种服务器上运行PHP。可以是本地的或托管的。研究如何设置PHP服务器。实时重新加载是不够的。
[
  'name' => '',
  'email' => '',
  'comments' => ''
]
<?php

/** 
 * Get all the send data from JS.
 * $_GET is a global PHP variable. It is an array
 * which contains the data sent with the GET method.
 */
$name     = isset($_GET['name']) ? $_GET['name'] : '';
$email    = isset($_GET['email']) ? $_GET['email'] : '';
$comments = isset($_GET['comments']) ? $_GET['comments'] : '';

/**
 * All data that a user can send must be treated as dirty.
 * Therefor it is important to filter all malicious characters
 * from the data. Otherwise you are open to vulnerabilities.
 */
$name     = filter_var($name, FILTER_SANITIZE_STRING);
$email    = filter_var($email, FILTER_SANITIZE_EMAIL);
$comments = filter_var($comments, FILTER_SANITIZE_FULL_SPECIAL_CHARS);

/**
 * Now the data is received and sanitized we can use it
 * to send an email with.
 */
$to      = $email; // The email address to send to.
$from    = 'sender@example.com'; // Your email address.
$subject = 'the subject'; 
$message = $comments; // I assume comments is the message. 
$headers = 'From: ' . $from . "\r\n" . 'Reply-To: ' . $from . "\r\n" . 'X-Mailer: PHP/' . phpversion(); 

/**
 * Send the email.
 * The mail() function returns a true or false based
 * on if the email was sent successfully.
 */
$mail_status = mail($to, $subject, $message, $headers);

/**
 * Send a message back to JS based on if the mail has beent sent or not.
 */
if ($mail_status === true) {
  return 'Mail has been sent';
} else {
  return 'Sending the mail has failed';
}

?>