【问题标题】:How can i take data from a radio button when it's name is a variable?当单选按钮的名称是变量时,如何从单选按钮获取数据?
【发布时间】: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 '&lt;td&gt;&lt;input type="radio" name="orderISBN" value="'.$currISBN.'"&gt;&lt;/td&gt;'; 并让订单功能存储所选的 isbn 就像 $specISBN = $_POST['orderISBN']; 一样比我正在做的废话更容易和更清晰.

标签: php html mysql


【解决方案1】:

你可以用一个数组来表示以 isbn 作为索引的单选按钮

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

然后在服务器端循环遍历它

foreach ($_POST['books'] as $isbn => $on){
// do something with $isbn
}

【讨论】:

  • 我在新功能中回显 ISBN,它似乎正在工作!这正是我想要的,现在我可以使用该信息下订单了。非常感谢您的快速回复。对 PHP 和 HTML 如此陌生,我没有意识到答案如此简单。出于好奇,for循环语句的“=> $on”部分是什么意思?给了你一个赞成票,但我担心我的代表太低而无法公开。再次感谢您!
  • $_POST['books'][$isbn] === $on$on的实际值并不重要,因为你感兴趣的只是$_POST['books'][$isbn]的存在,但通常是字符串on。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-28
  • 2011-07-28
  • 2013-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多