0

表单由打印出所有等待批准的行的函数组成;

function all_reviews(){
$query1= mysql_query("SELECT * FROM {$table} WHERE first != '' && disp_state= '0' && rejected != '1'");
if (!$query1) {
    die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($query1);

// printing table rows
while($row = mysql_fetch_array($query1))
{
//review action buttons
        echo "<div class=\"rev_action\">";
        echo "<input type=\"hidden\" name=\"".$row['id']."\">";
        echo "<input type=\"submit\" name=\"accept\" value=\"Approve\" class=\"success rev_button\" onclick=\"document.getElementById('review_action').submit()\">";
        echo "<input type=\"submit\" name=\"reject\" value=\"Reject\" class=\"error rev_button\" onclick=\"document.getElementById('review_action').submit()\">";
        echo "</div>";
}

我猜正确的方法是在隐藏的输入中打印行 ID。问题是,一次可能有超过 1 行打印出来。

如何选择与批准或拒绝按钮相关的确切行?

html:

<form name="form" action="include/review_submit.php" id="review_action" method="post">

    <?php all_reviews(); ?>

</form>

php (review_submit.php):

//database server connection 
mysql_connect ("$servername","$dbusername","$dbpassword") or die(mysql_error());
//database connection 
mysql_select_db("esd_db") or die(mysql_error()); 

//data feilds from review form
if($_POST['button'] == "Approve") {
   //code to update given row with an approval status
}

//data feilds from review form
if($_POST['button'] == "Reject") {
   //code to update given row with an rejection status
}
//insert values into table
 mysql_query("UPDATE `reviews` ") or die(mysql_error());

 Print "Success"; 

?>
4

2 回答 2

1

您总是可以使每一行成为不同的形式,并将其传递给动态 URL,GetDATA 将告诉您选择了哪一个。

或者,您不能使用表单,而是让每个表单都链接到一个页面。(假设操作页面是 action.php,你有 100 行。你可以有 action.php?row=1&ac=y、action.php?row=1&ac=n 等)

于 2012-06-15T01:19:36.577 回答
0

如果您想使用基本的非 AJAX 表单,您应该为每一行添加一个复选框并将按钮从每一行移动到表单的底部:

<div class="rev_action">
  <input type="checkbox" name="select_row[]" value="1">
</div>
<div class="rev_action">
  <input type="checkbox" name="select_row[]" value="2">
</div>
<!-- ... -->
<input type="submit" name="accept" value="Approve" class="success rev_button">
<input type="submit" name="reject" value="Reject" class="error rev_button">

如果你想使用 ajax,你不需要在上面做任何改变,并且需要为每个按钮添加一个点击事件:

echo "<div class=\"rev_action\">";
echo "<input class="id_input" type=\"hidden\" name=\"".$row['id']."\">";
echo "<input type=\"submit\" name=\"accept\" value=\"Approve\" class=\"success rev_button\" onclick=\"document.getElementById('review_action').submit()\">";
echo "<input type=\"submit\" name=\"reject\" value=\"Reject\" class=\"error rev_button\" onclick=\"document.getElementById('review_action').submit()\">";
echo "</div>";
...

</form>
<script>
$('input.success.rev_button').click(function(){
  var review_id = $(this).siblings('input.id_input').attr('name')
  $.ajax({
    url: '/reviews/success',
    type:'POST',
    data: {id: review_id}
  })
})
</script>
于 2012-06-15T01:27:31.273 回答