当名称是变量时,如何从单选按钮获取数据?

时间:2016-11-26 13:58:10

标签: php html mysql

我有一张表格,显示有关它们的书籍和数据。每行还有一个单选按钮。这个想法是用户选择按钮来表明他们想订购那本书。

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 =&#34;将按钮名称加载到会话变量&#34;但我一直无法实现它。

每个单选按钮的名称值都是放入表中的当前书籍的ISBN(主键)。我希望能够选择单选按钮,将ISBN存储在会话或全局变量中,并将该特定ISBN用于另一种方法placeOrder()。选中一个单选按钮后,用户输入他们的名字和姓氏,然后按下#34;订购选定的书&#34;,重新加载页面并通过以下方式触发placeOrder()函数:

else if(isset($_POST["placeOrder"])){
    //for placing orders on a single book
placeOrder();
}

出现在PHP部分的开头,以及其他函数调用。

我是PHP和HTML的新手,所以如果答案很明显,请原谅我。如果单选按钮名称是显式的,我可以这样做,但由于它每行都在变化,我无法理解。

主要思想:我如何捕获所选单选按钮对应的信息,以便我可以在另一个功能中使用所述信息?

答案不必涉及会话或全局变量,任何帮助都表示赞赏。

1 个答案:

答案 0 :(得分:1)

您可以使用数组来表示以isbn作为索引的单选按钮

trade ['MFE_pr'] = np.nan
trade ['MFE_pr'] = trade ['MFE_pr'].where(trade ['trend']<0, trade.high.cummax())
trade ['MFE_pr'] = trade ['MFE_pr'].where(trade ['trend']>0, trade.low.cummin())

然后在服务器端循环它

echo '<td><input type="radio" name="books['.$currISBN.']"></td>';