Jump to content

Radio Button Array


vjmehra

Recommended Posts

 

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?

Link to comment
https://forums.phpfreaks.com/topic/262272-radio-button-array/
Share on other sites

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....

Link to comment
https://forums.phpfreaks.com/topic/262272-radio-button-array/#findComment-1344097
Share on other sites

See how this works.  Untested.

<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 "<table>";
echo "<tr><td>";
echo "<input type='submit' id='btnupdate' name='btnupdate' value='Update & Delete' >";
echo "</td></tr>";

$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 "<tr><td>";
echo "<input type='radio' name='venue_id[".$row['venue_id']."]' value='1'".$checked."/>".$row['venue_id']."";
echo "<input type='radio' name='venue_id[".$row['venue_id']."]' value='0'".$checked2." />P2"; 
echo "</td></tr>";

$irow++;

}
echo "</table>";

echo "<pre>";
print_r($_POST);
echo "</pre>";
?>


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

Link to comment
https://forums.phpfreaks.com/topic/262272-radio-button-array/#findComment-1344099
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.