hella fellas, I got a little task about half done...this is regarding checkbox arrays in php code...
after i make a selection from a list of check boxes and I click submit, I get the check boxes that i ticked but i also get the entire list of check boxes printed again which is a problem because I do not want the check-boxes that I ticked to be displayed again, but they are displayed again but this time as an unchecked box.
I tried to use the array_diff method but I get warning signs instead telling me that my Argument is a string and not an array...so I'm really confused...i've uploaded the screenshots of the checkboxes to explain it better -
[IMG]http://ift.tt/1AqJUUv] - checkbox that i ticked
[IMG]http://ift.tt/1Kq2EDr] - after i ticked the check box, as you can see, my selections are printed again - "Motor Binding And Connection", and "Repair Sensors" but this time they are unchecked.
when I use Array_diff I get the following error message:
Warning: array_diff(): Argument #1 is not an array in C:\xampp1\htdocs\rajib1\rajib\php_checkbox1.php on line 50
here's my code without the array_diff method being used:
<?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>
If I add the following lines of code - //$result=array_diff($skills,$skills1); //print_r($result); which are commented, then I get the following error message:
Warning: array_diff(): Argument #1 is not an array in C:\xampp1\htdocs\rajib1\rajib\php_checkbox1.php on line 50
here's the code for the table as well from which I retried the data to print out the listings with the checkboxes - please name the file skillsset.sql if you decide to copy and paste the code below and inject it into a mysql/apache (or any other) server - thnx
-- phpMyAdmin SQL Dump
-- version 4.1.6
-- http://ift.tt/HXFLWR
--
-- 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 */;
please let me know how i can get the checked/ticked boxes from printing again as checked/ticked boxes when the complete list is retrieved and printed...thnx a mill!
Aucun commentaire:
Enregistrer un commentaire