我想从foreach循环php中使用ajax获取单击行的UID,但获取所有UID
我试图从锚点标记中获取UID,这是ajax中foreach循环PHP中锚点标记的ID属性值。但它一次只能工作一次,当我再次单击锚定标记时,所有UID都会被ajax抓取 当我点击anchor标记时,我首先显示一个bootstrap 4模式,在模式中,一个textarea标记带有一个按钮,当我点击按钮时,我必须再次向action.php页面发送uid和消息 这是我的action.php代码我想从foreach循环php中使用ajax获取单击行的UID,但获取所有UID,php,jquery,ajax,Php,Jquery,Ajax,我试图从锚点标记中获取UID,这是ajax中foreach循环PHP中锚点标记的ID属性值。但它一次只能工作一次,当我再次单击锚定标记时,所有UID都会被ajax抓取 当我点击anchor标记时,我首先显示一个bootstrap 4模式,在模式中,一个textarea标记带有一个按钮,当我点击按钮时,我必须再次向action.php页面发送uid和消息 这是我的action.php代码 if(isset($_POST['action']) && $_POST['action']
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>';
}
}
我相信会发生这样的情况:因为您在另一个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中转储变量,然后查看其中的内容
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();
}
});
}
});