0

I have a HTML form that has a selection drop down. I would like to populate the drop down from my MySQL DB. The HTML form already has some java scripts to handle datepicker, and current date on one of the fields in the form.

I can code the form for the selection manually, but would like it to come from MySQL. the php I am using I put in it's own file name.php to make sure it worked correctly. I added some echo statements in the php to build the form, and the selection, and then populate the selection, and close it. this worked just fine as a standalone php.

When I append the php in the form under the selection definition, it does not populate the selection at all, it is left blank/empty. If I add some echo statments to output info in the format of a selection option, the first echo never shows up, and the subsequent echos show up, but any variable show up as the variable name.

<form id="form1" name="form1" method="post" action="/db_log.php">
Field:<select name="id">

<?php

    require_once dirname(__FILE__) . '/db_connect.php';
$db = new DB_CONNECT();

$query="SELECT id, name FROM file order by name";
$result=mysql_query($query);

while ($row = mysql_fetch_array($result)) {
  $id = $row['id'];
  $name =$row['name'];
  echo "<option value=\"$id\"> $name </option>";
  }
?>

</select><br />
</form>

the above yealds an empty select dropdown. But this works great as a PHP file.

<?php
echo "<form id=\"form1\" name=\"form1\" method=\"post\" action=\"/db_log.php\">";
    echo "Select:<select name=\"id\">";
    require_once dirname(__FILE__) . '/db_connect.php';
$db = new DB_CONNECT();

$result=mysql_query("SELECT id, name FROM file order by name");

while ($row = mysql_fetch_array($result)) {
  $id = $row['id'];
  $name =$row['name'];
  echo "<option value=\"$id\"> $name </option>";
  }

    echo "</select><br />";
    echo "</form>";
?>

If I add a echo to the HTML form one like a test echo before any other echo, it is ignored.

<form id="form1" name="form1" method="post" action="/db_log.php">
Field:<select name="id">

<?php
  echo "<option value=\"1\"> Test-name </option>";

    require_once dirname(__FILE__) . '/db_connect.php';
$db = new DB_CONNECT();

$query="SELECT id, name FROM file order by name";
$result=mysql_query($query);

while ($row = mysql_fetch_array($result)) {
  $id = $row['id'];
  $name =$row['name'];
  echo "<option value=\"$id\"> $name </option>";
  }
?>

</select><br />
</form>

the only thing that shows up from this is the option to select $name

3
  • This is a duplicate... stackoverflow.com/questions/12558918/… Commented Sep 24, 2012 at 14:57
  • Is there a trick to adding PHP to a HTML. I just created a PHP that did the full form, and selection, and the submit button. It works as a php file. I then created a blank html file and pasted the php in the body of the html, file, and I get something that looks like this as a result: Commented Sep 24, 2012 at 15:18
  • "; echo "Airplane: "; echo ""; echo ""; ?> Commented Sep 24, 2012 at 15:19

2 Answers 2

0

Based on this question and the previous questions and the comments you've gotten and made I'm going to guess that you're trying to run php from a .html file. You need to save your code (including the html) in a .php file for it to work.

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

2 Comments

So any time I insert php code in html, the file can no longer be of type html, it has to be of type php? Just re saving the file worked.
Just get in the habit of calling everything .php. If it doesn't have <$php in it, PHP will just pass it through as is. Or you could change your webserver configuration to use PHP for .html.
0

A have a combo box that feeds from my mysql database.

I goes like this:

 <tr>
                    <td>Emergency service</td>
                    <td><?php 
                            $nombreEmergencia;
                            $idEmergencia;
                            $sql4 = "SELECT  `idEmergencias`, `nombre` FROM  `emergencias` WHERE 1";
                            $iterador = $conn->consulta($sql4);

                            echo '<select name="idEmergencia" id="idEmergencia" style="width:228px">
                            <option value="-1" selected>(please select:)</option>';
                            while($iterador->hayElemento()){
                                    $nombreEmergencia = $iterador->getElemento("nombre");
                                    $idEmergencia = $iterador->getElemento("idEmergencias");
                                    echo '<option value="'.$idEmergencia.'">'.utf8_encode($nombreEmergencia).'</option>';
                            }
                            echo '</select>'; ?></td>
                  </tr>

as you can see you have to use an "iterator" variable with a function that checks the database for more elements and brings 'em.

That auxiliary function looks like this:

<?php
class Iterador{
    private $ultimoDato;
    private $id;

    public function __construct($id){
        $this->id = $id;
    }

    public function hayElemento(){
        $dato = mysql_fetch_array($this->id);
        $this->ultimoDato = $dato;
        return !empty($dato);
    }

    public function getElemento($columnas){
        return $this->ultimoDato[$columnas];
    }

    public function inicializar(){
        $dato = mysql_fetch_array($this->id);
        $this->ultimoDato = $dato;
    }

    public function hay(){
        return !empty($this->ultimoDato);
    }

    public function siguiente(){
        $dato=mysql_fetch_array($this->id);
        $this->ultimoDato=$dato;
    }


}
?>

hope this helps!!

Comments

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.