[RESOLVED] Selections and mixed values
Hi,
How would I deal with mixture of radio buttons, checkbox values? At the moment I have this code:
PHP Code:
if(isset($_POST['genconsent'], $_POST['photo'], $_POST['mon'], $_POST['moncare'], $_POST['tue'], $_POST['tuecare'], $_POST['wed'], $_POST['wedcare'], $_POST['thur'], $_POST['thurcare'], $_POST['fri'],$_POST['fricare'],$_POST['mon2fri'], $_POST['t-shirt'],$_POST['church'],$_POST['mailout'],$_POST['wom'],$_POST['letterbox'],$_POST['newspaper'],$_POST['internet'],$_POST['school'],$_POST['other'])){
$genconsent = "Y";
$photo = "Y";
$mon = "Y";
$moncare = "Y";
$tue = "Y";
$tuecare = "Y";
$wed = "Y";
$wedcare = "Y";
$thur = "Y";
$thurcare = "Y";
$fri = "Y";
$fricare = "Y";
$mon2fri = "Y";
$tshirt = "Y";
$church = "Y";
$mailout = "Y";
$wom = "Y";
$letterbox = "Y";
$newspaper = "Y";
$net = "Y";
$school = "Y";
$other = "Y";
}else{
$genconsent = "N";
$photo = "N";
$mon = "N";
$moncare = "N";
$tue = "N";
$tuecare = "N";
$wed = "N";
$wedcare = "N";
$thur = "N";
$thurcare = "N";
$fri = "N";
$fricare = "N";
$mon2fri = "N";
$tshirt = "N";
$church = "N";
$mailout = "N";
$wom = "N";
$letterbox = "N";
$newspaper = "N";
$net = "N";
$school = "N";
$other = "N";
}
If set the value = "Y" else value = "N" . However, this method won't work if this is a mixtures of yes and no values. How would I retrieve the actual checkbox or radio button values so I can assign them to the above variables?
Thanks,
Nightwalker
Re: Selections and mixed values
not sure why you didn't get this the first time you posted something that looked similar to this and I told you it was a bad idea. checkboxes will never be set on a form unless they are checked. that means unless these checkboxes you have in your long isset() statement are required, then this would never get triggered. this is a terrible way of trying to do things.
radio buttons and checkboxes are just like any other form object. they hold the value you give them on the form. so, if I have these elements on my form:
HTML Code:
<input type="radio" name="somefield" value="Y" />
<input type="radio" name="somefield" value="N" />
<input type="checkbox" name="someotherfield" />
then when I submit my form, $_POST['somefield'] will be set to either Y or N depending on which radio option was clicked (or $_POST['somefield'] may not be set at all because I haven't given it a default value -- because of this, you should always set a default selection by using the selected attribute on whichever radio <input> would be most common: selected="selected"). $_POST['someotherfield'] will not be set if I do not check it, or will be set if I do check it. it will be equal to its value on the form (which I have omitted in my form, but you don't have to omit it) if it is checked.
how would I store these values in a variable? simple:
PHP Code:
<?php
//radio buttons
if(isset($_POST['somefield'])){
//set the value to whatever radio button was selected
$somefield = $_POST['somefield'];
}else{
//this wasn't selected at all. you can give a default value.
$somefield = 'N';
}
//checkbox
if(isset($_POST['someotherfield'])){
$someotherfield = 'Y';
}else{
$someotherfield = 'N';
}
?>
but, this would take up a lot of much space. if you understand it, use a ternary operator instead:
PHP Code:
//radio
$somefield = (isset($_POST['somefield'])) ? $_POST['somefield'] : 'N';
//checkbox
$someotherfield = (isset($_POST['someotherfield'])) ? 'Y' : 'N';
the two examples of code above do the exact same thing.
Re: Selections and mixed values
Yeah, I realized that last night that I had probably done this before and could have checked the source code of a previous website of mine to see if I had used the method I was looking for before. Thanks for the help though!
Re: [RESOLVED] Selections and mixed values
Also, don't make the easy mistake of thinking the values from radio buttons and checkboxes are 'safe' — they can be set to any value just like any other GET or POST parameter.