Reputation: 1
I am trying to send data from multiple Radio Button with
name="answer<?php echo$data[id]?>"
.
with this $_POST[answer[]]
how I can save data ?
here is the code. thanks.
`<form method="post" action="proses.php">`<tbody>
<?php
$no=1;
$getdata = mysql_query("SELECT * FROM pertanyaan where kategori='pekerjaan' order by kategori desc");
while($data=mysql_fetch_array($getdata)){
?>
<tr>
<td><?php echo $no ?></td>
<td><?php echo $data[pertanyaan]?></td>
<td><input type="radio" name="answer<?php echo $data[id] ?>" value="ss"></td>
<td><input type="radio" name="answer<?php echo $data[id] ?>" value="s"></td>
<td><input type="radio" name="answer<?php echo $data[id] ?>" value="b"></td>
<td><input type="radio" name="answer<?php echo $data[id] ?>" value="ts"></td>
<td><input type="radio" name="answer<?php echo $data[id] ?>" value="sts"></td>
</tr>
</form>
Upvotes: 0
Views: 210
Reputation: 41
<form method="post" action="proses.php">`<tbody>
<?php
$no=1;
$getdata = mysql_query("SELECT * FROM pertanyaan where kategori='pekerjaan' order by kategori desc");
while($data=mysql_fetch_array($getdata)){
?>
<tr>
<td><?php echo $no ?></td>
<td><?php echo $data[pertanyaan]?></td>
<td><input type="radio" name="answer[<?php echo $data[id] ?>]" value="ss"></td>
<td><input type="radio" name="answer[<?php echo $data[id] ?>]" value="s"></td>
<td><input type="radio" name="answer[<?php echo $data[id] ?>]" value="b"></td>
<td><input type="radio" name="answer[<?php echo $data[id] ?>]" value="ts"></td>
<td><input type="radio" name="answer[<?php echo $data[id] ?>]" value="sts"></td>
</tr>
</form>
//process.php
<?php
if(isset($_POST)) {
$getRadio = $_POST['answer'];
if(count($getRadio) > 0) {
// While updating data
foreach($getRadio as $key => $val) {
$query = "Update pertanyaan set kategori = $val WHERE id = $key";
mysql_query($query);
}
//Insert Data
foreach($getRadio as $key => $val) {
//$key Reference Id of Table
// val get checked radio button value
$query = "insert into TABLENAME values('NULL','$val','$key')";
mysql_query($query);
}
}
}?>
Upvotes: 1
Reputation: 556
You need to put square [] brackets in the name of radio battons.
<form method="post" action="proses.php">`<tbody>
<?php
$no=1;
$getdata = mysql_query("SELECT * FROM pertanyaan where kategori='pekerjaan' order by kategori desc");
while($data=mysql_fetch_array($getdata)){
?>
<tr>
<td><?php echo $no ?></td>
<td><?php echo $data[pertanyaan]?></td>
<td><input type="radio" name="answer[<?php echo $data[id] ?>]" value="ss"></td>
<td><input type="radio" name="answer[<?php echo $data[id] ?>]" value="s"></td>
<td><input type="radio" name="answer[<?php echo $data[id] ?>]" value="b"></td>
<td><input type="radio" name="answer[<?php echo $data[id] ?>]" value="ts"></td>
<td><input type="radio" name="answer[<?php echo $data[id] ?>]" value="sts"></td>
</tr>
</form>
Upvotes: 1