Reputation: 125
I have a page with some radio buttons and form. When I select one radio button I need its id to be set to $_POST['id'] varible so when I submit the form I can check which radio button was selected and act accordingly. I know I can't do it directly so tried using
onclick="document.getElementById("id").value="'.$row['id'].'"
on radio button but it didn't work.
$d="select * from {$_c['dbprefix']}card where active='1' order by s_sent desc, s_order asc limit 0,{$_c['filtr']['step']}";
$r=mysql_query($d);
if (mysql_num_rows($r)>0) {
echo '<div class="cards">';
echo '<form method="POST">';
$i=0;
while ($row=mysql_fetch_array($r)):
echo '<div class="item">';
echo '<input id="'.$row['id'].'" type="radio" name="id" class="detail">'.$_l['detailtitle'].'</input>';
echo '<label for="'.$row['id'].'" class="itemin" style="height:'.$_c['card']['sy'].'px"><img src="pub/cards/'.$row['id'].'s.jpg" /></lable>';
echo '</div>';
$i++;
if ($i%3==0) echo '<div class="cl-left"></div>';
endwhile;
echo '<div class="cl-left"></div>';
echo '</div>';
}
?>
<div class="dvasl">
<div class="sl1">
<fieldset>
<legend>Saatja andmed</legend>
<table class="info">
<colgroup><col width="12%"><col></colgroup>
<tr>
<th>Nimi:</th>
<td><input type="text" name="item[from_name]" value="<?php echo $ap['set']['from_name']; ?>" class="inpedit"></td>
</tr>
<tr>
<th>Email:</th>
<td><input type="text" name="item[from_email]" value="<?php echo $ap['set']['from_email']; ?>" class="inpedit"></td>
</tr>
</table>
</fieldset>
<fieldset>
<legend>Saaja andmed</legend>
<table class="info">
<colgroup><col width="12%"><col></colgroup>
<tr>
<th>Nimi:</th>
<td><input type="text" name="item[to_name]" value="<?php echo $ap['set']['to_name']; ?>" class="inpedit"></td>
</tr>
<tr>
<th>Email:</th>
<td><input type="text" name="item[to_email]" value="<?php echo $ap['set']['to_email']; ?>" class="inpedit"></td>
</tr>
</table>
</fieldset>
</div>
<div class="sl2">
<fieldset>
<legend>E-kaardi tekst</legend>
<table class="info">
<tr>
<td><textarea name="item[text]" cols="20" rows="8" class="inpedit2"></textarea></td>
</tr>
</table>
</fieldset>
</div>
<div class="cl-left"></div>
</div>
<div class="buttons">
<div class="t2"><input type="submit" value="Vaata kaardi eelvaadet" name="button[nahled]" class="unbutt1"></div>
</div>
<input type="hidden" name="id" value="<?php echo $ap['card']['id']; ?>">
<input type="hidden" name="action" value="itemadd">
</form>
<?php
$k=floor(($i-1)/3);
if ($k<2) echo '<div class="spacer-'.(2-$k).'"></div>';
?>
Upvotes: 0
Views: 3500
Reputation: 101
change this line
echo '<input id="'.$row['id'].'" type="radio" name="id" class="detail">'.$_l['detailtitle'].'</input>';
to
echo '<input id="'.$row['id'].'" type="radio" value="'.$row['id'].'" name="id" class="detail">'.$_l['detailtitle'].'</input>';
when your form submit,you will get the selected id as $_POST['id']
Upvotes: 0
Reputation: 17481
When I select one radio button I need its id to be set to
$_POST['id']
variable
You are changing the value, not the id, to change the id use:
onclick="document.getElementById("id").id="<?php echo $row['id'] ?>";
Anyway, I think you should use the name
attribute for this.
Upvotes: 0
Reputation: 9884
The value of the selected radio button gets posted, and the name
is used to group radio buttons together. (Only one radio button with the specified name
can be selected.) Instead of trying to post the value of the id
attribute you should add a value
attribute and set that. You could set it to the same value as the id
attribute, or remove the id
attribute completely.
Upvotes: 1
Reputation: 2622
Its better if you can use a hidden field in the form and update its value with the id of the selected radio button and when the form will be posted you can get its value and more over this after once you changed the value of the hidden field you can access it with javascript also with id attibute and can get it in the post by name attribute
add a hidden field named
<input type="hidden" id="checked_radio" name="checked_radio" value="" />
and then change the value with javascript on click of button
<input type="radio" id="foo" name="foo" onclick="document.getElementById('checked_radio').value=this.value;" />
Upvotes: 1