Radio Button Array

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
vjmehra
php-forum Active User
php-forum Active User
Posts: 32
Joined: Mon Mar 05, 2012 12:07 pm

Mon May 07, 2012 10:36 am

Hi,

I have 2 radio button's within a table and need to pass this array.

All other rows within my table can be sent as array's and captured using $_POST['field_name'][$ROW 1 value etc].

However for radio buttons this doesn't work.

As an example, a standard textbox I can send an array using the following:

echo "<td width='33%'><input type='Textbox' width='100%' id='test' name='test[]' value='" .$row['test']. "'></td>";

I can then capture it using:

$test = $_POST['test'][$iLine];

This works, no problem, however for radio buttons, I can't use a static array name, as presumably it dumps all rows into the array, if I do this, only the last row has a checked button, hence I need to use the following (where $checked includes the logic to decide whether the button should be checked or not, this works correctly):

echo "<input type='radio' name='".$row['venue_id']."[]' value='1'".$checked."/>P1"
echo "<input type='radio' name='".$row['venue_id']."[]' value='0'".$checked2." />P2";

I tried using the following:

var_dump($_POST[$row]['game_id'][$iLine]);

But this just returns a null value.

Any ideas?
vjmehra
php-forum Active User
php-forum Active User
Posts: 32
Joined: Mon Mar 05, 2012 12:07 pm

Mon May 07, 2012 10:48 am

I thought I'd knock up a quick simple form to show what I mean, not sure if this helps or not:

<html>
<head>
<title></title>
</head>
<body marginheight="0" topmargin="0" marginwidth="0"
leftmargin="0" style="margin:0;padding:0" bgcolor="#B0E0E6">
<h1></h1>

<form action="" method="post">


<?php

@ $db = new mysqli('xxx', 'xxx', 'xxx', 'xxx');
$query = "call sp_view_all_venues();";
$result = $db->query($query);
$num_results = $result->num_rows;

function db_result_to_array($result){
//---------------------------------------------------------------------
$res_array = array();

for ($count=0; $row = $result->fetch_assoc(); $count++) {
$res_array[$count]=$row;
}

return $res_array;
}
//---------------------------------------------------------------------


echo "<input type='submit' id='btnupdate' name='btnupdate' value='Update & Delete' >";


