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.

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][]

Member Avatar for diafol

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" />
<br />
<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" />
<br />
<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;
commented: Understood and answered my question perfectly! +0

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.