0

im trying to put a drop down list generated from mysql in html page , the thing is this code work fine in a php page but it doesnt in html page , I've put it in a php tags yet nothing was shown , I would appreciate any help here is my code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <title>Add Qustions</title>
 </head>
 <body>
  <form action="saveQ.php" method="post" >

  <br />
  <?php   $connectdb = mysql_connect('localhost','root','sara') or die ("Not Connect");
  if (!$connectdb)
  {
      die('Could not connect :'. mysql_errno());
  }
  $selestdb  = mysql_select_db('iexa', $connectdb) or die ("not selected database");
   $qu = mysql_query("SELECT ID,Name FROM Course ORDER BY ID asc") or die ("mysql error");
    echo "Choose the course that you want to create the test for : <br /> ";
    echo " <select name='courseID' >Course</option>";
    echo "<option value=0>Course </option>";
     $curvalue=2;
    while  ($row = mysql_fetch_assoc($qu)){
    echo '<option value="' .$row[ID].'">' .$row[ID].' ' .$row[Name].'</option>';
    $curvalue = $curvalue+1;
     }
     echo "</select> "; ?>
     <br />
      /// some other unrelated code here 
    <input type="submit" value="Save Question" />
    </form>

    </body>
    </html>
2
  • 4
    Not sure what you mean by "this code work fine in a php page but it doesnt in html page". With the usual default settings for most servers, any script that contains PHP code must have a valid PHP extension, like .php or .phtml. Commented Apr 10, 2012 at 20:12
  • PHP does not work in HTML, but HTML works in PHP. Commented Apr 10, 2012 at 20:13

3 Answers 3

1

I think this script is what you wanted to do:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Add Qustions</title>
</head>
<body>
    <form action="saveQ.php" method="post">
        <br />
        <?php  
            $connectdb = mysql_connect('localhost','root','sara') or die ("Not Connect");
            if (!$connectdb) die('Could not connect :'. mysql_errno());

            echo "          Choose the course that you want to create the test for: <br />
            <select name=\"courseID\">
                <option value=\"0\">Course </option>\n";

            $selestdb  = mysql_select_db('iexa', $connectdb) or die ("not selected database");
            $qu = mysql_query("SELECT ID,Name FROM Course ORDER BY ID asc") or die ("mysql error");
            while ($row = mysql_fetch_assoc($qu))
                echo "              <option value=\"{$row["ID"]}\">{$row["ID"]} {$row["Name"]}</option>\n";

            echo "          </select> ";
        ?>
        <br />
        <!-- some other unrelated code here -->
        <input type="submit" value="Save Question" />
    </form>
</body>
</html>

Your script's name should end with a PHP extension, like .php or .phtml.

Sign up to request clarification or add additional context in comments.

Comments

1

I personally don't like to echo out html in PHP so i use this method, in any case your issue is html based and not php based...

Example PHP Model to Query DB :

<?php
$selectdb  = mysql_select_db('iexa', $connectdb) or die ("not selected database");
$qu = mysql_query("SELECT ID,Name FROM Course ORDER BY ID asc") or die ("mysql error");
?>

Example View File to Handle the DOM and html.

<p>Choose the course that you want to create the test for:</p><br />
<select name="courseID">
    <option value="0">0 Course</option>
    <option value="1">1 Course</option>
  <?php while( $row = mysql_fetch_assoc($qu) ): ?>
    <option value="<?= $row['ID']; ?>"><?php echo $row['ID'].' '.$row['Name']; ?></option>
  <?php endwhile; ?>
</select>

In short, you have an error here: echo " <select name='courseID' >Course</option>";.

It should be: echo " <select name='courseID' >Course</select>";


Also, you dont need to set $curvalue = $curvalue+1; You can simply do $curvalue++; IE:

<?php
  while  ($row = mysql_fetch_assoc($qu)){
  <select name="courseID">
  <option value="<?= $row['ID']; ?>"><?php echo $row['ID'].' '.$row['Name']; ?></option>
  </select>
   $curvalue++;
 }

Also note that its considered best practice to not mix DOM with Server side code. IE you should put the while statement and select boxes in a seperate php view file that iterates through things, rather than using echo. This way there is less server processing and more client side processing.

5 Comments

the example code has been udpated, its still ideal to remove the mysql_fetch_assoc from the "view file" and pass in an array to a foreach loop instead of using while.
In fact, it doesn't look like $curvalue is used at all. You can completely remove it. Also, granted, it is good to use a code structure, like MVC, but it seems kinda outside the scope of this question IMO.
Hold on, I just looked at your examples better. It looks like you are only fetching one row from the database. One call to mysql_fetch_assoc fetches one row from the database and stores it in an associative array, with each entry representing one column from the singluar row. So the point of the while-loop is to continually fetch rows until there are no more rows to fetch.
So your foreach loop is looping through the columns of one row.
yeah, confusing ActiveRecord from CI with old school base php stuf.. i reupdated it. I do most of my db stuff in CodeIgniter or FuelPHP which iterates through for you and provides an array of all rows returned.. Forgot mysql_fetch_assoc() doesnt do that out of the box, in which case your previous comment stands true $curvalue is useless.
0

Maybe because you are doing it like this:

echo " <select name='courseID' >Course</option>";

Change it to:

echo "<select name='courseID'>";

4 Comments

That seems wrong as well...did you mean <select name='courseID'><option>Course</option>?
@Travesty3 In case you skipped the code, the OP is having and echo <option> in just the next statement.
OK, that's fine, but what is the point of Course in your example?
Oh! Totally didn't see it. I guess it's good night time now. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.