Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checkbox variable

Status
Not open for further replies.

d0s

Programmer
Apr 15, 2004
48
US
yep me again....Ok after searching serveral sites i came down to a question i couldn't find the answer to. What im doing is having a set of checkboxes each one representing something different..eg Red, Blue, Green, Black blah blah. When the user selects however many checkboxes, i want to get the variable when they submit and if its possible when its emailed to be displayed with , after it except for the last one. Now, while searching i came across alot of people talking about arrays like colors[] or food[] wutever. I also read tho, that if you set a value to the checkbox you can't use if statement to check if the variable is null or if it has been checked. So i have this.

checkbox 1
checkbox 2
checkbox 3
checkbox 4
whatever they click wether being 1 or 2 or even all of them, i was wanting it to display the variable like this person likes varibale 1, variable 2, variable 4 if they checked the 1st 2nd and fourth checkbox. Any idea's? Thanks
 
You mean like this...
Code:
<form ...>
<input type=checkbox name=something[] value="red">
<input type=checkbox name=something[] value="grass">
.
.
.
<input type=submit name=submit value="Send me what you like">
</form>
...
<?
if (isset($_POST['submit']) && ($_POST['submit'] == 'Send me what you like')) {
$something = (isset($_POST['something']))?$_POST['something']:array();
$you_like = explode(",",$something);
echo 'You like: '.$you_like."<br>\n";
}
?>

This code has not been tested ... (I just wrote it now) :)

Ken
 
ah koo koo man..thanks for the code..definately getting really close to getting this thing done...only problem im having now is no matter what i select, it says Array in place of the variables its suppose to display any ideas on wut that could be..here's some of the code
Code:
if ($_POST) {
if (isset($_POST['submit']) && ($_POST['submit'] == 'Send')) {
$something = (isset($_POST['something']))?$_POST['something']:array();
$you_like = explode(",",$something);
}

$name = $_POST['name'];
$email = $_POST['email'];
$text = " $name has requested to join. Below is the information\n\n";
$text .= "****************************************************\n\n";
$text .= "Name: $name \n";
$text .= "Email: $email \n";
$text .= "Games: $you_like \n";
$text .= "****************************************************\n";
$text .= $_POST['text'];
}
?>
thats just part of it i thought was the important part.
 
That's because if you name your checkboxes like:

<input type="checkbox" name="something[]" value="red">
<input type="checkbox" name="something[]" value="grass">

then when submitted, $_POST['something'] will itself be an array. The first checked box's value will be in $_POST['something'][0], the second in $_POST['something'][1], etc.




Want the best answers? Ask the best questions!

TANSTAAFL!!
 
ok now im confused again...so do i need to use another if statement to see which one's were clicked?
 
Only the values for those checkboxes that were clicked will appear in the array. You don't have to test -- you can assume that any values in that array were from checkboxes that were checked.

You could use a loop to iterate through the values.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top