Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Getting selected checkboxes from a list into an array

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Getting selected checkboxes from a list into an array

Postby nevodj » Tue Apr 09, 2013 9:20 pm

Hello,

This is probably a dumb question from a php newbie

I am using this code to generate a list of files in a folder, with a checkbox next to each name.

Code: Select all
<?php
//path to directory to scan
$directory = "../";
 
//get all image files with a .jpg extension.
$images = glob($directory . "*.jpg");
 
//print each file name
foreach($images as $image)
{
   $image = substr($image, 3);
echo "<input name='Select' type='checkbox' value='' />" . $image . "<br />";
}
php?>


I want the users to be able to select the checkbox next to the name

Then my php will get the selected images into an array or a string separated by commas, so I can zip up the selected files for them to download.

Is there a better way of creating the list?
nevodj
New php-forum User
New php-forum User
 
Posts: 10
Joined: Thu Jun 21, 2012 3:43 pm

Re: Getting selected checkboxes from a list into an array

Postby swarups » Tue Apr 09, 2013 9:41 pm

Hi nevodj,

To get all selected(checked) items in a array, you need to set the name in the form to Select[] and you will be able to access all the checkboxes as an array($_POST['Select']). And one more thing you have to defined the value for each checkbox, because the value is going to be posted by the form. So define the value so that you can able to know which image has been checked by user.

So your check box code should look like:
Code: Select all
echo "<input name='Select[]' type='checkbox' value='$image' />" . $image . "<br />";
Last edited by swarups on Wed Apr 10, 2013 1:32 am, edited 1 time in total.
swarups
New php-forum User
New php-forum User
 
Posts: 20
Joined: Tue Mar 06, 2012 11:05 pm

Re: Getting selected checkboxes from a list into an array

Postby johnj » Tue Apr 09, 2013 9:56 pm

Not good to use "select" as it is a keyword/reserved word. Good to use name="files_to_download" or something meaningful.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Getting selected checkboxes from a list into an array

Postby nevodj » Tue Apr 09, 2013 10:05 pm

Hello,

Thanks for your help.

So this is my code now in index.php
Code: Select all
<?php
//path to directory to scan
$directory = "../";
 
//get all image files with a .jpg extension.
$images = glob($directory . "*.jpg");
 
echo "<form action='post.php' method='post' name='images'>";
 
//print each file name
foreach($images as $image)
{
   $image = substr($image, 3);
echo "<input name='Select[]' type='checkbox' value='$image' />" . $image . "<br />";
}
echo "<input name='Submit' type='submit' />";
echo "</form>";
?>


And this is my code in post.php
Code: Select all
<body>
Image list <?php echo array($_POST['Select'][]);?>
</body>


When I hit submit I get this error

Fatal error: Cannot use [] for reading in /home/content/31/9302031/html/sheffieldblades/content/webimages/download/post.php on line 9
nevodj
New php-forum User
New php-forum User
 
Posts: 10
Joined: Thu Jun 21, 2012 3:43 pm

Re: Getting selected checkboxes from a list into an array

Postby seandisanti » Tue Apr 09, 2013 10:22 pm

There are a few problems here; my favorite is an error line 9 of your supposedly 3 line file. That one is referring to the extra [] after the $_POST['select'] in the line you posted. When you remove that though, best case scenario you're going to get another error, or your output is just going to read 'array'. you can't echo an array as an string unless you implode or join the elements. you have to print() or print_r() it, most prefer to do it like this:
Code: Select all

echo 
'<pre>Your array:<br />';//the pre tag makes it more readable, but try it with and without so you see
print_r($_POST['Select']);//play with this one also to see how associative and indexed arrays appear.

Also, just name your checkboxes with the filenames instead of trying to make an array etc. You're doing way more than you need to. the $_POST array will only contain keys (with the name of each control being the key) for the checkboxes that are checked. That means you can just do:
Code: Select all

if 
($_POST){ //some older versions want you to use isset() but if you're up to date this will work
foreach ($_POST as $fname){
//your code here for each file
}
just make sure you don't name your submit input or you'll get more errors. Also, make sure you're cleaning those filenames in your code before doing anything with them, and using is_readable() and/or file_exists() to save yourself some headaches.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Getting selected checkboxes from a list into an array

Postby nevodj » Tue Apr 09, 2013 10:32 pm

Thanks

I left of the html head of the post.php to save space on the post thats why it was only 3 lines

I dont understand the 2nd piece of code you have there... is that to go in index.php or post.php?
nevodj
New php-forum User
New php-forum User
 
Posts: 10
Joined: Thu Jun 21, 2012 3:43 pm

Re: Getting selected checkboxes from a list into an array

Postby seandisanti » Tue Apr 09, 2013 10:35 pm

in post, but realistically you could have the same page display and process the form. Like this:
Code: Select all

<?php
if ($_POST){ //we are responding to a form
//response code here
} else { //not responding to form, should display it instead:
//code to display form
}
 
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Getting selected checkboxes from a list into an array

Postby nevodj » Tue Apr 09, 2013 10:43 pm

Thanks for your help... think this is a bit over my head :(
nevodj
New php-forum User
New php-forum User
 
Posts: 10
Joined: Thu Jun 21, 2012 3:43 pm

Re: Getting selected checkboxes from a list into an array

Postby seandisanti » Tue Apr 09, 2013 10:51 pm

Don't get discouraged, learning php or anything else is a process, and once you start getting it, everything will start falling into place. Here are some great, free, video primers to help you get started. http://jream.com/learning/videos/php-basics He usually does a pretty good job about explaining complex concepts in plain English. He sometimes flies through examples kind of quickly, but the beauty of a video is you can rewind it as many times as it takes to get it, or pause it to copy and play with his code.

Every person who's ever learned php was right where you are at one point or another, and there are an abundance of resources online and people who want to help.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Getting selected checkboxes from a list into an array

Postby nevodj » Tue Apr 09, 2013 10:54 pm

Great, I got it to work using your example. Small error in my code.

This is the output from post.php

Your array:
Array
(
[0] => img1.jpg
[1] => img2.jpg
[2] => img3.jpg
[3] => img4.jpg
[4] => img5.jpg
)

Just wondering - to improve this further, is there a way in php that rather than having checkboxes I could have two list boxes... one on the left with available, and when you double click it jumps to the right hand box called selected. Then you can double click to make them jump back etc
nevodj
New php-forum User
New php-forum User
 
Posts: 10
Joined: Thu Jun 21, 2012 3:43 pm

Re: Getting selected checkboxes from a list into an array

Postby seandisanti » Tue Apr 09, 2013 11:04 pm

It's possible, but is actually more of a pain in the butt, requiring javascript or jquery to update the lists and provide real time responsiveness. I've posted a script sometime within the last month or so of code to do exactly that, but if you want to go that route, i'll leave it to you to find it. It was on this forum, probably early march.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.