Php 以ajax注释形式获取post参数
我正试图从我购买的模板中定制一个评论表单。简而言之,我有3个文件-Php 以ajax注释形式获取post参数,php,jquery,ajax,Php,Jquery,Ajax,我正试图从我购买的模板中定制一个评论表单。简而言之,我有3个文件-post.php,comment\u post.php和main.js。php中的post.php是简单的html注释表单。我在ajax方面不是很好,我还在努力学习php,所以我需要一些帮助 <form class="row" role="form" id="comments-form" name="comments-form" action="comments-send.php" method="POST">
post.php
,comment\u post.php
和main.js
。php中的post.php
是简单的html注释表单。我在ajax
方面不是很好,我还在努力学习php,所以我需要一些帮助
<form class="row" role="form" id="comments-form" name="comments-form" action="comments-send.php" method="POST">
<input type="text" class="form-control form-name-error" name="comments[form-name]" id="form-name" placeholder="Name">
<input type="email" class="form-control form-email-error" id="form-email" name="comments[form-email]" placeholder="Email">
<input type="hidden" name="comments[post_id]" value="<?php echo $row['post_id'];?>" >
<textarea class="form-control input-row-2 form-review-error" rows="3" id="form-comments" name="comments[form-review]" placeholder="Comment"></textarea>
<div class="form-group text-right btn-submit">
<button type="submit" class="btn btn-dark button-submit">Send</button>
<div class="message-success alert-success alert hidden" style="position: absolute"><i class="fa fa-check"></i></div>
</div>
</form>
在原始文件(comment\u post.php
)中,没有用于插入数据库的内容,这是我的代码。我不确定在发送到php部件时如何从表单中获取值。这是来自main.js
文件的comment\u表单
$("#comments-form").submit(function(e) {
$('#comments-form .form-control').removeClass('#comments-form message-error');
$.post("comments-send.php", $('#comments-form').serialize(), function(data) {
if (data.status === 'ok') {
$("#comments-form .message-success").removeClass('hidden').velocity({ opacity : 1 });
$("#comments-form .button-submit").addClass('button-transparent');
$('#comments-form .form-control').val('');
setTimeout(function() {
$("#comments-form .message-success").velocity({ opacity : 0 }, function() {
$(this).addClass('hidden');
});
$("#comments-form .button-submit").removeClass('button-transparent');
}, 3000);
} else {
$.each(data.errors, function(i, e) {
$('.' + i).addClass('#comments-form message-error');
});
}
}, 'json');
e.preventDefault();
});
$("#comments-form").on('keyup', '.contact-form', function() {
var that = this;
if ($(this).val() !== '') {
$(this).removeClass('message-error');
} else {
$(that).addClass('message-error');
}
});
看起来您没有正确设置变量 更新此
$comment_author_name = $_POST['comments']['form-name'];
$comment_author_email = $_POST['comments']['form-email'];
$post_id = $_POST['comments']['post_id'];
$comment_text = $_POST['comments']['form-review'];
实际上,您要做的是从$\u POST中获取值,并将它们保存到您创建的变量中
之前你在做oposite,因此变量不存在,你也在重置$\u POST中的值。。。这就是问题所在,但是
$\u POST['comments']['form-name']=$comment\u author\u name
和$comment\u author\u name=$\u POST['comments']['form-name']代码>非常感谢。我真的不知道这两种方式都有区别;将$comment\u author\u name的值设置为$\u POST['comments']['form-name']。而$comment\u author\u name=$\u POST['comments']['form-name']将$\u POST['comments']['form-name']的值设置为变量$comment\u author\u name.nice:)很高兴我能提供帮助
$comment_author_name = $_POST['comments']['form-name'];
$comment_author_email = $_POST['comments']['form-email'];
$post_id = $_POST['comments']['post_id'];
$comment_text = $_POST['comments']['form-review'];