$irow=0;
while($row = $result->fetch_assoc())
{

if($row['venue1']==1)
$checked=" checked='checked'";
else
$checked='';

if($row['venue2']==1)
$checked2=" checked='checked'";
else
$checked2='';

echo "<table><tr><td>";
echo "<input type='radio' name='".$row[venue_id']."[]' value='1'".$checked."/>".$row['venue_id'];
echo "<input type='radio' name='".$row['venue_id']."[]' value='0'".$checked2." />P2";
echo "</td>";

$irow++;

}
echo "</tr></table>";

var_dump($_POST);

?>


</form>
</body>
</html>

the var dump shows:

array(85) { ["btnupdate"]=> string(15) "Update & Delete" [92]=> array(1) { [0]=> string(1) "1" } [90]=> array(1) { [0]=> string(1) "0" } [91]=> array(1) { [0]=> string(1) "0" } [88]=> array(1) { [0]=> string(1) "0" } [89]=> array(1) { [0]=> string(1) "0" } [87]=> array(1) { [0]=> string(1) "0" } [80]=> array(1) { [0]=> string(1) "0" } [79]=> array(1) { [0]=> string(1) "0" } [78]=> array(1) { [0]=> string(1) "0" } [77]=> array(1) { [0]=> string(1) "0" } [76]=> array(1) { [0]=> string(1) "0" } [75]=> array(1) { [0]=> string(1) "0" } [74]=> array(1) { [0]=> string(1) "0" } [73]=> array(1) { [0]=> string(1) "0" } [72]=> array(1) { [0]=> string(1) "0" } [71]=> array(1) { [0]=> string(1) "0" } [70]=> array(1) { [0]=> string(1) "0" } [69]=> array(1) { [0]=> string(1) "0" } [68]=> array(1) { [0]=> string(1) "0" } [67]=> array(1) { [0]=> string(1) "0" } [66]=> array(1) { [0]=> string(1) "0" } [65]=> array(1) { [0]=> string(1) "0" } [64]=> array(1) { [0]=> string(1) "0" } [63]=> array(1) { [0]=> string(1) "0" } [62]=> array(1) { [0]=> string(1) "0" } [61]=> array(1) { [0]=> string(1) "0" } [16]=> array(1) { [0]=> string(1) "0" } [15]=> array(1) { [0]=> string(1) "0" } [14]=> array(1) { [0]=> string(1) "0" } [13]=> array(1) { [0]=> string(1) "0" } [12]=> array(1) { [0]=> string(1) "0" } [11]=> array(1) { [0]=> string(1) "0" } [60]=> array(1) { [0]=> string(1) "0" } [59]=> array(1) { [0]=> string(1) "0" } [10]=> array(1) { [0]=> string(1) "0" } [9]=> array(1) { [0]=> string(1) "0" } [8]=> array(1) { [0]=> string(1) "0" } [7]=> array(1) { [0]=> string(1) "0" } [6]=> array(1) { [0]=> string(1) "0" } [5]=> array(1) { [0]=> string(1) "0" } [56]=> array(1) { [0]=> string(1) "0" } [55]=> array(1) { [0]=> string(1) "0" } [54]=> array(1) { [0]=> string(1) "0" } [53]=> array(1) { [0]=> string(1) "0" } [52]=> array(1) { [0]=> string(1) "0" } [51]=> array(1) { [0]=> string(1) "0" } [50]=> array(1) { [0]=> string(1) "0" } [4]=> array(1) { [0]=> string(1) "0" } [3]=> array(1) { [0]=> string(1) "0" } [58]=> array(1) { [0]=> string(1) "0" } [57]=> array(1) { [0]=> string(1) "0" } [49]=> array(1) { [0]=> string(1) "0" } [48]=> array(1) { [0]=> string(1) "0" } [47]=> array(1) { [0]=> string(1) "0" } [46]=> array(1) { [0]=> string(1) "0" } [45]=> array(1) { [0]=> string(1) "0" } [44]=> array(1) { [0]=> string(1) "0" } [43]=> array(1) { [0]=> string(1) "0" } [42]=> array(1) { [0]=> string(1) "0" } [41]=> array(1) { [0]=> string(1) "0" } [40]=> array(1) { [0]=> string(1) "0" } [39]=> array(1) { [0]=> string(1) "0" } [38]=> array(1) { [0]=> string(1) "0" } [37]=> array(1) { [0]=> string(1) "0" } [36]=> array(1) { [0]=> string(1) "0" } [35]=> array(1) { [0]=> string(1) "0" } [34]=> array(1) { [0]=> string(1) "0" } [33]=> array(1) { [0]=> string(1) "0" } [32]=> array(1) { [0]=> string(1) "0" } [31]=> array(1) { [0]=> string(1) "0" } [30]=> array(1) { [0]=> string(1) "0" } [29]=> array(1) { [0]=> string(1) "0" } [21]=> array(1) { [0]=> string(1) "0" } [20]=> array(1) { [0]=> string(1) "0" } [19]=> array(1) { [0]=> string(1) "0" } [18]=> array(1) { [0]=> string(1) "0" } [17]=> array(1) { [0]=> string(1) "0" } [28]=> array(1) { [0]=> string(1) "0" } [27]=> array(1) { [0]=> string(1) "0" } [26]=> array(1) { [0]=> string(1) "0" } [25]=> array(1) { [0]=> string(1) "0" } [24]=> array(1) { [0]=> string(1) "0" } [23]=> array(1) { [0]=> string(1) "0" } [22]=> array(1) { [0]=> string(1) "0" } }

Which is expected, but I have no idea how to extract and make sense of the submitted data....
User avatar
minimihi
php-forum GURU
php-forum GURU
Posts: 236
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Mon May 07, 2012 12:38 pm

Radio buttons are grouped by same name. If two radio buttons have same name - only one of them can be selected.
So it's normal that you have such data sent via POST.
User avatar
Nullsig
php-forum GURU
php-forum GURU
Posts: 979
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Mon May 07, 2012 12:47 pm

Radio buttons are "select one of many" they do not support the "[]" notation in their name. Instead you name all of the buttons with the same name and give them different values then the posted value is the value of the selected button.

Example

Code: Select all

Favorite Fictional Character:
<input type='radio' name='charname' value='Dexter Morgan' selected> Dexter Morgan <br>
<input type='radio' name='charname' value='Naruto'> Naruto<br>
<input type='radio' name='charname' value='Dr. Who'> Dr. Who <br>
<input type='radio' name='charname' value='River Tam'> River Tam <br>
<input type='radio' name='charname' value='Dean Winchester'> Dean Winchester <br>
The default selection here is Dexter Morgan. If the user then chose Dr. Who and submitted the form then the variable $_POST['charname'] would be set to "Dr. Who".

In your case you are creating multiple button groups, each group is tagged with the venue_id using the "name" attribute. Then you define the value for each button.

If the venue_id is an integer than you might run into problem. You might want to try concatenating a string before the venue_id in the name attribute so each group will be

Code: Select all

name='Venue" . $venue_id . "'
vjmehra
php-forum Active User
php-forum Active User
Posts: 32
Joined: Mon Mar 05, 2012 12:07 pm

Mon May 07, 2012 12:50 pm

yep, but I still can't work out how to capture that string using $_POST...thats the key issue here (I think)?
vjmehra
php-forum Active User
php-forum Active User
Posts: 32
Joined: Mon Mar 05, 2012 12:07 pm

Wed May 09, 2012 10:55 am

I tried using a string too, like you suggested, but no luck there either :-(

Any ideas? This must be possible, the scenario comes up on web forms frequently enough...
User avatar
Nullsig
php-forum GURU
php-forum GURU
Posts: 979
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Wed May 09, 2012 10:58 am

I explained everything I could about your question. If you still don't understand you should try reading my post again. If you need more assistance I would suggest posting your updated code so we can see where you have gone wrong.

I suggest using the [ code] and [ /code] tags to segregate your code in future posts.
vjmehra
php-forum Active User
php-forum Active User
Posts: 32
Joined: Mon Mar 05, 2012 12:07 pm

Wed May 09, 2012 11:19 am

Sorry, but I'm very confused, you suggested the following unless I'm mistaken:

Code: Select all

echo "<input type='radio' name='Venue".$row['venue_id']."' value='1'".$checked1." />P1"; 
echo "<input type='radio' name='Venue".$row['venue_id']."' value='0'".$checked2." />P2"; 
I still am unable to work out how to capture the sent value, I see the following in the dump:

Code: Select all

["Game_ID92"]=> string(1) "1" ["Game_ID90"]=> string(1) "0" ["Game_ID91"]=> string(1) "0" 
Which makes sense, but how can I capture each row, normally for example I can simply use:

Code: Select all

$venue  = $_POST['venue_id'][$iLine];
(where $iLine represents the respective row)

However I am not sure how to construct the statement in this case?

$venue = $_POST[????]

That (I think) is the missing link in my puzzle..
User avatar
Nullsig
php-forum GURU
php-forum GURU
Posts: 979
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Wed May 09, 2012 11:33 am

Should be:

Code: Select all

$venue  = $_POST['Game_ID' . $iLine];
vjmehra
php-forum Active User
php-forum Active User
Posts: 32
Joined: Mon Mar 05, 2012 12:07 pm

Wed May 09, 2012 11:43 am

Sorry I've confused the issue here I think, ignore the game_id variable, the line for this is:

Code: Select all

$game_id  = $_POST['gameid'][$iLine];
This works correctly, however the venue_id is confusing me, as I need to capture:

name='Venue".$row['venue_id']."'

as opposed to simply:

name=".$row['venue_id']."'

and I don't know how to structure that?
User avatar
Nullsig
php-forum GURU
php-forum GURU
Posts: 979
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Wed May 09, 2012 11:48 am

Sorry you probably read my post before I corrected it.

You should use:

Code: Select all

$venue  = $_POST['Venue' . $iLine];
vjmehra
php-forum Active User
php-forum Active User
Posts: 32
Joined: Mon Mar 05, 2012 12:07 pm

Wed May 09, 2012 12:10 pm

Code: Select all

$_POST['Venue' . $iLine];
Returns a NULL unfortunately

Code: Select all

$venue  = $_POST['venue'][$iLine];
Works where 'venue' is just a textbox, however so I'm guessing somehow the way the variable is being captured is incorrect, given that in this instance the $row['venue_id'] is in the name attribute, would I not need to try and capture this rather than $iLine?
User avatar
Nullsig
php-forum GURU
php-forum GURU
Posts: 979
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Wed May 09, 2012 12:24 pm

If the inputs you have are:

Code: Select all

<input type='radio' name='Venue90' value='1' checked>Yes
<input type='radio' name='Venue90' value='0'>No


<input type='radio' name='Venue91' value='1' >Yes
<input type='radio' name='Venue91' value='0' checked>No
Then to pull them from post you would need to do the following:

Code: Select all

$venue90 = $_POST['Venue90'];
$venue91 = $_POST['Venue91'];
Now assuming that you are iterating through a list of button groups from 0-91 (Venue0-Venue91) and you want to grab all of their values you would do the following:

Code: Select all

$venueArray = array();
for($iLine = 0; $iLine <= 91; $iLine++){
      $venueArray[$iLine] = $_POST['Venue' . $iLine];
}
This would return an array with values of:
$venueArray[0] = $_POST['Venue0'];
$venueArray[1] = $_POST['Venue1'];
$venueArray[2] = $_POST['Venue2'];
.
.
.
$venueArray[90] = $_POST['Venue90'];
$venueArray[91] = $_POST['Venue91'];
vjmehra
php-forum Active User
php-forum Active User
Posts: 32
Joined: Mon Mar 05, 2012 12:07 pm

Wed May 09, 2012 1:10 pm

Your code looks logical to me, but I can't get it working.

I get a null result every time:

$_POST['Venue' . $iLine];

doesn't seem to capture any value...
User avatar
Nullsig
php-forum GURU
php-forum GURU
Posts: 979
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Thu May 10, 2012 4:07 am

I just used this code to test:

Code: Select all

<?php
    $_POST['Venue91'] = 1;
	$_POST['Venue92'] = 2;
	$_POST['Venue93'] = 3;
	
	for($i = 91; $i <= 93; $i++){
		echo "Venue" . $i . " is equal to " . $_POST['Venue' . $i] . "<br>";
	}
    ?>
I got this result:

Code: Select all

Venue91 is equal to 1
Venue92 is equal to 2
Venue93 is equal to 3
User avatar
freshnet
php-forum GURU
php-forum GURU
Posts: 365
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Thu May 10, 2012 10:20 am

Post the exact code you're using. Also it may be worth looking at the variables to see if they are assigned

Code: Select all

$arr = get_defined_vars();
print_r($arr["$_POST"]);

vjmehra
php-forum Active User
php-forum Active User
Posts: 32
Joined: Mon Mar 05, 2012 12:07 pm

Fri May 11, 2012 11:52 am

hmm still struggling to see where I'm going wrong, here is my exact code:

Code: Select all

<html>
<head>
<title>test page</title>
</head>
<body marginheight="0" topmargin="0" marginwidth="0" 
leftmargin="0" style="margin:0;padding:0" bgcolor="#B0E0E6">
<h1></h1>

<form action="test97.php" method="post">


<?php

@ $db = new mysqli('xxx', 'xxx', 'xxx', 'xxx');
$query = "call sp_venue();";
$result = $db->query($query);
$num_results = $result->num_rows;

function db_result_to_array($result){
//---------------------------------------------------------------------
$res_array = array();

for ($count=0; $row = $result->fetch_assoc(); $count++) {
$res_array[$count]=$row;
}

return $res_array;
}
//---------------------------------------------------------------------


echo "<input type='submit' id='btnupdate' name='btnupdate' value='Update & Delete' >";


$irow=0;
while($row = $result->fetch_assoc())
   {

echo "<table><tr><td>";
   echo "<input type='radio' name='Venue_ID".$row['venue_id']."' value='1' checked='checked'/>P1";
   echo "<input type='radio' name='Venue_ID".$row['venue_id']."' value='0'/>P2";  
echo "</td>";

$irow++;

  }
 echo "</tr></table>";

$venueArray = array();
for($iLine = 0; $iLine <= 91; $iLine++){
      $_POST['Venue_id' . $iLine];
}

var_dump($_POST);

$arr = get_defined_vars();
print_r($arr["$_POST"]);


?>


</form>
</body>
</html>
User avatar
minimihi
php-forum GURU
php-forum GURU
Posts: 236
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Fri May 11, 2012 1:24 pm

1st. Array indexes are case sensitive. So it should be:
$_POST['Venue_ID' . $iLine];
vjmehra
php-forum Active User
php-forum Active User
Posts: 32
Joined: Mon Mar 05, 2012 12:07 pm

Fri May 11, 2012 2:26 pm

Yep, okay I have it working on my test page now, but on my actual page its very odd, it keeps returning a NULL (the case is definitely correct here ;-)

Oddly when I take a var_dump I get the following:

Code: Select all

["G_ID104"]=> string(1) "1" ["G_ID92"]=> string(1) "0" ["G_ID90"]=> string(1) "0" ["G_ID91"]=> string(1) "0" ["G_ID88"]=> string(1) "0" ["G_ID89"]=> string(1) "0" ["G_ID87"]=> string(1) "0" ["G_ID80"]=> string(1) "0" ["G_ID79"]=> string(1) "0" ["G_ID78"]=> string(1) "0" ["G_ID77"]=> string(1) "0" ["G_ID76"]=> string(1) "0" ["G_ID75"]=> string(1) "0" ["G_ID74"]=> string(1) "0" ["G_ID73"]=> string(1) "0" ["G_ID72"]=> string(1) "0" ["G_ID71"]=> string(1) "0" ["G_ID70"]=> string(1) "0" ["G_ID69"]=> string(1) "0" ["G_ID68"]=> string(1) "0" ["G_ID67"]=> string(1) "0" ["G_ID66"]=> string(1) "0" ["G_ID65"]=> string(1) "0" ["G_ID64"]=> string(1) "0" ["G_ID63"]=> string(1) "0" ["G_ID62"]=> string(1) "0" ["G_ID61"]=> string(1) "0" ["G_ID16"]=> string(1) "0" ["G_ID15"]=> string(1) "0" ["G_ID14"]=> string(1) "0" ["G_ID13"]=> string(1) "0" ["G_ID12"]=> string(1) "0" ["G_ID11"]=> string(1) "0" ["G_ID60"]=> string(1) "0" ["G_ID59"]=> string(1) "0" ["G_ID10"]=> string(1) "0" ["G_ID9"]=> string(1) "0" ["G_ID8"]=> string(1) "0" ["G_ID7"]=> string(1) "0" ["G_ID6"]=> string(1) "0" ["G_ID5"]=> string(1) "0" ["G_ID56"]=> string(1) "0" ["G_ID55"]=> string(1) "0" ["G_ID54"]=> string(1) "0" ["G_ID53"]=> string(1) "0" ["G_ID52"]=> string(1) "0" ["G_ID51"]=> string(1) "0" ["G_ID50"]=> string(1) "0" ["G_ID4"]=> string(1) "0" ["G_ID3"]=> string(1) "0" ["G_ID58"]=> string(1) "0" ["G_ID57"]=> string(1) "0" ["G_ID49"]=> string(1) "0" ["G_ID48"]=> string(1) "0" ["G_ID47"]=> string(1) "0" ["G_ID46"]=> string(1) "0" ["G_ID45"]=> string(1) "0" ["G_ID44"]=> string(1) "0" ["G_ID43"]=> string(1) "0" ["G_ID42"]=> string(1) "0" ["G_ID41"]=> string(1) "0" ["G_ID40"]=> string(1) "0" ["G_ID39"]=> string(1) "0" ["G_ID38"]=> string(1) "0" ["G_ID37"]=> string(1) "0" ["G_ID36"]=> string(1) "0" ["G_ID35"]=> string(1) "0" ["G_ID34"]=> string(1) "0" ["G_ID33"]=> string(1) "0" ["G_ID32"]=> string(1) "0" ["G_ID31"]=> string(1) "0" ["G_ID30"]=> string(1) "0" ["G_ID29"]=> string(1) "0" ["G_ID21"]=> string(1) "0" ["G_ID20"]=> string(1) "0" ["G_ID19"]=> string(1) "0" ["G_ID18"]=> string(1) "0" ["G_ID17"]=> string(1) "0" ["G_ID28"]=> string(1) "0" ["G_ID27"]=> string(1) "0" ["G_ID26"]=> string(1) "0" ["G_ID25"]=> string(1) "0" ["G_ID24"]=> string(1) "0" ["G_ID23"]=> string(1) "0" ["G_ID22"]=> string(1) "0"
However using the following:

Code: Select all

  foreach ($_POST["cbupdate"] as $sKey =>$iLine) {

   $G_ID = $_POST['G_ID' . $iLine];

echo "G_ID: ".$G_ID;
}
Returns a Null every time...the data is obviously passing...so my attempt to capture it must be wrong, but how?
vjmehra
php-forum Active User
php-forum Active User
Posts: 32
Joined: Mon Mar 05, 2012 12:07 pm

Sat May 12, 2012 6:57 am

Ok, well I've got it working as follows:

Code: Select all

if (isset($_POST["cbupdate"])) {
  foreach ($_POST["cbupdate"] as $sKey =>$iLine) {

    // $iLine is the check box value, 0 for first line, 1 for 2nd line etc

    $game_id  = $_POST['gameid'][$iLine];

$venueArray = array();
for($iLine = $game_id; $iLine <=$game_id; $iLine++){
      $wonlag1=$_POST['G_ID' . $iLine];
}
(cbupdate is a checkbox, so this only executes for the checked rows).
Post Reply