我的代码从数据库中提取条目并将它们打印成单独的表单,以便我可以使用不同操作的按钮接受或拒绝它们中的每一个。每当我在其中一个表单上执行操作时,它都不会在同一表单上执行操作,而是从数据库中的第一个条目开始按顺序移动。
在上图中,如果我接受第二张表格,它将在第一张表格上执行该功能。
代码:
<?php
} else if ($usertype == 1) {
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
$server = "";
$user = "";
$pass = "r=~";
$db = "";
$db3 = "aukwizcq_professors";
$user1 = $_SESSION['username'];
$mysqli = new Mysqli($server, $user, $pass, $db) or mysqli_error($mysqli);
$mysqli5 = new Mysqli($server, $user, $pass, $db3) or mysqli_error($mysqli);
$name= $mysqli5->query("SELECT name FROM professor WHERE username= '$user1'")->fetch_object()->name; //taking name from list of professors that are equal to the userid logged in (if hes a professor, it will show, if not 0)
$overrides = $mysqli->query("SELECT * FROM Overrides WHERE professor= '$name' AND status ='1'");
$num_rows = mysqli_num_rows($overrides);
echo " Pending Overrides: " . $num_rows;
?>
<?php
while($row = mysqli_fetch_array($overrides)) { ?>
<fieldset>
<form method="post" action="dbheads.php" name="HF" id="HF" autocomplete="off">
<?php
echo "First Name: " . $row['name'] . "<br />";
echo "<br />Mid. Name: " . $row['mname'] . "<br />";
echo "<br />Fam. Name: " . $row['fname'] . "<br />";
echo "<br />Student ID: " . $row['sid'] . "<br />";
echo "<br />Scolarship: " . $row['sc'] . "<br />";
echo "<br />Phone No: " . $row['phone'] . "<br />";
echo "<br />Email: " . $row['email'] . "<br />";
echo "<br />Subject: " . $row['subject'] . "<br />";
echo "<br />Section: " . $row['section'] . "<br />";
echo "<br />Semester: " . $row['semester'] . "<br />";
echo "<br />Professor: " . $row['professor'] . "<br />";
$id = $row['id'];
echo "<input type='hidden' name='id' value='$id'>";
$name = $row['name'];
echo "<input type='hidden' name='name' value='$name'>";
$mname = $row['mname'];
echo "<input type='hidden' name='mname' value='$mname'>";
$fname = $row['fname'];
echo "<input type='hidden' name='fname' value='$fname'>";
$sid = $row['sid'];
echo "<input type='hidden' name='sid' value='$sid'>";
$sc = $row['sc'];
echo "<input type='hidden' name='sc' value='$sc'>";
$phone = $row['phone'];
echo "<input type='hidden' name='phone' value='$phone'>";
$email = $row['email'];
echo "<input type='hidden' name='email' value='$email'>";
$subject = $row['subject'];
echo "<input type='hidden' name='subject' value='$subject'>";
$section = $row['section'];
echo "<input type='hidden' name='section' value='$section'>";
$semester = $row['semester'];
echo "<input type='hidden' name='semester' value='$semester'>";
//$professor = $row['professor'];
// echo "<input type='hidden' name='professor' value='$professor'>";
?>
<br />
<div>
<label for="comments" accesskey="c">Notes & Comments:</label><br />
<textarea name="comments" id="comments" cols="50" rows="5"></textarea>
<br>
</div>
<br>
<script type="text/javascript">
function submitForm(action)
{
document.getElementById('HF').action = action;
document.getElementById('HF').submit();
}
</script>
<input type="button" onclick="submitForm('dbheads.php')" value="Accept" />
<input type="button" onclick="submitForm('dbheads2.php')" value="Deny" /> </form>
</fieldset>
<br>
<?php
}
?>
dbheads.php
<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';
sec_session_start();
?>
<html>
<?php
$mysql_host = "localhost";
$mysql_username = "";
$mysql_password = "";
$mysql_database = "";
$user = $_SESSION['username'];
if (login_check($mysqli) == true) : ?>
<p>Welcome <?php echo htmlentities($user); ?>!</p>
<?php
$mysqli = new Mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database) or die(mysqli_error());
$status = 2;
$id = $_POST['id'];
$stmt = $mysqli->prepare("UPDATE Overrides SET status=? WHERE id='$id'");
$stmt->bind_param("s", $status);
$stmt->execute();
print 'Error : ('. $mysqli->errno .') '. $mysqli->error;
echo htmlentities(accepted);
?>
<?php else : ?>
<p>
<span class="error">You are not authorized to access this page.</span> Please <a href="index.php">login</a>.
</p>
<?php endif; ?>
</html>
请帮忙修理?
答案 0 :(得分:0)
您的JS将始终只按照您设置的方式提交上一个<form>
中包含的值。
这里最大的问题是您的网页上有多个<form>
元素,但没有任何内容可以将您的按钮与特定表单联系起来。
我建议你使用JQuery为每个接受或拒绝发送一个AJAX请求。
以下文章可能有所帮助。