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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?