【发布时间】:2016-11-26 22:15:31
【问题描述】:
我有一个表格,显示书籍的元组和有关它们的数据。每行还有一个单选按钮。这个想法是用户选择按钮来表明他们想要订购那本书。
function displayAllBooks(){
$dbhost = 'localhost:3306';
$dbuser = 'root';
$conn = mysqli_connect($dbhost, $dbuser);
//sql statement to use the database
$sql = 'use BookStore';
mysqli_query($conn, $sql);
$sql = 'SELECT * FROM Author, Books, Written_By, Book_Categories, Assigned_To '
. 'WHERE Author.Author_ID = Written_By.Author_ID'
. ' AND Books.ISBN = Written_By.ISBN'
. ' AND Books.ISBN = Assigned_To.ISBN'
. ' AND Assigned_To.Cat_Code = Book_Categories.Cat_Code'
. ' ORDER BY ALname ASC';
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
echo '<form action = "customerDashboard.php" method = "post">';
echo 'First name:
<input type= "text" name ="CFname"<br>
Last name:
<input type= "text" name ="CLname"<br>';
echo '<p style="text-align:center"><b> All Books </b></p>';
echo '<table class="center">'
. '<tr>'
. '<th>Order</th>'
. '<th>Title</th>'
. '<th>Price</th>'
. '<th>Author</th>'
. '<th>Publication Date</th>'
. '<th>User Review</th>'
. '<th>Category</th>'
. '</tr>';
if (mysqli_num_rows($result) > 0) {
$bookCt = 0;
//mysqli_fetch_assoc associates an array with the results
while ($row) {
$retTitle = $row["Title"];
$retPrice = $row["Price"];
$retALname = $row["ALname"];
$retPubDate = $row["Publication_Date"];
$retReview = $row["User_Reviews"];
$retCat = $row["Cat_Desc"];
//fetch ISBN for each book, for use with the radio buttons to
//place orders
$sql = 'SELECT ISBN from Books WHERE Title="'.$retTitle .'"';
$resultISBN = mysqli_query($conn, $sql);
$rowISBN = mysqli_fetch_assoc($resultISBN);
$currISBN = $rowISBN["ISBN"];
echo"<tr>";
echo '<td><input type="radio" name="'.$currISBN.'"></td>';
echo "<td> $retTitle </td>";
echo "<td> $retPrice </td>";
echo "<td> $retALname </td>";
echo "<td> $retPubDate </td>";
echo "<td> $retReview </td>";
echo "<td> $retCat </td>";
echo "</tr>";
$row = mysqli_fetch_assoc($result);
}
}
else {
echo "0 results";
}
echo "</table>";
echo '<input type="submit" name="placeOrder" value="Order Selected Book">';
echo '</form>';
我一直在尝试类似 onselect="load the button name into a session variable" 但我无法实现它。
每个单选按钮都有一个名称值,即要放入表中的当前图书的 ISBN(主键)。我希望能够选择一个单选按钮,将该 ISBN 存储在会话或全局变量中,并将该特定 ISBN 用于另一种方法 placeOrder()。选中单选按钮后,用户输入他们的名字和姓氏,然后按“order selected book”,这会重新加载页面并通过以下方式触发 placeOrder() 函数:
else if(isset($_POST["placeOrder"])){
//for placing orders on a single book
placeOrder();
}
它出现在 PHP 部分的开头,与其他函数调用一起。
我对 PHP 和 HTML 还很陌生,如果答案很明显,请原谅我。如果单选按钮名称是明确的,我可以这样做,但由于它随每一行而变化,我无法弄清楚。
主要思想:如何捕获选定单选按钮对应的信息,以便我可以在另一个功能中使用所述信息?
答案不必涉及会话或全局变量,感谢任何帮助。
【问题讨论】:
-
感谢 Musa 解决了这个问题,但我真正想做的是
echo '<td><input type="radio" name="orderISBN" value="'.$currISBN.'"></td>';并让订单功能存储所选的 isbn 就像$specISBN = $_POST['orderISBN'];一样比我正在做的废话更容易和更清晰.