简体   繁体   中英

Fill drop down list on page load php

I have two input text fields where user has to specify the begin and end of the fly.

<input type="text" name="start" placeholder="Start destination">
<input type="text" name="end" placeholder="End destination">    

I would like to change that and give user to chose start and end destination from database.

 <select>
  <option value="$id">$name</option>
</select> 

I know how to get done if i read database and input values manually, but i know its posible if page loads and execute my SELECT QUERY.

So i have to create dropdown list and fill that with a values from database.

This dropdown list has to be filled when the page load.

Some idea for this ???

I am working with php.

Thank you in advance !!

EDIT : I get done this only with php.

<?php 

                        $db_host = "localhost";
                        $db_username = "root";
                        $db_password = "";
                        $db_name = "flights";

                        $conn = mysql_connect("$db_host","$db_username","$db_password") or die ("no conn");
                        @mysql_select_db("$db_name") or die ("no database");

                        if ($conn = true) {
                          // echo "";

                        }
                        //cyrilic
                        $sql = "SET NAMES 'utf8'";
                        mysql_query($sql);

                        //query for end
                        $sql="SELECT Distinct end from flights_table;";
                        $result=mysql_query($sql);



                        echo "<select name=\"city\">";
                        echo "<option>end destination</option>";

                        while ($row = mysql_fetch_array($result))
                        {
                            echo "<option value='".$row['end']."'>".$row['end']." </option>";

                        }
                        echo "</select>";


                         ?>

This php fires when page loads. Those select options i have putted in a form, and when form is submited, it fires php itself. I am getting selected options this way :

$startfly=$_POST['end'];

I am doing this for starting the flight :)

Thank you guys !

Try this :

At the top of page include your database connection file :

<?php
     require "connection.php";
?>

Then :

<?php
$selectStart = "Start : <select name='start'>";
$selectEnd = "End : <select name='end'>";
$query = mysql_query("SELECT * FROM someTable ORDER BY dateField ASC");
if(mysql_num_rows($query) > 0)
{
    while($row = mysql_fetch_assoc($query))
    {
        $selectStart .= "<option value='".$row['startItem']."'>".$row['startItemName']."</option>";
        $selectEnd .= "<option value='".$row['endItem']."'>".$row['endItemName']."</option>";
    }
}
$selectStart = "</select>";
$selectEnd = "</select>";
?>

In your HTML :

<form action='destinationPage.php' method='post'>
   <?php
      echo $selectStart;
      echo $selectEnd;
   ?>
   <input type='submit' value='Submit' />
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM