[英]My ajax call is not giving any error and response
Ajax调用没有响应给我一个空结果(PHP)代码可以正常工作,只有我与ajax有关的问题。 我已经检查了所有CDN是否正常
我的html代码:
<form method="post">
<input type="hidden" name="product_id" id="product_id" value="<?= $id ?>" >
<input type="hidden" name="user_id" id="user_id" value="<?= $_SESSION['id'] ?>">
<textarea name="comment" id="comment" cols="30" rows="10"></textarea>
<input type="hidden" name="rating" id="rating">
<input type="submit" name="c_submit" id="c_submit">
</form>
<ol id="comment"> </ol>
我的PHP代码:
<?php
include "../../functions/dbs.php";
if (isset($_POST["c_submit"])){
$product_id = $_POST['product_id']; $user_id = $_POST['user_id'];
$comment = $_POST['comment']; $rating = $_POST['rating'];
$query = "INSERT INTO user_profile (product_id,user_id,comment,ratting) values ('$product_id','$user_id','$comment','$rating')";
print_r($query);exit();
$sql=mysqli_query($con->connect(),$query);
?>
<li class="box">
<?= '<span class="email">' . $product_id. '</span>' ?>
<?= '<span class="comment">' . $comment . '</span>' ?>
</li>
<?php } ?>
我的ajax代码:如果用户可以单击“提交”,则在ol#comment中将闪烁我们在输入字段中拥有的所有结果。
$(function () {
$("#c_submit").click(function() {
var product_id = $("#product_id").val();
var user_id = $("#user_id").val();
var comment = $("#comment").val();
var rating = $("#rating").val();
var dataString = 'product_id =' + product_id + '&user_id=' + user_id + '&comment=' + comment + '&rating=' + rating;
if (product_id == '' || user_id == '' || comment == '' || rating ==''){
alert('Please Give Valid Details');
}else {
$.ajax({
type: "POST",
url : "include/comm.php",
data : dataString,
cache: false,
success : function(html){
console.log(html);
$("ol#comment").append(html);
$("ol#comment li:last").fadeIn("slow");
}
});
}
});
});
忽视 :
“快速的棕狐狸跳过了一只懒狗。当MTV斧头测验节目前,DJ蜂拥而至。垃圾MTV测验被狐狸幼崽所宠爱。鲍德斯慢跑,轻弹石英,VEX若虫。抢快节奏的华尔兹。砖测验“
我尝试了您的代码段,发现了几个问题:
我的代码建议:
$(function () {
$("form").on('submit', function(e) {
e.preventDefault();
...
var dataString = 'product_id=' + product_id + '&user_id=' + user_id + '&comment=' + comment + '&rating=' + rating;
...
});
});
对于PHP对应对象,假设始终提交product_id数据,我建议:
if (isset($_POST["product_id"])){
....
}
您必须首先阻止click或Submit事件的默认行为。 您可以通过在事件处理程序的开头添加这一行来做到这一点。 另外,我从未见过使用jQuery对象的$
符号调用匿名函数的方式。 我将假定此函数是整个应用程序的根目录或包装函数,并且将向您展示我的工作方式和人们的工作方式。
(function($) {
$(document).ready(function() {
$(document).on('submit', '#info_form', function(event) {
event.preventDefault();
// Add this line.
var product_id = $("#product_id").val();
var user_id = $("#user_id").val();
var comment = $("#comment_input").val();
var rating = $("#rating").val();
var data = {
product_id: product_id,
user_id: user_id,
comment: comment,
rating: rating
};
if (product_id == '' || user_id == '' || comment == '' || rating == '') {
alert('Please Give Valid Details');
}
else {
$.ajax({
type: "POST",
url: "include/common.php",
data: data,
cache: false,
success: function(html) {
console.log(html);
$("ol#comment").append(html);
$("ol#comment li:last").fadeIn("slow");
}
});
}
});
});
})(window.jQuery);
按照您监听submit
事件的方式。 我认为,如果您现在开始更改HTML内容或操纵与表单本身相关的DOM,那么您现在的操作方式将引起一些问题。 我更喜欢使用$(document).on('submit', "#my-form-id", function(e){});
这样,侦听器将被绑定到文档,而不是元素本身。 我不知道这是否是一种不好的做法,但过去它解决了我的很多问题。 另外,我倾向于将事件监听器代码包装在$(document).ready(function(){});
该函数可以避免在DOM树完成加载之前执行代码,或者避免在我的应用中使用所需的库。
同样对于数据,因为您正在使用发布请求发送数据。 您必须以要在后端访问数据的方式来保留数据的结构。 在您的前端代码中,您正在使用字符串连接来连接参数,这是错误的。 jQuery中的AJAX
函数获取数据并对其进行序列化。 因此,在您的情况下,您必须按照以下步骤操作:
var product_id = $("#product_id").val();
var user_id = $("#user_id").val();
var comment = $("#comment").val();
var rating = $("#rating").val();
var data = {
product_id: product_id,
user_id: user_id,
comment: comment,
rating: rating
};
将您的ol
ID更改为comment_answer
或其他所需内容,因为您已经在input
comment
ID。
<ol id='comment_answer'></ol>
您需要防止表单提交的默认行为,现在,当您调用“提交”按钮时,应该刷新页面,但不应该这样做,因为只写return false;
在ajax调用之后,在ajax成功函数中将ol#comment
更改为ol#comment_answer
$(function () {
$("#c_submit").click(function() {
var product_id = $("#product_id").val();
var user_id = $("#user_id").val();
var comment = $("#comment").val();
var rating = $("#rating").val();
var dataString = 'product_id =' + product_id + '&user_id=' + user_id + '&comment=' + comment + '&rating=' + rating;
if (product_id == '' || user_id == '' || comment == '' || rating ==''){
alert('Please Give Valid Details');
}else {
$.ajax({
type: "POST",
url : "include/comm.php",
data : dataString,
cache: false,
success : function(html){
console.log(html);
$("ol#comment_answer").append(html);
$("ol#comment_answer li:last").fadeIn("slow");
}
});
}
return false;
});
});
变化
1)整个页面上的ID不能相同。 a) <textarea name="comment" id="comment"
& <ol id="comment"></ol>
。 将其更改为<ol id="show_comment"></ol>
。
2)由于没有提供任何action
,因此也没有提供e.preventDefault();
,它将采用当前网址。 为了防止它,添加e.preventDefault();
在脚本中。
3)在include/comm.php
文件中,第一个条件是检查if (isset($_POST["c_submit"])){
。 在ajax脚本中没有传递c_submit
属性。 因此,它不会使您陷入困境。 在ajax脚本中传递c_submit
。
4)在<?= '<span class="email">' . $email . '</span>' ?>
<?= '<span class="email">' . $email . '</span>' ?>
<?= '<span class="email">' . $email . '</span>' ?>
此行。
未定义索引
定义$email
位置将为否。 所以,检查一下。 我已将其替换为$user_id
形成
<form method="post">
<input type="hidden" name="product_id" id="product_id" value="<?= $id ?>" >
<input type="hidden" name="user_id" id="user_id" value="<?= $_SESSION['id'] ?>">
<textarea name="comment" id="comment" cols="30" rows="10"></textarea>
<input type="hidden" name="rating" id="rating">
<input type="submit" name="c_submit" id="c_submit">
</form>
<ol id="show_comment"></ol>
包括/comm.php
<?php
include "../../functions/dbs.php";
if (isset($_POST["c_submit"])){
$product_id = $_POST['product_id'];
$user_id = $_POST['user_id'];
$comment = $_POST['comment'];
$rating = $_POST['rating'];
$query = "INSERT INTO user_profile (product_id,user_id,comment,ratting) values ('$product_id','$user_id','$comment','$rating')";
$sql= mysqli_query($con->connect(),$query);?>
<li class="box">
<?= '<span class="email">' . $user_id . '</span>' ?>
<?= '<span class="comment">' . $comment . '</span>' ?>
</li>
<?php }?>
脚本
<script>
$(function () {
$("#c_submit").click(function(e) {
e.preventDefault();
var product_id = $("#product_id").val();
var user_id = $("#user_id").val();
var comment = $("#comment").val();
var rating = $("#rating").val();
if (product_id === '' || user_id === '' || comment === '' || rating === ''){
alert('Please Give Valid Details');
} else {
$.ajax({
type: "POST",
url : "include/comm.php",
data : {
product_id : product_id, user_id : user_id, comment: comment, rating:rating,
c_submit: true
},
cache: false,
success : function(html){
console.log(html);
$("ol#show_comment").append(html);
$("ol#show_comment li:last").fadeIn("slow");
}
});
}
});
});
</script>
快速链接
在不回显PHP输出之前,响应将为空白。
将以下代码放入变量中,并回显输出:
$output = '<li class="box">
'<span class="email">' . $email . '</span>'
'<span class="comment">' . $comment . '</span>'
</li>';
并回显输出: echo $output
同样在JS中,当您发送html内容作为响应时,ajax的内容类型应该是html/text
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.