问题描述
我目前正在开发一个 php 和 MySQL 应用程序.在我的应用程序中,我从 MySQL 加载一个表并将其显示在我的网站中.该表由列(ID、Name、SurName)组成.当我加载表格时,我还创建了另一列,其中包含不同的复选框.我创建表的源代码如下:
I am currently working on a php and MySQL application. In my application I load a table from MySQL and I display it in my Website. The table consists of the columns (ID,Name,SurName). When I load the table I also create another column which consists of different checkboxes. My source code for the creation of the table is the following:
function user_clients_table() {
$con = mysql_connect("localhost","root",'');
if(!$con){
die("Cannot Connect" . mysql_error());
}
mysql_select_db("client_app",$con);
$get_user_clients = "SELECT `ID`,`Name`,`SurName` FROM `clients` ";
$clients = mysql_query($get_user_clients,$con);
echo "<table border=2>
<tr>
<th>Client</th>
<th>Name</th>
<th>SurName</th>
<th>Receive Message</th>
</tr>";
while($record = mysql_fetch_array($clients)){
echo "<form action=pushnotification.php method=post>";
echo "<tr>";
echo "<td>".$record['ID']." </td>";
echo "<td>".$record['Name']." </td>";
echo "<td>".$record['SurName']." </td>";
echo "<td>"."<input type=checkbox name= checkbox[".$record['ID']."] "."</td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
mysql_close();
}
表格看起来像网站中的那样:
The table looks Lke that in the Website:
如果在我单击网站上的发送按钮后设置了客户端的复选框,我想要做的是从表的第一列中回显客户端 ID.例如,如果顶部复选框 isset 我想回显 "1" ,如果第三行中的复选框被选中,我想回显 "3".
What i want to do is to echo the client id from the first column of the table if the checkbox of the client isset after i click the send button on the website. For example if the top checkbox isset i want to echo "1" , if the checkbox in the third row is checked i want to echo "3".
到目前为止我已经这样做了:
I ve done this so far:
if (isset($_POST['checkbox']))
{
foreach ($_POST['checkbox'] as $key => $value)
{
$receivemsg = $key;
}
echo '<span style="color:#AFA;text-align:center;">'.$receivemsg.'</span>';
}
但它仅适用于第一个复选框,其他复选框不起作用.有人可以帮我做到这一点吗?致谢
But it works only for the first checkbox the other ones are not working.Can someone please help me to do this?Thanks in Regards
推荐答案
(在 HTML 中,您将属性放在 "" 中,例如 type="checkbox".使用 '' 作为您的标签,因此您可以使用 "" 作为属性.您还缺少输入标签末尾的"/>".)
(In HTML, you put the attributes in "", like type="checkbox". Use '' for your tags, so you can use "" for the attributes. You are also missing a " />" at the end of your input tag.)
使用isset,您实际上只检查第一个.使用 foreach 删除它,无论如何您都不需要它,因为它会遍历选中的复选框(如果您从 HTML 将它们作为数组发送).如果没有选中任何复选框,循环将运行 0 次.
With the isset you actually check only the first one. Remove it, with foreach, you don't need it anyway, as it loops through the checked checkboxes (if you send them from HTML as an array). If none of the checkboxes are selected, the loop will run 0 times anyway.
foreach ($_POST['checkbox'] as $key => $value)
{
$receivemsg = $key;
}
如果你这样写,它只会保存最后一个选中的复选框.也许你想要
If you write it this way, it will only save the very last checked checkbox. Maybe you want
foreach ($_POST['checkbox'] as $key => $value)
{
$receivemsg[] = $key;
}
当然还有注入、mysqli 和评论中提到的其他内容.
And of course, injection, mysqli, and others what has been mentioned in the comments.
(我个人觉得有点奇怪,如果复选框作为数组发送,isset 不再对它们起作用.或者更准确地说,它作为数组的元素对它们起作用.)
(Personally I find it kind of strange that if checkboxes are sent as an array, isset doesn't work on them anymore. Or to be more precise, it works on them as elements of the array.)
正如@Johannes 所说,您还为每个复选框声明了一个表单.
As @Johannes said, you also declare a form for each checkbox.
这篇关于获取选中复选框的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!