我已经尝试了一段时间,接下来是一些不同的教程和论坛帖子。这似乎很简单,但我无法从textarea获取任何数据。输入类型"名称" &安培; "邮件"好的,它正在工作。我将在下面粘贴我的PHP编码和表单。任何帮助将非常感激。感谢。
<section id="forma" >
<div class="container">
<div class="row">
<div class="col-md-12 text-center">
<form action="send.php" method="post" enctype="multipart/form-data" id="form_page">
<input type="text" name="name" placeholder="Name" required>
<input type="email" name="mail" placeholder="E-mail" required>
<textarea name="comment" id="comment" style="margin: 0px 15px -33px 0px; width: 307px; height: 66px;"></textarea><br>
<label class="uploadbutton">
<div class="button" ></div>
<div class='input'></div>
<input type="file" name="file" onchange="this.previousSibling.previousSibling.innerHTML = this.value"/>
</label>
<button type="submit" class="btn btn2"></button>
</form>
</div>
</div>
</div>
</section>
腓
<?php
require_once('phpmailer/PHPMailerAutoload.php');
$mail = new PHPMailer;
$mail->CharSet = 'utf-8';
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['mail'];
$text = $_POST['message'];
$comment = $_POST['comment'];
echo $comment;
$mail->SMTPDebug = 3;
//$mail->isSMTP();
//$mail->Host = '';
//$mail->SMTPAuth = true;
//$mail->Username = '';
//$mail->Password = '';
//$mail->SMTPSecure = 'ssl';
//$mail->Port = 587;
$mail->setFrom('');
$mail->addAddress('');
//$mail->addAddress('');
//$mail->addReplyTo('', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
//$mail->addAttachment('/var/tmp/file.tar.gz');
$mail->isHTML(true);
if(isset($_FILES['file']))
$mail->addAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']); // Optional name$mail->isHTML(true);
$mail->Subject = '';
$mail->Body = '' .$name . ' ,' .$comment. '<br>: ' .$email.'<br>' .$text;
$mail->AltBody = '';
if(!$mail->send()) {
echo 'Error';
} else {
header('location: thank-you.html');
}
?>
JS
<script>
$ ('form').submit(function(event) {
event.preventDefault();
var data = new FormData();
data.append('name', $(this).find('input[name=\'name\']').val());
data.append('phone', $(this).find('input[name=\'phone\']').val());
data.append('mail', $(this).find('input[name=\'mail\']').val());
data.append('comment',$('#comment').val());
if($(this).attr('id') == 'form_page')
data.append('file', $(this).find('input[name=\'file\']')[0].files[0])
$.ajax({
type: "POST",
url: "send.php",
data: data,
cache: false,
contentType: false,
processData: false,
success: (function(data) {
$(this).find("input").val("");
$('#modal').modal('hide');
$('#modal1').modal('show');
$("form").trigger("reset");
})
});
return false;
});
</script>
答案 0 :(得分:0)
你的错误是在PHP代码中:
取代:
$comment = $_POST['comment']);
使用:
$comment = $_POST['comment'];
删除额外的括号
答案 1 :(得分:0)
在js文件中替换$(this).find('input [name = \'comment \']')。val()和$('#comment')。val() 因为你正在使用textarea输入选择器,它不能用于获取textarea值