Current location: Hot Scripts Forums » Programming Languages » PHP » How to get values from checked checkboxes?


How to get values from checked checkboxes?

Reply
  #1 (permalink)  
Old 09-18-06, 03:59 PM
Godwin Godwin is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Question How to get values from checked checkboxes?

I have a form with 4 checkboxes and I want to get the checkbox NAMES which are checked and assign in it's own variable.

Ex:

apple
mango
banana
grape

apple - grape are checkbox names not values. each checkbox will have either 'Y' or 'N' values.
If the user checks both apple and mango I want to store apple in variable 1 and mango in variable 2.
If banana and grape then banana in variable 3 and grape in variable 4 ....


I need to then store these variables in a session and make it available in other pages(to query the database)

How do I do that?

Last edited by Godwin; 09-18-06 at 04:03 PM.
Reply With Quote
  #2 (permalink)  
Old 09-18-06, 06:17 PM
jfulton's Avatar
jfulton jfulton is offline
Community VIP
 
Join Date: Apr 2006
Location: Los Angeles, CA
Posts: 660
Thanks: 0
Thanked 0 Times in 0 Posts
What exactly are you trying to do? I don't understand the problem, so your solution doesn't make much sense to me. It seems like you would be better off using arrays rather than individual variables... Could you post some of your code?

Why are you storing the name of the checkbox? I think that's where I'm confused - it doesn't really make much sense...Why don't you store the "fruit type" in the value of the check box? Then in your form processing page you could just plug in the array from the form into a session variable rather than dealing with x variables...
Reply With Quote
  #3 (permalink)  
Old 09-18-06, 06:17 PM
nova912's Avatar
nova912 nova912 is offline
Code Guru
 
Join Date: Sep 2004
Location: Traverse City, MI, USA
Posts: 821
Thanks: 0
Thanked 0 Times in 0 Posts
How about this then, you have 4 items that are true or false right? So try something like this.

Make the value of checkbox the name of the item, that way you can make a simple script to check if the value was check.

If a checkbox is not checked it will not be passed with the rest of the form vars. so make 4 check boxes.
Code:
<input type="checkbox" name="checkboxes[]" value="apple" />
		<input type="checkbox" name="checkboxes[]" value="mango" />
		<input type="checkbox" name="checkboxes[]" value="banana" />
		<input type="checkbox" name="checkboxes[]" value="grape" />
Then check in php if the vars exist in the array sent.
PHP Code:

if(in_array('apple',$_POST['checkboxes']))

{
$_SESSION['apple'] = true;
}
else
{
$_SESSION['apple'] = false;
}
// do the above for each var // 
Does that help?
Reply With Quote
  #4 (permalink)  
Old 09-19-06, 12:17 AM
Godwin Godwin is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by jfulton
What exactly are you trying to do? I don't understand the problem, so your solution doesn't make much sense to me. It seems like you would be better off using arrays rather than individual variables... Could you post some of your code?

Why are you storing the name of the checkbox? I think that's where I'm confused - it doesn't really make much sense...Why don't you store the "fruit type" in the value of the check box? Then in your form processing page you could just plug in the array from the form into a session variable rather than dealing with x variables...
I have a form with 4 courses. When registering a student I have to select courses. So I created 4 checkboxes and a table with student name and 4 columns representing the courses. I have assigned the "vaules" of the checkboxes to 'Y' if checked and 'N' if not. So lets say I register a student for course 1 and 2, then after I submit the form, the data will be inserted in the table in this format:

student_name course1 course2 course3 course4
godwin................Y............ Y...........N...........N

It means the student has been registered for course 1 and 2 only.
Then I wanted to get the name of the course of 'Y' (checked) to be available in other pages using sessions. The problem is that I don't want to use if else staments after inserting the data but use it on the form based on values checked. And the solution nova912 gave is what exactly I needed!
Thanks nova912 that worked!.

Last edited by Godwin; 09-19-06 at 12:20 AM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Auto Dealer Script nuzzle PHP 17 04-14-10 08:34 PM
Displaying Mysql data as checked checkboxes djnaf PHP 7 03-24-06 05:10 AM
MySQL table problem perleo PHP 9 12-16-03 01:16 PM
asp: validating checkboxes in groups by groupid seala ASP 0 09-08-03 01:36 PM
asp: values in array not in order?? seala ASP 0 08-16-03 12:06 PM


All times are GMT -5. The time now is 07:28 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.