如何根据数据库中的值为下拉菜单生成选择?

时间:2015-01-07 04:08:18

标签: php jquery forms drop-down-menu mysqli

这是我正在努力实现的图片

http://oi60.tinypic.com/b9gf20.jpg

这是我所有的代码......

PHP文件:contact_form.php

<?php

define('DB_NAME', 'xxx');
define('DB_USER', 'xxx');
define('DB_PASSWORD', 'xxx');
define('DB_HOST', 'xxx');

$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);

if(!$connection){
    die('Database connection failed: ' . mysqli_connect_error());
}

$db_selected = mysqli_select_db($connection, DB_NAME);

if(!$db_selected){
    die('Can\'t use ' .DB_NAME . ' : ' . mysqli_connect_error());
}

echo 'Connected successfully';

if (isset($_POST['itemname'])){
    $itm = $_POST['itemname'];
}else{
    $itm = '';
}    

if($_POST['mile']){
    $mi = $_POST['mile'];
}else{
    echo "Miles not received";
    exit;
}

if($_POST['email']){
    $email = $_POST['email'];
}else{
    echo "email not received";
    exit;
}

$sql = "INSERT INTO seguin_orders (itemname, mile, email) VALUES ('$itm', '$mi', '$email')";
if (!mysqli_query($connection, $sql)){
    die('Error: ' . mysqli_connect_error($connection));
}

CONACT FORM:formz.php

<html>
<header>



</header>

<body>

<form action="/demoform/contact_form.php" class="well" id="contactForm" method="post" name="sendMsg" novalidate="">

<big>LOAD PAST ORDERS:</big>
<select id="extrafield1" name="extrafield1">
<option value="">Please select...</option>
</select>

</br>

<input type="text" required id="mile" name="mile" placeholder="Miles"/>

</br>

<input id="email" name="email" placeholder="Email" required="" type="text" value="demo@gmail.com" readonly="readonly"/>

</br>

<input id="name" name="itemname" placeholder="ITEM NAME 1" required="" type="text" />

</br>

<input type="reset" value="Reset" />


<button type="submit" value="Submit">Submit</button>


</form>


</body>


</html>

非常感谢和感谢任何时候并感谢你们的帮助。

2 个答案:

答案 0 :(得分:0)

当您单击select时,然后将ajax请求发送到另一个php脚本以选择有关所需列的数据库;然后更改它;如下所示:

 <input name="email" />
 <select onclick="ajaxFunc();" name="sel">
 </select>
 <script>
 function ajaxFunc(){
 var mail = $("input[name='email']").val();
 $.post('anotherFile.php',{mail:mail},function(e){
 $("select[name='sel']").append(e);
 });
 }
 </script>

答案 1 :(得分:0)

<form>
    <?php 
        while ($row = mysqli_fetch_assoc($result)) 
        {
         echo '<option value =" ' . $row['column'] . ' ">' . $row['column'] . '</option>';
        };
    ?>
 </form>