checkboxes...

Post all your tuts or request for tuts here.
Post Reply
alexander19
Posts: 180
Joined: Fri Apr 02, 2010 1:05 pm

checkboxes...

Post by alexander19 »

Hello everyone!
Been working on a private messaging system and I met some problems with the delete section.
I managed to make radioboxes for each message,and if you check the radiobox and press delete it will delete the selected message.
My question is...how do I make another checkbox..like a main one,that when checked it will check all the other radioboxes so if you press delete it will delete all messages.

Any help would be greatly appreciated.

Alexander,
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: checkboxes...

Post by Chris »

It's actually really simple. And shouldn't this be in the help and support section?

First you need to make each checkbox that gets listed have it's own key. This is done by placing '[]' after name, and will create an array. Then you need to give the checkbox a value. This will be the primary key in the database.

Code: Select all


$messages = array(  array( 'id' => 6, 'message' => "LOL!" ),
                    array( 'id' => 2, 'message' => "haha!" ),
                    array( 'id' => 3, 'message' => "lol!" ),
                    array( 'id' => 4, 'message' => "AHAHAHA!" ),
                    array( 'id' => 5, 'message' => "nice!" ),
                    array( 'id' => 26, 'message' => "okay you get the point" ) );

foreach( $messages as $message )
{
    echo "<input type=\"checkbox\" name=\"box[]\" value=\"{$message['id']}\" />". $message['message'] ."<br />\n";
}
 
This will be the output

Code: Select all

<input type="checkbox" name="box[]" value="6" />LOL!<br />
<input type="checkbox" name="box[]" value="2" />haha!<br />
<input type="checkbox" name="box[]" value="3" />lol!<br />
<input type="checkbox" name="box[]" value="4" />AHAHAHA!<br />
<input type="checkbox" name="box[]" value="5" />nice!<br />
<input type="checkbox" name="box[]" value="26" />okay you get the point<br />

Notice they all have the same name but different values. When these are posted, they are all sent with a unique key.

To get the value of any of the ones sent we can simply echo it by selecting the key:

Code: Select all

//Will only show something if selected
echo ( !empty($_POST['box'][3]) ? $_POST['box'][3]  : '' ); // 4
 
To delete them all we need to do is loop through them and delete them:

Code: Select all


foreach( $_POST['box'] as $box )
{
    mysql_query("DELETE * FROM `table` WHERE `id` = '$box'");
}

 
Hope this helps.
Last edited by Chris on Fri Jun 18, 2010 4:32 pm, edited 1 time in total.
Fighting for peace is declaring war on war. If you want peace be peaceful.
alexander19
Posts: 180
Joined: Fri Apr 02, 2010 1:05 pm

Re: checkboxes...

Post by alexander19 »

Yes I think it shoulda be in help and support section...my bad :(
Thx for the fast answer man,gonna try it now and let you know you know how it worked.

Edit:Thx man it helped me out.
Post Reply

Return to “Tutorials”