从复选框列表中选择后,我点击提交,我收到了勾选的复选框,但我也得到了再次打印的复选框的完整列表,这是一个问题,因为我不想要检查我打勾再次显示的方框,但它们会再次显示,但这次是未经检查的方框。
我尝试使用array_diff方法,但是我收到警告标志而不是告诉我我的Argument是一个字符串而不是一个数组...所以我真的很困惑...我已经上传了复选框的截图更好地解释 -
[IMG] http://i61.tinypic.com/eqtmix.png[/IMG] - 我勾选的复选框
[IMG] http://i62.tinypic.com/e5o16p.png[/IMG] - 在勾选复选框后,如您所见,我的选择会再次打印 - “电机绑定和连接”和“修理传感器”,但这次它们没有被检查。
当我使用Array_diff时,我收到以下错误消息:
警告:array_diff():参数#1不是第50行的C:\ xampp1 \ htdocs \ rajib1 \ rajib \ php_checkbox1.php中的数组
这是我没有使用array_diff方法的代码:
<?php
include('dbcategory.php');
error_reporting(0);
error_reporting(E_ERROR | E_PARSE);
// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP: Get Values of Multiple Checked Checkboxes</title>
<link rel="stylesheet" href="css/php_checkbox.css" />
</head>
<body>
<td valign="top">
<?php
$list = "SELECT * FROM skillsset WHERE category='Engineering' ORDER BY skills ASC";
$listAHI = $dbs ->prepare($list);
$listAHI -> execute();
if(!isset($_POST['submit'])){
while($row = $listAHI ->fetch(PDO::FETCH_ASSOC))
{
$skills = $row["skills"];
echo "
<form action='' method='post'>
<input type='checkbox' name='skills[]' value='$skills'> $skills<br>
";
}
echo"<input type='submit' name='submit' Value='Submit'/></form>";
}
else {
if(isset($_POST['submit'])){
if(!empty($_POST['skills'])) {
foreach($_POST['skills'] as $skills1) {
echo "<p><form action='' method='post'><input type='checkbox' name='skills[]' value='$skills1' checked>".$skills1 ."</p>";
}
while($row = $listAHI ->fetch(PDO::FETCH_ASSOC)){
$skills = $row["skills"];
//$result=array_diff($skills,$skills1);
//print_r($result);
echo "
<form action='' method='post'>
<input type='checkbox' name='skills[]' value='$skills'> $skills<br>
";
}
}
}
else{
echo "<b>Please Select At least One Option.</b><input type='submit' name='submit' Value='Submit'/>";
}
}
?>
</td>
</form>
</div>
</div>
</body>
</html>
如果我添加以下代码行 - // $ result = array_diff($ skills,$ skills1); //的print_r($结果); 注释,然后我收到以下错误消息:
警告:array_diff():参数#1不是第50行的C:\ xampp1 \ htdocs \ rajib1 \ rajib \ php_checkbox1.php中的数组
这里是表格的代码,我从中重试数据以打印带有复选框的列表 - 如果您决定复制并粘贴下面的代码并将其注入mysql /,请将文件命名为skillset.sql文件apache(或任何其他)服务器 - thnx
-- phpMyAdmin SQL Dump
-- version 4.1.6
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Feb 04, 2015 at 04:01 AM
-- Server version: 5.6.16
-- PHP Version: 5.5.9
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `quietservices`
--
-- --------------------------------------------------------
--
-- Table structure for table `skillsset`
--
CREATE TABLE IF NOT EXISTS `skillsset` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`category` varchar(250) NOT NULL,
`skills` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=40 ;
--
-- Dumping data for table `skillsset`
--
INSERT INTO `skillsset` (`id`, `category`, `skills`) VALUES
(11, 'Information', 'PHP'),
(13, 'Information', 'C++'),
(14, 'Information', 'C#'),
(15, 'Information', 'Javascript'),
(17, 'Information', 'AJAX'),
(18, 'Information', 'Java'),
(19, 'Information', 'Adobe Photoshop'),
(21, 'Information', 'Adobe Premiere Pro'),
(22, 'Information', 'Final Cut'),
(28, 'Engineering', 'Power Factor Improvement (PFI)'),
(29, 'Engineering', 'Motor Binding & Connection'),
(31, 'Engineering', 'Transformer'),
(34, 'Engineering', 'AutoCAD'),
(35, 'Information', 'AutoCAD'),
(36, 'Engineering', 'Switchgear Panels'),
(37, 'Engineering', 'General Wiring'),
(38, 'Engineering', 'Repair Sensors'),
(39, 'Engineering', 'Install & Maintain Substation');
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
请让我知道如何在检索并打印完整列表时再次打印已勾选/勾选的复选框作为选中/勾选框... thnx a mill!
答案 0 :(得分:0)
根据您的陈述:
我不希望我勾选的复选框再次显示
您有所选技能的数组,您不想再次显示 数据库中包含整个技能列表的另一个数组。 / p>
因此,当您再次打印技能列表时,在显示每个项目之前询问POSTed数组中是否存在该项目。我会用in_array()来做这件事。 首先删除整个部分,因为您强制打印所有已发布的项目,您不希望: 然后用下面的方法立即替换(...): 我还注意到您正在为打印的每个复选框打开和关闭表单,我假设您需要在单个表单上使用它们,因此我修改了您的代码以显示包含所有复选框的单个表单。 我希望这可以解决您的问题。if(isset($_POST['submit'])){
if(!empty($_POST['skills'])) {
foreach($_POST['skills'] as $skills1) {
echo "<p><form action='' method='post'><input type='checkbox' name='skills[]' value='$skills1' checked>".$skills1 ."</p>";
}
echo "<form action='' method='post'>"; // You can use a single <form> item for all checkboxes
while($row = $listAHI ->fetch(PDO::FETCH_ASSOC)){
// Traverse your entire list of skills.
$skill = $row["skills"]; // I renamed your var to singular because this is a single skill item.
if(isset($_POST['skills']) and !in_array($skill, $_POST['skills'])){
// Skill not found on POSTed array; then show it.
echo "<input type='checkbox' name='skills[]' value='$skill'>$skill<br>";
}
}
echo "</form>"; // close your form at end of checkboxes