dsdzz04802
2015-01-07 04:08
浏览 35

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

Here's a pic of what i'm trying to achieve

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

Heres all my code...

PHP FILE: 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>

Much thanks and appreciation for any time and help with this thanks.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongmei8460 2015-01-07 05:14
    已采纳
    <form>
        <?php 
            while ($row = mysqli_fetch_assoc($result)) 
            {
             echo '<option value =" ' . $row['column'] . ' ">' . $row['column'] . '</option>';
            };
        ?>
     </form>
    
    已采纳该答案
    打赏 评论
  • dstwfcz1377 2015-01-07 04:41

    When you click select ,then send a ajax request to another php script to select database about the columns you want;then change it;like this:

     <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>
    
    打赏 评论

相关推荐 更多相似问题