Jump to content

Radio Buttons


lazycarrot

Recommended Posts

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

Link to comment
Share on other sites

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']];
}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.