如何根据数据库中的值生成下拉菜单的选择?
How can I generate selections for a dropdown menu based on values in my database?
这是我正在努力实现的目标的照片
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));
}
联系方式: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>
非常感谢和感谢任何时间和帮助,谢谢。
当您单击 select 时,然后将 ajax 请求发送到另一个 php 脚本到 select 数据库关于您想要的列;然后更改它;像这样:
<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>
<form>
<?php
while ($row = mysqli_fetch_assoc($result))
{
echo '<option value =" ' . $row['column'] . ' ">' . $row['column'] . '</option>';
};
?>
</form>
这是我正在努力实现的目标的照片
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));
}
联系方式: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>
非常感谢和感谢任何时间和帮助,谢谢。
当您单击 select 时,然后将 ajax 请求发送到另一个 php 脚本到 select 数据库关于您想要的列;然后更改它;像这样:
<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>
<form>
<?php
while ($row = mysqli_fetch_assoc($result))
{
echo '<option value =" ' . $row['column'] . ' ">' . $row['column'] . '</option>';
};
?>
</form>