ShaolinF Posted December 13, 2007 Share Posted December 13, 2007 Hi, I have the 3 following radio boxes and I want to add whichever one is selected into a session, how would I do this ? <input type="radio" name="group2" value="Water"> Water<br> <input type="radio" name="group2" value="Beer"> Beer<br> <input type="radio" name="group2" value="Wine" checked> Wine<br> Quote Link to comment Share on other sites More sharing options...
Stooney Posted December 13, 2007 Share Posted December 13, 2007 switch($_POST['group2']){ case 'Water': $_SESSION['group2']=$group2; break; case 'Beer': $_SESSION['group2']=$group2; break; case 'Wine': $_SESSION['group2']=$group2; break; } edit: forgot the breaks Quote Link to comment Share on other sites More sharing options...
revraz Posted December 13, 2007 Share Posted December 13, 2007 Why do you need CASEs when a radio can only send one of the values. Quote Link to comment Share on other sites More sharing options...
Stooney Posted December 13, 2007 Share Posted December 13, 2007 A switch will execute the code you want based on the value of the variable, just like if and else if. Makes sense to me at least. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 13, 2007 Share Posted December 13, 2007 They all do the same thing, there is no need. Just set the radio name to the session variable. Quote Link to comment Share on other sites More sharing options...
CMC Posted December 13, 2007 Share Posted December 13, 2007 Yup that switch statement will work for sure. Using an if statement though is less code for the same function. if(isset($_POST['group2'])){ $_SESSION['group2'] = $_POST['group2']; } Either way will work. Quote Link to comment Share on other sites More sharing options...
Stooney Posted December 13, 2007 Share Posted December 13, 2007 Oh damn. You have a point, I'm retarded. Listen to revraz. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 13, 2007 Share Posted December 13, 2007 no biggy, I was just curious. Some times things are done for good reasons and I like to learn too. Quote Link to comment Share on other sites More sharing options...
PC Nerd Posted December 13, 2007 Share Posted December 13, 2007 you dont even need to use the case. $_SESSION[group2'] = $_POST['group2']; just move it directly into the session array *** assumign youve validated all data at he top of the page etc. Quote Link to comment Share on other sites More sharing options...
ShaolinF Posted December 13, 2007 Author Share Posted December 13, 2007 Thanks. How do I validate ? and what does isset mean ? Quote Link to comment Share on other sites More sharing options...
revraz Posted December 13, 2007 Share Posted December 13, 2007 Validate means you check the variable before you send it and sanitize it for xss injections. ISSET means if something is set. Quote Link to comment Share on other sites More sharing options...
CMC Posted December 13, 2007 Share Posted December 13, 2007 isset checks if the variable is set. Ex: $submit = $_POST['submit']; //check if the form has been submitted if(isset($submit)){ echo "The form has been submitted!"; }else{ //otherwise if the form hasn't been submitted, display the form echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">"; echo "<input type=\"submit\" value=\"Submit\" name=\"submit\">"; } What that script does is it checks if a user has pressed the submit button. If they have pressed the submit button (it is checked using isset), it displays the message 'The form has been submitted'. If they have not clicked submit, it will show them the form. By checking if the form has been submitted using isset, this prevents the script from automatically being run when someone visits the page. PHP isset Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.