将表单PHP修改提交到jQueryAjax
我希望将下面的php表单提交转换为jQueryAjax提交。我以前在一些简单的请求中使用过Ajax,但我不确定如何从MySQL提交和返回以下代码的数据 下面的代码将用户输入条目提交到MySql查询,返回单列行。然后,While循环查看这些行并触发另一个mysql查询,返回每行用户喜欢的数量将表单PHP修改提交到jQueryAjax,php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,我希望将下面的php表单提交转换为jQueryAjax提交。我以前在一些简单的请求中使用过Ajax,但我不确定如何从MySQL提交和返回以下代码的数据 下面的代码将用户输入条目提交到MySql查询,返回单列行。然后,While循环查看这些行并触发另一个mysql查询,返回每行用户喜欢的数量 <?php if(!empty($_POST['Message'])) { $userid = session_id(); $searchStr = get_post($con
<?php
if(!empty($_POST['Message']))
{
$userid = session_id();
$searchStr = get_post($con,'Message');
$aKeyword = explode(" ", $searchStr);
$aKeyword = array_filter($aKeyword);
$stmt = $con->prepare(
'SELECT m.ID, m.MessageText
FROM MessageMain m
LEFT OUTER JOIN Likes l on m.ID = l.PostID
WHERE MessageText REGEXP ?
GROUP BY m.ID, m.MessageText ORDER BY count(m.id) desc'
);
$regexString = implode('|', $aKeyword);
$stmt->bind_param('s', $regexString);
$stmt->execute();
$result = $stmt->get_result();
$rowcount=mysqli_num_rows($result);
echo "<pre> Returned ". $rowcount . " matches</pre>";
if(mysqli_num_rows($result) > 0) {
$row_count=0;
While($row = $result->fetch_assoc()) {
$postid = $row['ID'];
$row_count++;
// Checking user status
$status_query = $con->prepare("SELECT COUNT(*) AS type FROM likes WHERE userid = ? AND postid = ?");
$status_query->bind_param('ss',$userid,$postid);
$status_query->execute();
$status_result = $status_query->get_result();
$status_row = $status_result->fetch_assoc();
$type = $status_row['type'];
// Count post total likes
$like_query = $con->prepare("SELECT COUNT(*) AS cntLikes FROM likes WHERE postid = ?");
$like_query->bind_param('s',$postid);
$like_query->execute();
$like_result = $like_query->get_result();
$like_row = $like_result->fetch_assoc();
$total_likes = $like_row['cntLikes'];
?>
<div class="post">
<div class="post-text">
<?php
echo nl2br(htmlentities($row['MessageText'],ENT_COMPAT|ENT_IGNORE, "UTF-8") );
?>
</div>
<div class="post-action">
<input type="button" value="Like" id="like_<?php echo htmlentities($postid . "_" . $userid); ?>" class="like" style="<?php if($type == 1){ echo "color: #ffa449;"; } ?>" /> (<span id="likes_<?php echo $postid . "_" . $userid; ?>"><?php echo htmlentities($total_likes); ?></span>)
</div>
</div>
<?php
}
}
}
单击按钮时,需要从ajax中的表单发送
// Add the array declaration somewhere at the beginning of your script ( outside the while loop )
$likesData = array();
这里有一些例子。
$.post是ajax的快捷方式,即方法post
我认为最好是分割文件,而不是在一个文件中完成所有工作。
form.php->ajax request->backend.php->使用ajax检索的数据返回form.php单击按钮时,需要从ajax中的表单发送数据
// Add the array declaration somewhere at the beginning of your script ( outside the while loop )
$likesData = array();
这里有一些例子。
$.post是ajax的快捷方式,即方法post
我认为最好是分割文件,而不是在一个文件中完成所有工作。
php最简单的方法是重写php脚本,只返回数据,以便在JS中使用它来构建HTML
我的建议是简单地创建一个数组,并在while循环中将所有数据添加到此数组中,例如:
$likesData[] = array(
'ID' => $postid,
'type' => $type,
'totallikes' => $total_likes
);
然后在while循环中:
// Return the array as JSON, die script to ensure no other data gets send after the json response
die( json_encode( $likesData ) );
// Do the AJAX request
$.post( "yourscript/url/maybephpfile.php", function( jsonResponse ) {
// Loop over json response
for( var key of Object.keys( jsonResponse ) ) {
$( '.your_existing_element' ).append( `<div class="post"><div class="post-text">${jsonResponse[key].totallikes}</div></div>` );
}
});
然后在while循环之后:
// Return the array as JSON, die script to ensure no other data gets send after the json response
die( json_encode( $likesData ) );
// Do the AJAX request
$.post( "yourscript/url/maybephpfile.php", function( jsonResponse ) {
// Loop over json response
for( var key of Object.keys( jsonResponse ) ) {
$( '.your_existing_element' ).append( `<div class="post"><div class="post-text">${jsonResponse[key].totallikes}</div></div>` );
}
});
然后在JS(jQuery)中执行如下操作:
//执行AJAX请求
$.post(“yourscript/url/maybephpfile.php”,函数(jsonResponse){
//循环json响应
for(Object.keys的var键(jsonResponse)){
$('.your_existing_element').append(`${jsonResponse[key].totalikes}`);
}
});
希望这对您有所帮助,如果您有任何问题,请告诉我。最简单的方法是重写PHP脚本,只返回数据,这样您就可以在JS中使用它来构建HTML
我的建议是简单地创建一个数组,并在while循环中将所有数据添加到此数组中,例如:
$likesData[] = array(
'ID' => $postid,
'type' => $type,
'totallikes' => $total_likes
);
然后在while循环中:
// Return the array as JSON, die script to ensure no other data gets send after the json response
die( json_encode( $likesData ) );
// Do the AJAX request
$.post( "yourscript/url/maybephpfile.php", function( jsonResponse ) {
// Loop over json response
for( var key of Object.keys( jsonResponse ) ) {
$( '.your_existing_element' ).append( `<div class="post"><div class="post-text">${jsonResponse[key].totallikes}</div></div>` );
}
});
然后在while循环之后:
// Return the array as JSON, die script to ensure no other data gets send after the json response
die( json_encode( $likesData ) );
// Do the AJAX request
$.post( "yourscript/url/maybephpfile.php", function( jsonResponse ) {
// Loop over json response
for( var key of Object.keys( jsonResponse ) ) {
$( '.your_existing_element' ).append( `<div class="post"><div class="post-text">${jsonResponse[key].totallikes}</div></div>` );
}
});
然后在JS(jQuery)中执行如下操作:
//执行AJAX请求
$.post(“yourscript/url/maybephpfile.php”,函数(jsonResponse){
//循环json响应
for(Object.keys的var键(jsonResponse)){
$('.your_existing_element').append(`${jsonResponse[key].totalikes}`);
}
});
希望这对您有所帮助,如果您有任何问题,请告诉我。不知道是否有人可以提供一些建议。谢谢。我想知道是否有人能提供一些建议。谢谢。谢谢你的回复@Cristo。我理解上面的逻辑,它更复杂的是为每条消息添加like的数量,并将其与like按钮的html合并,等等。为该编号添加一列。尽管如此,我还是会一次完成两个查询。比如加入什么。我认为这比为每一行选择查询性能更好谢谢您的回复@Cristo。我理解上面的逻辑,它更复杂的是为每条消息添加like的数量,并将其与like按钮的html合并,等等。为该编号添加一列。尽管如此,我还是会一次完成两个查询。比如加入什么。我认为这比为每一行选择查询性能更好谢谢你@Lennart。原谅我的无知。$.post与$.ajax({url:,method:,data:})有何不同@CGarden它只是一个$.ajax
的简写功能,在大多数情况下,您不需要完整的$.ajax
功能,因此他们添加了$.post
和$.get
。更多信息请参见文档:上面的循环似乎在我的屏幕上显示数据一毫秒,然后就消失了。不知道它为什么这么做。。。。谢谢新年快乐@CGarden很抱歉响应太晚,这听起来像是JS中的问题,您是否对现有元素使用了append()
方法?如果使用html()
功能,则可能会覆盖每个循环中的数据。如果你使用jsfiddle或者类似的东西来分享你的JS,我可以帮你看看。谢谢@Lennart。原谅我的无知。$.post与$.ajax({url:,method:,data:})有何不同@CGarden它只是一个$.ajax
的简写功能,在大多数情况下,您不需要完整的$.ajax
功能,因此他们添加了$.post
和$.get
。更多信息请参见文档:上面的循环似乎在我的屏幕上显示数据一毫秒,然后就消失了。不知道它为什么这么做。。。。谢谢新年快乐@CGarden很抱歉响应太晚,这听起来像是JS中的问题,您是否对现有元素使用了append()
方法?如果使用html()
功能,则可能会覆盖每个循环中的数据。如果您使用JSFIDLE或类似的东西共享您的JS,我可以看一看并帮助您。