Ok guys this is driving me nuts. Maybe its the monday morning but I just can't get this right:
<form id="form1" name="form1" method="post" action="choice.php">
<table border="1px">
<tr>
<?php
while ($row = $result->fetch_assoc()) {
unset($id);
$id = $row['id'];
echo '<td><img src="images/smiley.jpg" alt="Chose me"><br><input type="radio" name="choice" value='.$id <?php if ( $choice == $id ) {echo "checked";} ?>.' >Chose me</td>';
}
?>
</tr>
</table>
<br class="clear" />
<input type="submit" value="Submit">
<br class="clear" />
</form>
</body>
The problem is with this:
echo '<td><img src="images/smiley.jpg" alt="Chose me"><br><input type="radio" name="choice" value='.$id <?php if ( $choice == $id ) {echo "checked";} ?>.' >Chose me</td>';
I have read these (but still can't get the syntax right):
I want to update a value of radio button in database (mysql) using php
php update radio button state in db
I think that the problem is with single and double quotes. Tried changing them all around and still get get various Parse errors: for unexpected ? or > and so on
Even tried to move this part
<?php if ( $choice == $id ) {echo "checked";} ?>
to a separate line, and managed to get a valid php file that way, but when I copy it back to the echo statement it always screws up.
Pretty please tell me the right way.
is this line correct $id <?php if ( $choice == $id ) {echo "checked";} ?>
?? you're trying to put the checked attribute inside the value? or what you mean is this
$checked = $choice == $id ? "checked" : null;
echo "<td><img src='images/smiley.jpg alt='Chose me'><br><input type='radio name='choice value='{$id}' {$checked}>Chose me</td>";
note that i alter the use of single quote to double quote so that you can use variables in side double quotes
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.