2

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

1
  • In other words, it works in browsers other than Firefox? Sometimes you don't need to be too specific ;) Commented May 5, 2010 at 2:30

5 Answers 5

4

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>
Sign up to request clarification or add additional context in comments.

Comments

3
<html>
<title>
demo </title>
<body>
<?
// Load field datas into List box
$cn=mysql_connect("localhost",root) or die("Note: " . mysql_error());
echo "Conn ok<br>";
$res=mysql_select_db("test",$cn) or die("Note: " . mysql_error());
echo " Database opened<br>";
$q="insert into usha2 (`sno`, `city name`) values (NULL, 'Nagercoil');";
//$q="INSERT into `test`.`usha2` (`sno`, `city name`) VALUES (NULL, 'Delhi');";
$res=mysql_query($q) or die("Note: " . mysql_error());
echo " record inserted<br>";
$res=mysql_query("select * from usha2;") or die("Note: " . mysql_error());
echo " qry executed<br>";
?>
<select name="pno" size=1>
<?
while($ri = mysql_fetch_array($res))
{
echo "<option value=" .$ri['city name'] . ">" . $ri['city name'] . "</option>";
}
echo "</select> "
?>
</body>

Comments

1

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>

Comments

0

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

Comments

0

When you pull the data from your database you should use the following format as the format you are currently using is being phase out as MySqli advances

    $religion = $db->query("SELECT Religion FROM TableName ORDER BY RID");

$db is a connection to the database created as such

    $db = new Mysqli($server, $username, $password, $database);

Just set the variable to the correct value.

1 Comment

The point is correct but is not an answer to the question: why does the code crash firefox?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.