下拉列表从数据库php javascript加载数据

时间:2014-07-02 12:02:30

标签: javascript php html

加载的数据未显示在下拉列表中

请给我一个解决方案

<html>
<?php

$con = mysqli_connect("localhost","root","","pestpack");

if (mysqli_connect_errno())
  {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
mysqli_select_db($con,"pestpack");

$query1 = "SELECT teamname FROM team";
if($rs1 = mysqli_query($con,$query1))
?>
<select>
<option>select a team</option>
<?php
while($row1 = mysqli_fetch_array($rs1))
{
  echo '<option value="'.$row['teamname'].'">' . $row['teamname'] . '</option>';      
}
?>
</select>
</html>

3 个答案:

答案 0 :(得分:1)

像这样填充,

        $con = mysqli_connect("localhost","root","","pestpack");

        if (mysqli_connect_errno())
          {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
          }
        // mysqli_select_db($con,"pestpack"); No need to select it again, already mentioned in connection

        $query1 = "SELECT teamname FROM team";
        $rs1 = mysql_fetch_assoc($con,$query1);

        echo '<select>';
        echo '<option>select a team</option>';

        foreach($rs1 as $row) {    
          echo '<option value="'.$row['teamname'].'">' . $row['teamname'] . '</option>';
        }
        echo '</select>';

这用于在另一个表中插入数据。

<form method="POST" action="team_insert.php"> <!-- Also define it using `echo` -->
    <!-- This is generated html select tag-->
    <select name="team">  <!-- Define name attribute -->
       <option value="team1">team1</option>
       <option value="team2">team2</option>
       <option value="team3">team3</option>
       <option value="team4">team4</option>
    </select>
    <input type="submit" name="insert" value="Insert" />
</form>

现在在team_insert.php中编写插入逻辑

  if((isset($_SERVER['REQUEST_METHOD'])) && 
     ($_SERVER['REQUEST_METHOD'] == 'POST') && $_POST['team']) {
     $selected_team = $_POST['team'];
     // $selected_team variable grap your dropdown value.
     // Then write a query logic to insert
  }

答案 1 :(得分:0)

           <html>
            <?php

            $con = mysqli_connect("localhost","root","","pestpack");

            if (mysqli_connect_errno())
              {
                echo "Failed to connect to MySQL: " . mysqli_connect_error();
              }
            mysqli_select_db($con,"pestpack");

            $query1 = "SELECT teamname FROM team";
            $rs1 = mysql_fetch_array($con,$query1);
            ?>

            <select>
            <option>select a team</option>
            <?php

            foreach ($rs1 as $row) {    
              echo '<option value=\"{$row['teamname']}\">{$row['teamname']}</option>';
            }
            ?>
            </select>
            </html> 

答案 2 :(得分:0)

希望这会对某人有所帮助!!!

  $conn = mysql_connect("","","");
                            mysql_select_db("databaseName",$conn);
                    $sc = mysql_real_escape_string($_SESSION['username']);
                    $result = mysql_query("SELECT Client_table.Name, Client_table.Client_Id 
                            FROM Client_table, user 
                            WHERE user.username = '$sc' 
                            AND Client_table.Branch = user.area 
                            Order by Name ASC");
                    //$result = mysql_fetch_assoc($conn, $query1);

                    echo '<select class="element select large" id="element_17" name="element_17">';
                    echo '<option> Select Company Name</option>';

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