dporu02280 2016-11-26 13:58
浏览 46
已采纳

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

I have a table that displays tuples of books and data about them. There is also a radio button for each row. The idea is that the user selects the button to indicate that they want to order that book.

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>';

I've been trying something like onselect="load the button name into a session variable" but I have been unable to implement it.

Each radio button has a name value that is the ISBN (primary key) for the current book thats being put into the table. I want to be able to select a radio button, have that ISBN be stored in a session or global variable, and use that specific ISBN for another method, placeOrder(). After a radio button is checked, the user inputs their first and last name and presses "order selected book", which reloads the page and triggers the placeOrder() function via:

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

which is present at the beginning of the PHP portion, alongside other function calls.

I'm pretty new to PHP and HTML, so forgive me if the answer is obvious. I could do this if the radio button name was explicit, but since it is changing with each row, I cannot figure it out.

Main idea: How can i capture info that a selected radio button corresponds with so I can use said info in another function?

The answer doesn't have to involve session or global variables, any help is appreciated.

  • 写回答

1条回答 默认 最新

  • douqudi5282 2016-11-26 14:15
    关注

    You could an array to represent the radio buttons with the isbn as the index

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

    and then loop through it on the server side

    foreach ($_POST['books'] as $isbn => $on){
    // do something with $isbn
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退