php - 我想使用 foreach 循环 php 中的 ajax 获取单击行的 UID,但获取所有 UID
问题描述
我正在尝试从锚标记中获取 UID,该标记是 ajax 中 foreach 循环 PHP 中锚标记的 ID 属性的值。但它一次只能工作一次,当我再次单击锚标记时,所有的 UID 都会在 ajax 中被抓取!
当我单击锚标记时,第一次显示引导程序 4 模态,并且在模态中,带有按钮的 textarea 标记存在,当我单击按钮时,我必须再次将 uid 和消息发送到 action.php 页面
这是我的 action.php 代码
if(isset($_POST['action']) && $_POST['action'] == 'fetchAllFeedback'){
$feedback = $admin->fetchFeedback();
$output = '';
if($feedback){
$output .= '<table class="table table-striped table-bordered text-center">
<thead>
<tr>
<th>FID</th>
<th>UID</th>
<th>User Name</th>
<th>User E-Mail</th>
<th>Subject</th>
<th>Feedback</th>
<th>Sent On</th>
<th>Action</th>
</tr>
</thead>
<tbody>';
foreach ($feedback as $row) {
$output .= '<tr>
<td>'.$row['id'].'</td>
<td>'.$row['uid'].'</td>
<td>'.$row['name'].'</td>
<td>'.$row['email'].'</td>
<td>'.$row['subject'].'</td>
<td>'.$row['feedback'].'</td>
<td>'.$row['created_at'].'</td>
<td>
<a href="#" id="'.$row['uid'].'" title="Reply" class="text-primary feedbackReplyIcon" data-toggle="modal" data-target="#showReplyModal"><i class="fas fa-reply fa-lg"></i></a>
</td>
</tr>';
}
$output .= '</tbody>
</table>';
echo $output;
}
else{
echo '<h3 class="text-center text-secondary">:( No any feedback written yet!</h3>';
}
}
这是feedback.php ajax 代码
$("body").on("click", ".feedbackReplyIcon", function(e){
let uid = $(this).attr('id');
$("#feedback-reply-btn").click(function(e){
if($("#feedback-reply-form")[0].checkValidity()){
let message = $("#message").val();
e.preventDefault();
$("#feedback-reply-btn").val('Please Wait...');
$.ajax({
url: 'assets/php/admin-action.php',
method: 'post',
data: {uid:uid,message:message},
success:function(response){
console.log(response);
$("#feedback-reply-btn").val('Send Reply');
$("#showReplyModal").modal('hide');
$("#feedback-reply-form")[0].reset();
}
});
}
});
});
解决方案
这是我认为会发生的情况:由于您在另一个 click 方法中声明了一个 click 方法,因此该uid
变量在第一次使用后变为空。将这样的事件相互堆叠通常是一种不好的做法。尝试将您的 javascript 更改为:
var uid;
$("body").on("click", ".feedbackReplyIcon", function(e){
uid = $(this).attr('id');
});
$("#feedback-reply-btn").click(function(e){
if($("#feedback-reply-form")[0].checkValidity()){
let message = $("#message").val();
e.preventDefault();
$("#feedback-reply-btn").val('Please Wait...');
$.ajax({
url: 'assets/php/admin-action.php',
method: 'post',
data: {uid:uid,message:message},
success:function(response){
console.log(response);
$("#feedback-reply-btn").val('Send Reply');
$("#showReplyModal").modal('hide');
$("#feedback-reply-form")[0].reset();
}
});
}
});
这可能仍然不是最佳的,我有点不清楚你的元素是如何布局的,我无法真正测试它。让我知道会发生什么 - 如果它仍然不起作用,请尝试转储 admin-action.php 中的变量,看看里面有什么。
推荐阅读
- android - 反应本机后退按钮处理程序
- odoo - 我在 Odoo8 中找不到我的模块
- java - 如何为不同于 exe 的文件夹添加上下文菜单项到 Windows 资源管理器
- random - 如何在 Crystal 中生成随机数?
- python - 气流触发 dag 故障
- ruby-on-rails - 您如何使用 ActiveRecord 从“created_at”按年份分组?
- php - “解析错误:无效的数字文字”php chr 039
- visual-studio-code - 如何在 vscode 中配置 Ctrl+PgUp 和 Ctrl+PgDown 键绑定导航到视图的顶部/底部而不是切换选项卡?
- applescript - 使用 Apple 脚本通过包标识符的给定包名称删除应用程序
- android - 数据重新绑定问题