php - 提交表单 PHP 修改为 JQuery AJAX
问题描述
我希望将以下 php 表单提交转换为 JQuery Ajax 提交。我之前使用过 Ajax 和一些简单的请求,但我不确定如何从 MySQL 提交和返回以下代码的数据。
下面的代码将用户输入条目提交给返回单列行的 MySql 查询。然后,一个 While 循环查看这些行并触发另一个 mysql 查询,返回每行用户喜欢的数量。
<?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
}
}
}
解决方案
最简单的方法是重写您的 PHP 脚本以仅返回数据,以便您可以在 JS 中使用它来构建 HTML。
我的建议是简单地创建一个数组并将所有数据添加到你的while循环中的这个数组中,例如:
// Add the array declaration somewhere at the beginning of your script ( outside the while loop )
$likesData = array();
然后在你的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 ) );
然后在你的 JS ( jQuery ) 中做这样的事情:
// 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>` );
}
});
希望这对您有所帮助,如果您有任何问题,请告诉我。
推荐阅读
- google-sheets - Google Sheets API:调用者没有权限
- python - Python 没有看到它在它的路径中肯定有的模块
- css - 使用 CSS 更改 Shiny titlePanel 的颜色
- php - 来自路由器的 Laravel 5 替代调用控制器
- neo4j - 当关系已经存在时如何记录?
- c# - 创建事件源作为 Visual Studio 安装项目安装程序的一部分
- angular - 使用弹簧安全性使 Http Connection 请求标头保持有角度的活动
- reactjs - Redux 状态正在更新,而无需分派任何操作
- vba - 使用 VBA 将 Word 表复制到 Excel 时出错
- amazon-redshift - 如何防止亚马逊红移中的重复条目