Ryan Ryan - 3 months ago 17
HTML Question

Radio button check on load from mysql

Hi guys i have a table/form I'm making and in one of the

<TD>
.

I'm making a button to ban or unban a user but i would like it to auto check based on whats already in the DB using MySql.

Here is what i currently have but its not auto populating:

echo "<td>" . '<form method="post" action=""> <input type="hidden" name="id" value=' . $ID . '> Ban:<input type="radio" value="1" name="unban" onclick="javascript: submit()" if({$row[\'Banned\']}==1 {echo "checked"; }/><br> Unban:<input type="radio" value="0" name="name" onclick="javascript: submit()" if({$row[\'Banned\']}=="0 {echo "checked"; }/><br /> </td>';


Thanks in advanced.

EDIT: Here is the complete code minus the DB info:

$LINK = mysql_connect($DB_HOST, $DB_USER, $DB_PASS) or die(mysql_error()); // Connect to database server(localhost) with username and password.
mysql_select_db($DB_DTBS) or die(mysql_error()); // Select registration database.

$search = mysql_query('SELECT * FROM users') or die(mysql_error());

$match = mysql_num_rows($search);


if($match > 0){
echo "Users";
echo "<table border='1'>
<tr>
<th>UserName</th>
<th>Email</th>
<th>Access Level</th>
<th>Ban</th>
<th>Ban Reason</th>

</tr>";

while($row = mysql_fetch_array($search)) {
echo "<tr>";
$ID = $row['AccountID'];
# echo "<td>" . $row['AccountID'] . "</td>";
echo "<td>" . $row['Username'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "<td>" . '<form method="post" action=""><select onchange="this.form.submit()" name="access"><option value="0">Standard</option><option value="300">VIP</option><option value="500">Moderator</option><option value="600">Admin</option><option value="700">OP</option></select></td>';
#echo "<td>" . $row['Banned'] . "</td>";
echo "<td>" .
'<form method="post" action=""> <input type="hidden" name="id" value=' . $ID . '>
Ban:<input type="radio" value="1" name="unban" onclick="javascript: submit()" '. (($row['Banned'] == "1") ? 'checked=checked"' : '').' /><br>
Unban:<input type="radio" value="0" name="name" onclick="javascript: submit()" '. (($row['Banned'] == "0") ? 'checked=checked"' : '').' /><br /> </form></td>';
#the above now pulls code thanks to Fred and everyone els
#echo "<td>" . '<input type="text" name="reason" value=""> </form> </td>';
echo "<td>" . $row['Banned_Reason'] . "</td>";
#echo "<td>" . $row['LastLogin'] . "</td>";
echo "</tr>";
}

echo "</table>";



} else {
echo("Oops Something Went wrong");
}


?>


*note the above is already in php tags.

Answer

I strongly suggest rewriting this approach, but if you want some logic inside that echo, you cannot include them inside the string and expect them to be interpolated as logic since they will be considered as strings. You can utilize a ternary in this case:

echo '
    <td>
        <form method="post" action="">
            <input type="hidden" name="id" value=' . $ID . ' />
            Ban: <input type="radio" value="1" name="unban" onclick="javascript: submit()" ' . (($row['Banned'] == 1) ? 'checked=checked' : '') . ' /> <br/> 
            Unban: <input type="radio" value="0" name="name" onclick="javascript: submit()" ' . (($row['Banned'] == 0) ? 'checked=checked' : '') . ' /> <br/>
        </form>
    </td>
';