tags:

views:

238

answers:

3

Please help, I'm having trouble loading mysql data on the combo box. The data that I'm loading is 1 column from a table. Here is my current code, and it crashed firefox for some reason:

<td colspan=”2″>Religion</TD>
<td>
<select name="REL" onClick="submitCboSemester();">
<?php
$query_disp="SELECT * FROM Religion ORDER BY RID";
$result_disp = mysql_query($query_disp, $conn);
while($query_data = mysql_fetch_array($result_disp))
{
?>
<option value="<? echo $query_data["RID"]; ?>"<?php if ($query_data["RID"]==$_POST['REL']) {?>selected<? } ?>><? echo $query_data["RELIGION"]; ?></option>
<? } ?>
</select>
</td>

The column is RELIGION and it ID is RID How do I populate the combo box with all the data in the column RELIGION

+2  A: 

Chances are, you don't have short_open_tag enabled in php.ini, this is probably what was causing your code to fail.

Also, you should output selected="selected", not just selected

Try this:

<td colspan=”2″>Religion</TD>
<td>
<select name="REL" onClick="submitCboSemester();">
<?php
$query_disp="SELECT * FROM Religion ORDER BY RID";
$result_disp = mysql_query($query_disp, $conn);
while($query_data = mysql_fetch_array($result_disp))
{
?>
<option value="<?php echo $query_data["RID"]; ?>"<?php if ($query_data["RID"]==$_POST['REL']) {?> selected="selected"<? } ?>><?php echo $query_data["RELIGION"]; ?></option>
<? } ?>
</select>
</td>
Jacob Relkin
A: 

Looks like there is no space between the end of your value and the selected.

Josh Pinter
A: 

Extract the data before displaying it !

And also, use the PHP Alternative Syntax when mixing PHP & HTML :

<?php
$query_disp="SELECT * FROM Religion ORDER BY RID";
$result_disp = mysql_query($query_disp, $conn);
$options = array();
while ($query_data = mysql_fetch_array($result_disp)) {
    $options[$query_data["RID"]] = $query_data["RELIGION"];
}
?>

<select name="REL" onClick="submitCboSemester();">
<?php foreach ($options as $key => $value) : ?>
    <?php $selected = ($key == $_POST['REL']) ? 'selected="selected"' : ''; ?>
    <option value="<?php echo $key ?>" <?php echo $selected ?>>
    <?php echo $value ?>
    </option>
<?php endforeach; ?>
</select>
mexique1