我有这个问题,我现在几天都无法修复,而且我会发疯...
我有一个HTML表单,我试图将这些变量从表单传递到我的PHP脚本但由于某种原因它们没有被传递。
HTML表单:
<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();
});
});
我也尝试使用$ _POST('name')但它也不起作用。
非常感谢任何帮助!感谢
答案 0 :(得分:1)
评论开始变得过大,这对于评论来说太大了。
要进行测试,请使用以下内容而不是现在的内容。
更改自:
$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
注意:
我看到ID无处不在,这表明您可能正在使用其他Javascript来处理此问题。
如果是这种情况,那么您需要确保语法正确。
我之前对你的评论没有回答。
答案 1 :(得分:0)
好的发现问题 - 您需要将提交按钮名称作为提交而不是提交传递。
尝试以下代码:
你的html文件:
<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>
您的Php文件
if(isset($_POST['submit']))
{
print_r($_POST);
}