这是我第一次在stackoverflow上问一个问题,这个问题让我难过。我也是PHP和HTML的初学者。
<body>
<div class="topnav" id="navbar">
<a href ="user_main_page.php"><img style="max-width:32px; margin-top:-3px;"
src="/image/logo.jpg"></a1>
<a href="user_main_page.php">Home</a>
<a class="active" href="user_ordernow.php">Order Now</a>
<a href='user_menu.php'>Our Menu</a>
<a href="user_about.php">Who are we?</a>
<a href="user_contact.php">Contact Us</a>
<a style = "float:right" href="../login_page.php">Log Out</a>
</div>
<div class="tab">
<button class="tablinks" onclick="openTab(event, 'Breakfast')"
id="defaultOpen">Breakfast Menu</button>
<button class="tablinks" onclick="openTab(event, 'Snacks')">Snacks</button>
<button class="tablinks" onclick="openTab(event, 'Drinks')">Drinks</button>
<button class="tablinks" onclick="openTab(event, 'Main')">Main
Button</button>
<button class="tablinks" onclick="openTab(event,
'Checkout')">Checkout</button>
</div>
<div id="Breakfast" class="tabcontent">
<?php
/*declare parameters for $conn*/
$username="root";
$password="";
$database="login";
$con = mysql_connect("localhost", $username, $password);
$selectdb = mysql_select_db("login",$con);
$result = mysql_query("SELECT * FROM menu where category='1'")
or die(mysql_error());
echo "<table border='0' cellpadding='10'>";
echo "<tr> <th>Order</th> <th>ID</th> <th>Product</th> <th>Price ($)</th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
echo "<tr>"; // echo out the contents of each row into a table
echo '<td><input type="checkbox" value="" name="id[]" />' . '</td>'; />' . '</td>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo "</tr>";
}
echo "</table>";
?>
</div>
<div id="Snacks" class="tabcontent">
<h3>snacks</h3>
<p></p>
</div>
<div id="Drinks" class="tabcontent">
<h3>Drinks</h3>
<p></p>
</div>
<div id="Main" class="tabcontent">
<h3>Main</h3>
<p></p>
</div>
<div id="Checkout" class="tabcontent">
<h3>Checkout</h3>
<p></p>
</div>
它连接到MySQL数据库登录和表菜单。在代码的PHP部分中,我访问数据库并在表中显示数据库。我遇到的问题是在末尾按提交按钮后检索滴答的复选框。在按下提交按钮检索哪些复选框并将该信息存储到另一个MySQL数据库之后,我该如何?当前的布局是这样的。
网页设计 - 图片
谢谢!
假设您的所有字段都命名为html,html all all 文本框向服务器,但只有 selected 按钮。对于普通的按钮和广播按钮,这是直观的,但是对于复选框,需要额外的工作。
在您的情况下,复选框被命名为 id[]
。php将把数据视为数据的数组:
$_POST['id'][]
(或者是$_GET
?我看不到表单方法。(
问题是您将不知道检查哪一个,因为数组键只反映了选定的框。
如果您使用以下内容,那就更好了:
'<td><input type="checkbox" value="" name="id[' . $row['id'] . ']" …
也就是说,将id
放在名称的数组键中。您将有一系列名称:
name=id[3]
name=id[4]
name=id[7]
或任何您的ID。
然后,在PHP中,您可以运行以下内容:
if(isset($_POST['id'])) { // any checked?
$ids=array_keys($_POST['id']); // get selected keys
foreach($ids as $id) {
// process each selected id
}
}
诀窍是:
- 将put ID作为每个复选框中的密钥:
name="id[…]"
- 在PHP提取中这些键
您需要通过JS更新页面 - 实际上,您需要将检索和存储(crud(PHP代码从您的HTML(view(分解。JS应该对与数据库交谈的脚本进行异步调用,并使用这些结果更新字段。
但是,那可能超出了您现在能力的范围 - 因此,只需在您的时循环中添加评估,然后有人"提交"表单,重新绘制页面,想法是您的更新数据,应该存在的想法,当评估发生在WARE循环中时,您的HTML反映了数据的当前状态 - 类似(我完全忽略了JS(:
<body>
<div class="topnav" id="navbar">
<a href ="user_main_page.php"><img style="max-width:32px; margin-top:-3px;"
src="/image/logo.jpg"></a1>
<a href="user_main_page.php">Home</a>
<a class="active" href="user_ordernow.php">Order Now</a>
<a href='user_menu.php'>Our Menu</a>
<a href="user_about.php">Who are we?</a>
<a href="user_contact.php">Contact Us</a>
<a style = "float:right" href="../login_page.php">Log Out</a>
</div>
<div class="tab">
<button class="tablinks" onclick="openTab(event, 'Breakfast')"
id="defaultOpen">Breakfast Menu</button>
<button class="tablinks" onclick="openTab(event, 'Snacks')">Snacks</button>
<button class="tablinks" onclick="openTab(event, 'Drinks')">Drinks</button>
<button class="tablinks" onclick="openTab(event, 'Main')">Main
Button</button>
<button class="tablinks" onclick="openTab(event,
'Checkout')">Checkout</button>
</div>
<div id="Breakfast" class="tabcontent">
<?php
/*declare parameters for $conn*/
$username="root";
$password="";
$database="login";
$con = mysql_connect("localhost", $username, $password);
$selectdb = mysql_select_db("login",$con);
$result = mysql_query("SELECT * FROM menu where category='1'")
or die(mysql_error());
echo "<table border='0' cellpadding='10'>";
echo "<tr> <th>Order</th> <th>ID</th> <th>Product</th> <th>Price ($)</th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
echo "<tr>"; // echo out the contents of each row into a table
echo '<td><input type="checkbox" value="" name="id[]" '. ($row['selected'] ? .' checked=true '. : .''. ).' />' . '</td>'; />' . '</td>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo "</tr>";
}
echo "</table>";
?>
</div>
<div id="Snacks" class="tabcontent">
<h3>snacks</h3>
<p></p>
</div>
<div id="Drinks" class="tabcontent">
<h3>Drinks</h3>
<p></p>
</div>
<div id="Main" class="tabcontent">
<h3>Main</h3>
<p></p>
</div>
<div id="Checkout" class="tabcontent">
<h3>Checkout</h3>
<p></p>
</div>