表单由一个函数构成,该函数打印出所有等待批准的行;
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";
?>
答案 0 :(得分:1)
您总是可以将每一行设为不同的形式,然后将其传递给动态网址,GetDATA会告诉您选择的是哪一种。
或者,您可以不使用表单,并将每个表单链接到页面。 (假设动作页面是action.php,你有100行。你可以有action.php?row = 1&amp; ac = y,action.php?row = 1&amp; ac = n等)
答案 1 :(得分: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>