954,598 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Radio button array

Hi guys,

Hope you can help (and hope I can explain my problem OK!)

I am designing a sports site which takes three values from a user to input match details: a date (text input), a versus (text input), and if the match is home or away (radio buttons). Several matches can be inputed in one page, so I am trying to pass my PHP script three arrays (date, versus, and home/away). For the text input I simply name the inputs "date[]" and "versus[]", however am struggling with the radio inputs.

Basically if I name all the radio buttons "homeaway[]", they are all part of the same set and so I can only select one option (when I want to be able to select one option per home/away pair), so I need a different name per pair, however then I cannot add it to a single array!

Does anyone have any suggestions? I would preferably avoid "homeaway1[]", "homeaway2[]", etc. as the number of match inputs per page is dynamic using JavaScript.

Hope I have made myself clear!

Thanks in advance.

dshiells
Newbie Poster
6 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

Could you post whatever code you've done so that we can get a clearer picture?

if you want all of the radio in one array that you may have to make it a multidimensional array.

ex: the input name

option[home][] and option[away][]

qazplm114477
Junior Poster
193 posts since Apr 2010
Reputation Points: 24
Solved Threads: 37
 

I think this is the way to go, however, I'd do it slightly differently:

No labels used, but you'll get the idea:

<?php

if(isset($_POST['sub_it'])){
  print_r($_POST);
}
?>

<form method="post">
<input type="date" name="date[]" />
<input type="text" name="versus[]" />
<input type="radio" name="ha[0]" value="home" checked="checked" />
<input type="radio" name="ha[0]" value="away" />

<input type="date" name="date[]" />
<input type="text" name="versus[]" />
<input type="radio" name="ha[1]" value="home" checked="checked" />
<input type="radio" name="ha[1]" value="away" />

<input type="date" name="date[]" />
<input type="text" name="versus[]" />
<input type="radio" name="ha[2]"  value="home" checked="checked" />
<input type="radio" name="ha[2]" value="away" />
<input type="submit" value="go" name="sub_it" />
</form>


This can be easily created with a php for loop:

$groups = "";
for($x=0;$x<6;$x++){
  $groups .= "<input type=\"date\" name=\"date[]\" />\n
<input type=\"text\" name=\"versus[]\" />\n
<input type=\"radio\" name=\"ha[$x]\" value=\"home\" checked=\"checked\" />\n
<input type=\"radio\" name=\"ha[$x]\" value=\"away\" />\n";
}

....


echo $groups;
diafol
Rhod Gilbert Fan (ardav)
Moderator
7,796 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

Hi guys,

Thanks for your replys! I have given ardav's way a go and it works perfectly (i.e. produces: Array ( [0] => away [1] => home [2] => away [3] => home )).

Thanks so much for your help, right on the money!

dshiells
Newbie Poster
6 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: