根据选择值添加/显示动态表单字段

时间:2014-03-11 05:26:21

标签: javascript php jquery

我正在使用Jquery Mobile创建一个弹出窗体,显示用户可以选择的select语句。我使用ajax使select语句动态化。我已经获得了要显示的数据并创建了一个新的select语句。它似乎没有正确格式化。

Picture of The Form with before and After

Popup Form Code

<?php
$q = intval($_GET['q']);

include_once('session.php');
include_once('dbConnect.php');

$sql="SELECT Enrollment.c_id, Enrollment.s_id, users.f_name, users.l_name
FROM Enrollment
INNER JOIN users ON Enrollment.s_id = users.s_id
WHERE c_id=$q";

$result = mysqli_query($dbc, $sql);

echo "<label for='selectuser' class='select'>Select user:</label>";
echo "<select name='selectuser' id='selectuser' data-native-menu='false'>";
echo "<option>Choose Users:</option>";
echo "<option value='instructor'>All Instructors</option>";
echo "<option value='students'>All Students</option>";

while($row = mysqli_fetch_array($result))
  {
        $s_id = $row['s_id'];
        $f_name = $row['f_name'];
        $l_name = $row['l_name'];
        echo "<option value='$s_id'>$f_name $l_name</option>";
  }
echo "</select>";
?>

2 个答案:

答案 0 :(得分:10)

更简单的方法是:

HTML

首先,从头开始将所有选择框放在html中:

<select name="selectclass" id="selectclass" data-native-menu="false">
   <option value='default'>Select Class:</option>
   <?php echo $allClassOptions; ?>
</select>
<select name="selectuser" id="selectuser" data-native-menu="false">
   <option value='default'>Select User:</option>
   <?php echo $allUsers; ?>
</select>

优良作法是为没有javascript(优雅降级)的用户提供替代方案。

的Javascript

然后,在您的javascript文件中,隐藏应该在开始时隐藏的输入字段。将事件处理程序绑定到第一个选择字段的change事件,并使用Ajax调用填充第二个选择字段的选项字段。

var selectElement = $("#selectuser");
selectElement.hide();

$("#selectclass").on("change", function(){
    var selectedClass = this.value;

    if(selectedClass != "default"){
       selectElement.show();

       $.ajax({
           type: "POST",
           url: "getdatabaseresults.php",
           data: {"class": selectedClass },
           success: function(result){
                //remove old options
                selectElement.empty();

                //add new options
                selectElement.append(result);
           }
       });
    };
});

PHP

在PHP文件中,处理Ajax调用并返回想要的结果:

<?php

if(isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) == "xmlhttprequest"){
    //this is an Ajax call!

    $selectedClass = $_POST["class"];
    $options = "<option value='default'>Select User:</option>";

    //do whatever you want with the data
    //database calls and whatnot
    $stmt = mysqli_prepare($dbc, "SELECT * FROM users WHERE c_id = ?");
    mysqli_stmt_bind_param($stmt, "s", $selectedClass);
    mysqli_stmt_execute($stmt);
    mysqli_stmt_bind_result($stmt, $row);
    while(mysqli_stmt_fetch($stmt)) {
        $user = $row['username'];
        $options.= "<option value='$user'>$user</option>";
    }
    mysqli_stmt_close($stmt);

    echo $options;
}
?>

这个php文件可以扩展(例如switch()),因此它可以用于不同的ajax调用。

注意:有很多不同的方法可以实现这一点,这只是一个例子。

答案 1 :(得分:3)

我认为问题在于您在循环后没有关闭select标记。此外,建议最后只进行一次写操作。就这样:

<?php
$q = intval($_GET['q']);

include_once('session.php');
include_once('dbConnect.php');

$sql="SELECT Enrollment.c_id, Enrollment.s_id, users.f_name, users.l_name
FROM Enrollment
INNER JOIN users ON Enrollment.s_id = users.s_id
WHERE c_id=$q";

$result = mysqli_query($dbc, $sql);

$text = "<label for='selectuser' class='select'>Select user:</label>";
$text .= "<select name='selectuser' id='selectuser' data-native-menu='false'>";
$text .= "<option>Choose Users:</option>";
$text .= "<option value='instructor'>All Instructors</option>";
$text .= "<option value='students'>All Students</option>";

while($row = mysqli_fetch_array($result))
{
    $s_id = $row['s_id'];
    $f_name = $row['f_name'];
    $l_name = $row['l_name'];
    $text .= "<option value='$s_id'>$f_name $l_name</option>";
}
$text .= "</select>"
echo $text
?>