lazycarrot Posted August 9, 2010 Share Posted August 9, 2010 I'm totally naive to php coding and i'm trying to modify my website which was written for me by someone else in php. So i'm kind of shooting in the dark - but my modification really isn't that difficult... here goes. I already have a form set up allowing the user to select values from drop down menus. To this I want to add a further selection from a choice of 4 radio buttons. This is the coding I've used (i've stripped it down to basics for simplicity) it's inserted between the 'form' tags <input type=radio name="choice" value="FP"> Front Print<br> <input type=radio name="choice" value="BP"> Back Print<br> <input type=radio name="choice" value="PP"> Pocket Print<br> <input type=radio name="choice" value="CP"> Custom Print<br> Ok - this part displays ok and seems to function as expected. The form 'Action' sends the user to another php page so i need to access these values on a different page. This is where I'm running blind. I just want a simple 'if' statement to set a variable based on the value of the radio buttons. So something that looks like this.... if (choice[1].checked=true) {$printchoice = "Front Print";} if (choice[2].checked=true) {$printchoice = "Back Print";} if (choice[3].checked=true) {$printchoice = "Pocket Print";} if (choice[4].checked=true) {$printchoice = "Custom Print";} This part doesn't work - i've tried various combinations but i'm just taking wild guesses. Any help would be much appreciated - thanks Quote Link to comment Share on other sites More sharing options...
xangelo Posted August 9, 2010 Share Posted August 9, 2010 You would access the value via $_POST. So: $_POST['choice'] would equal either FP,BP,PP,CP or NULL depending on what was chosen. To format it more in line with your code switch($_POST['choice']){ case 'FP': $printchoice = 'Front Print'; break; case 'BP': $printchoice = 'Back Print'; break; case 'PP': $printchoice = 'Pocket Print'; break; case 'CP': $printchoice = 'Custom Print'; break; default: $printchoice = 'Front Print'; break; } I added a default value, incase the user doesn't select anything. Another way (if you don't have to do any further processing) would be like this: $allowed = array('FP'=>'Front Print','BP' => 'Back Print', 'PP' => 'Pocket Print', 'CP' => 'Custom Print'); if(array_key_exists($_POST['choice'],$allowed)) { $printchoice = $allowed[$_POST['choice']]; } Quote Link to comment Share on other sites More sharing options...
lazycarrot Posted August 10, 2010 Author Share Posted August 10, 2010 Thanks xangelo - this is a real help... Not managed to get it to work yet but at least now i know that i'm looking in the right direction. Cheers! Quote Link to comment Share on other sites More sharing options...
lazycarrot Posted August 11, 2010 Author Share Posted August 11, 2010 Ok - can't get this to work but.... i think i may have misled in my original description. In fact the radio buttons are set on one page. The form method takes me to a 2nd page. But I don't try to access the radio button code for another several pages after that. I have a suspicion that I need to access the radio button code immediately i arrive at the page directed by the method statement - can't find anywhere that confirms this but it kinda makes sense. So - do i need to capture this info straight away and then pass it along to the other pages somehow? Or am i chasing another red herring? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 11, 2010 Share Posted August 11, 2010 When the form has been submitted you'll need to capture the selected value first and then pass this value as a hidden field to your next form. Quote Link to comment Share on other sites More sharing options...
lazycarrot Posted August 11, 2010 Author Share Posted August 11, 2010 Thanks - it helps just to know you're looking in the right place! 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.