[SOLVED] Saving values from checkboxes into MySql

This is a discussion on "[SOLVED] Saving values from checkboxes into MySql" within the PHP Forum section. This forum, and the thread "[SOLVED] Saving values from checkboxes into MySql are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jan 26th, 2008, 15:56
karinne's Avatar
SuperMember

SuperMember
Join Date: Jan 2007
Location: You know where
Age: 31
Posts: 4,617
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Saving values from checkboxes into MySql

I have form where I'm displaying checkboxes with data taken from a MySQL table

PHP: Select all

<?
    $selectint 
mysql_query("select * from interets order by interet asc"); 
    while (
$rowint mysql_fetch_array($selectint)) {
        
$interet stripslashes($rowint['interet']);
        
$iID $rowint['iID'];
    
?>
        <input type="checkbox" name="iID" id="interets" value="<?=$iID?>" /> <?=$interet?><br />
    <?
    
?>
which displays this when you view the source online

HTML: Select all
<input type="checkbox" name="iID" id="interets" value="1" /> Activités de réseautage<br />
<input type="checkbox" name="iID" id="interets" value="3" /> Coaching<br />
<input type="checkbox" name="iID" id="interets" value="6" /> Comité amélioration physique<br />
<input type="checkbox" name="iID" id="interets" value="8" /> Comité développement écon.<br />
<input type="checkbox" name="iID" id="interets" value="7" /> Comité marketing<br />
<input type="checkbox" name="iID" id="interets" value="2" /> Formations et ateliers gratuits divers<br />
<input type="checkbox" name="iID" id="interets" value="5" /> Implication comme bénévole<br />
<input type="checkbox" name="iID" id="interets" value="4" /> Publicité commune<br />
What I wanted to do was to store the results like this

1-3-7

I think this is the easiest way...

What I want to know is, then user clicks submit, how do go about grabbing the ones that were checked and putting them in the format above?
Reply With Quote

  #2 (permalink)  
Old Jan 26th, 2008, 16:09
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Saving values from checkboxes into MySql

When a user clicks a form that has checkboxes, only the checked boxes are sent. Any that were left unchecked don't even arrive at the next page.

Then with php, you can just check if the name of the checkbox exists

PHP: Select all



if (isset($_POST['iID'])
//etc 
I am a bit worried about your form though as checkboxes should all have different names because each is treated as an individual form field as opposed to radio buttons where a group of them can all have the same name with differing values.

In its current form, PHP will only receive the last checkbox ticked because all previous one's will be overwritten by the last.


Alternatively, you can name your checkboxes with

HTML: Select all
name="iID[]"
When PHP receives the form request, it will build all of the checked elements into an array so if the name of the element isn't an issue, this would be the best way to proceed.

Hope that helps.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #3 (permalink)  
Old Jan 26th, 2008, 16:16
JustinStudios's Avatar
SuperMember

SuperMember
Join Date: Mar 2007
Location: USA
Posts: 406
Blog Entries: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to JustinStudios
Re: Saving values from checkboxes into MySql

Drats, the one time karinne needs help I get beaten by Rakuli to the punch! Man, I wanted to help! haha
Reply With Quote
  #4 (permalink)  
Old Jan 26th, 2008, 16:29
karinne's Avatar
SuperMember

SuperMember
Join Date: Jan 2007
Location: You know where
Age: 31
Posts: 4,617
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Saving values from checkboxes into MySql

Thanks Rakuli ...

OK ... so if there were checkboxes checked, how do I get it to save it in one variable like 3-6-8 ?

I was trying to check the php.net site but I can't seem to access it this morning.

I know how to split a variable like that using the explode() so ... is there an implode() function that creates it
Reply With Quote
  #5 (permalink)  
Old Jan 26th, 2008, 16:38
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Saving values from checkboxes into MySql

Yes.. okay so say you go for the multi-form array option and your form turns out like this

HTML: Select all
<input type="checkbox" name="iID[]" id="interets" value="1" /> Activités de réseautage<br />
<input type="checkbox" name="iID[]" id="interets" value="2" /> Activités de réseautage<br />

<input type="checkbox" name="iID[]" id="interets" value="3" /> Activités de réseautage<br />

<input type="checkbox" name="iID[]" id="interets" value="4" /> Activités de réseautage<br />

<input type="checkbox" name="iID[]" id="interets" value="5" /> Activités de réseautage<br />

<!-- etc //-->
On the page the the form is sent to you could use some PHP similar to: (assuming you use $_POST, if not ust replace with $_GET

PHP: Select all



// First check if ANY of them were checked.. PHP will place all the the checked box values into an array found in $_POST['iID']

if (isset($_POST['iID']))
{
   
// Now use the PHP implode function which is exatly like explode except you use the second argument as the glue for the string and it joins the array together

  
$variableString implode($_POST['iID'], '-'); // Join on the hyphen

echo $variableString


PHP normally has an opposite to everything... This function is quite handy

Cheers
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #6 (permalink)  
Old Jan 26th, 2008, 16:43
karinne's Avatar
SuperMember

SuperMember
Join Date: Jan 2007
Location: You know where
Age: 31
Posts: 4,617
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Saving values from checkboxes into MySql

Ah ... poor Justin ... got beaten by Rakuli again

Peeeerfect! Thanks Rakuli!
Reply With Quote
  #7 (permalink)  
Old Jan 26th, 2008, 16:48
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Saving values from checkboxes into MySql

Quote:
Ah ... poor Justin ... got beaten by Rakuli again
** Rakuli blows smoke from quick fired gun **

You're welcome.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #8 (permalink)  
Old Jan 26th, 2008, 16:58
JustinStudios's Avatar
SuperMember

SuperMember
Join Date: Mar 2007
Location: USA
Posts: 406
Blog Entries: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to JustinStudios
Re: Saving values from checkboxes into MySql

NOOESS!!! lol I think Rakuli is a hogging the forumz :P. Well good job Rakuli.
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Form to email script with checkboxes Posie PHP Forum 4 Dec 10th, 2007 03:54
[SOLVED] preserve the state of checkboxes after an AJAX call pesho318i JavaScript Forum 4 Nov 7th, 2007 16:35
[SOLVED] saving data from one array into another eon201 PHP Forum 2 Nov 7th, 2007 09:34
Assign string values to integer values of a session variable Andy K Classic ASP 1 Jul 13th, 2005 08:29
[SOLVED] Presenting binary values Anonymous User Classic ASP 3 Dec 7th, 2004 17:50


All times are GMT. The time now is 05:48.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43