Form POST不会将变量传输到PHP脚本
我有一个问题,我已经好几天没能解决了,我快疯了 我有一个HTML表单,我试图将这些变量从表单传递到我的PHP脚本,但由于某些原因,它们没有传递 HTML格式:Form POST不会将变量传输到PHP脚本,php,html,forms,post,http-post,Php,Html,Forms,Post,Http Post,我有一个问题,我已经好几天没能解决了,我快疯了 我有一个HTML表单,我试图将这些变量从表单传递到我的PHP脚本,但由于某些原因,它们没有传递 HTML格式: <div class="col-md-4 col-sm-12"> <a name="contactus"></a> <div class="contact-form bottom"> &l
<div class="col-md-4 col-sm-12">
<a name="contactus"></a>
<div class="contact-form bottom">
<h2>Send a message</h2>
<form name="contact form" method="POST" action="sendemail_1.php" id="main-contact-form">
<div class="form-group">
<input type="text" name="name" id="names" class="form-control" required="required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="email" id="emails" class="form-control" required="required" placeholder="Email">
</div>
<div class="form-group">
<input type="text" name="number" id="numbers" class="form-control" required="required" placeholder="Number">
</div>
<div class="form-group">
<textarea name="message" id="messages" required="required" class="form-control" rows="8" placeholder="Your text here"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-submit" value="Submit">
</div>
</form>
</div>
</div>
发送消息
完整的PHP脚本:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '*****@gmail.com';
$mail->Password = '****';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('*****@gmail.com', 'Test');
$mail->addReplyTo('****@gmail.com', 'Test');
$mail->addAddress("****@hotmail.com");
$mail->isHTML(false); // Set email format to HTML
$bodyContent = "test";
$name = filter_input(INPUT_POST, 'name');
$number = filter_input(INPUT_POST, 'number');
$email = filter_input(INPUT_POST, 'email');
$message = filter_input(INPUT_POST, 'message');
$mail->Subject = 'Test';
$mail->Body = $bodyContent;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
error_reporting(E_ALL);
?>
使用的JS
// Contact form
var form = $('#main-contact-form');
form.submit(function(event){
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
$.ajax({
url: $(this).attr('action'),
beforeSend: function(){
form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn() );
}
}).done(function(data){
form_status.html('<p class="text-success">Thank you for contact us. As early as possible we will contact you</p>').delay(3000).fadeOut();
});
});
//联系方式
变量形式=$(“#主联系人形式”);
表单提交(功能(事件){
event.preventDefault();
变量形式_状态=$('');
$.ajax({
url:$(this.attr('action'),
beforeSend:function(){
form.prepend(form_status.html(“电子邮件正在发送…”).fadeIn();
}
}).完成(功能(数据){
form_status.html('感谢您与我们联系。我们将尽快与您联系。
')。延迟(3000)。淡出();
});
});
我也用$\u POST('name')尝试过,但它也不起作用。
非常感谢您的帮助!谢谢好的发现了问题-您需要将提交按钮名称作为一个提交未提交 请尝试以下代码: 您的html文件:
评论开始变得太大,这对于评论来说太大了 要进行测试,请使用下面的内容,而不是您现在拥有的内容 更改为:
$name = filter_input(INPUT_POST, 'name');
$number = filter_input(INPUT_POST, 'number');
$email = filter_input(INPUT_POST, 'email');
$message = filter_input(INPUT_POST, 'message');
致:
- 我看不出这是失败的
注: 我看到ID无处不在,这表明您可能正在使用其他Javascript来处理此问题 如果是这种情况,那么您需要确保语法正确
这在我早些时候给您的评论中没有得到回答。那么您面临任何错误吗?或者它只显示空白?空白,没有错误。谢谢尝试打印post和请求变量。打印(邮政美元);打印请求($);这两个文件在同一目录中吗?是的,在同一目录中。它发送电子邮件,只是不包含任何变量。它们都是空白的。谢谢你的回复,但它不起作用。还有其他想法吗?你复制粘贴了我的两个文件的代码吗?在这里工作很好,我也检查过了,在你的sendemail_1.php文件中写代码,我在你的php文件中写的代码是的,我刚刚尝试过,没有运气,它让我发疯。我创建了相同的文件,在这里工作很好。再次检查我更新了答案我正在我的ec2主机上部署它,如何查看打印消息?我仍然没有收到我发送的电子邮件中的任何值。它也不起作用,我已经用表单中使用的JS更新了帖子。再次感谢您的帮助:)
if(isset($_POST['submit']))
{
print_r($_POST);
}
$name = filter_input(INPUT_POST, 'name');
$number = filter_input(INPUT_POST, 'number');
$email = filter_input(INPUT_POST, 'email');
$message = filter_input(INPUT_POST, 'message');
if(
!empty($_POST['name'])
&&
!empty($_POST['number'])
&&
!empty($_POST['email'])
&&
!empty($_POST['message'])
) {
$name = $_POST['name'];
$number = $_POST['number'];
$email = $_POST['email'];
$message = $_POST['message'];
}
else{
echo "No empty fields please.";
exit; // This will STOP any further execution.
}
// the rest of your code below