Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:从可靠的下拉菜单中回显所选项目的值_Php - Fatal编程技术网

PHP:从可靠的下拉菜单中回显所选项目的值

PHP:从可靠的下拉菜单中回显所选项目的值,php,Php,我目前正在使用一个可靠的下拉菜单,它在jQuery和PHP的帮助下运行。正在从MySQL数据库中提取这些值。是否有一个可靠的下拉菜单的选择值 HTML/PHP 您必须编写一个JavaScript函数,从第一个HTML选择字段中检索所选的值或选项。此函数通常会向当前页面写入新的URL路径,并添加一些相关的Get变量: <script type="text/javascript"> getSelectedOptionValue() { // create some variables t

我目前正在使用一个可靠的下拉菜单,它在jQuery和PHP的帮助下运行。正在从MySQL数据库中提取这些值。是否有一个可靠的下拉菜单的选择值

HTML/PHP


您必须编写一个JavaScript函数,从第一个HTML选择字段中检索所选的值或选项。此函数通常会向当前页面写入新的URL路径,并添加一些相关的Get变量:

<script type="text/javascript">
getSelectedOptionValue() {
// create some variables to store your know values such as URL path and document
var myPath = " put the URL path to the current document here ";
var currentPage = "currentPage.php";
// get the values of any necessary select fields
var carMake = document.getElementById("carMake").value;

// write out the final URL with the Get Method variables you want using concatnitation
var getMethodURL = myPath + currentPage + "?carMake='" + carMake + "'";

// function refreshes page using the function made URL
window.location.replace( getMethodURL );
}
</script>
由于第二个选择字段依赖于第一个字段,因此您必须假设用户将从第一个选项中进行选择。这意味着检索主选择字段值的函数必须运行以响应字段选择中的更改。比如说

<select name="carMake" id="carMake" onchange="getSelectedOptionValue();">
根据您设置数据库的方式,您可能希望选项标记的值或选项标记之间的字符串显示给用户……这取决于您是否记住,如果原始记录集尚未提取必要的信息来写入第二组选择选项标记,您可以如何重新查询信息

要使用php写出第二个select字段,只需重复第一个使用的while循环。这一次,使用一个变量将SQL语句替换为一个新语句,在该变量中,您使用get方法存储了从新URL检索到的值

<?php

// here I am using the more generic request method although you could use the get as well
$carMake = $_REQUEST['carMake'];

sql_secondSelectField = "SELECT * FROM tbl_carModels WHERE carMake = $carMake";

// Run new query and repeat similar while loop used to write your first select field ?>

您必须编写一个JavaScript函数,从第一个HTML选择字段中检索所选的值或选项。此函数通常会向当前页面写入新的URL路径,并添加一些相关的Get变量:

<script type="text/javascript">
getSelectedOptionValue() {
// create some variables to store your know values such as URL path and document
var myPath = " put the URL path to the current document here ";
var currentPage = "currentPage.php";
// get the values of any necessary select fields
var carMake = document.getElementById("carMake").value;

// write out the final URL with the Get Method variables you want using concatnitation
var getMethodURL = myPath + currentPage + "?carMake='" + carMake + "'";

// function refreshes page using the function made URL
window.location.replace( getMethodURL );
}
</script>
由于第二个选择字段依赖于第一个字段,因此您必须假设用户将从第一个选项中进行选择。这意味着检索主选择字段值的函数必须运行以响应字段选择中的更改。比如说

<select name="carMake" id="carMake" onchange="getSelectedOptionValue();">
根据您设置数据库的方式,您可能希望选项标记的值或选项标记之间的字符串显示给用户……这取决于您是否记住,如果原始记录集尚未提取必要的信息来写入第二组选择选项标记,您可以如何重新查询信息

要使用php写出第二个select字段,只需重复第一个使用的while循环。这一次,使用一个变量将SQL语句替换为一个新语句,在该变量中,您使用get方法存储了从新URL检索到的值

<?php

// here I am using the more generic request method although you could use the get as well
$carMake = $_REQUEST['carMake'];

sql_secondSelectField = "SELECT * FROM tbl_carModels WHERE carMake = $carMake";

// Run new query and repeat similar while loop used to write your first select field ?>
请添加jquery.js。 你的html代码

    <select name="gender" id="gender" class="update">
        <option value="">Select one</option>
        <?php if (!empty($list)) { ?>
            <?php foreach($list as $row) { ?>
                <option value="<?php echo $row['id']; ?>">
                    <?php echo $row['name']; ?>
                </option>
            <?php } ?>
        <?php } ?>
    </select>

    <select name="category" id="category" class="update" disabled="disabled">
        <option value="">----</option>
    </select>

    <select name="colour" id="colour" class="update" disabled="disabled">
        <option value="">----</option>
    </select>
        //jquery code for source list
    <script type="text/javascript">
        $(document).ready(function(){
            $('#gender').change(function() {
              if ($(this).val()!='') {
                $("#category").load("postfile.php",{gender_id: $(this).val()});
                $("#category").removeAttr('disabled');
              }
            });
            //code on change of sel_source
            $('#category').change(function() {
              if ($(this).val()!='') {
                $("#colour").load("postfile.php",{category_id: $(this).val()});
                $("#colour").removeAttr('disabled');
              }

            });
        });
    </script> 



//postfile.php
//your mysql connection other things goes here
//code for category
$objDb = new PDO('mysql:host=localhost;dbname=dbname', 'ur_username', 'ur_password');
if(isset($_REQUEST['gender_id']) && !empty($_REQUEST['gender_id'])) {

           $sql = "SELECT * FROM `categories` WHERE `master` = ?";
    $statement = $objDb->prepare($sql);
    $statement->execute(array($_REQUEST['gender_id']));
    $list = $statement->fetchAll(PDO::FETCH_ASSOC);

    if(!empty($list)) {
        $output = '<option value="">Select</option>';
        foreach($list as $row) {              
                $output .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
        }
    } else {
        $output = '<option value="">Select</option>';
    }
    echo $output;
} 
//code for color

if(isset($_REQUEST['category_id']) && !empty($_REQUEST['category_id'])) {
    $sql = "SELECT * FROM `categories` WHERE `master` = ?";
    $statement = $objDb->prepare($sql);
    $statement->execute(array($_REQUEST['category_id']));
    $list = $statement->fetchAll(PDO::FETCH_ASSOC);
     if(!empty($list)) {
        $output = '<option value="">Select</option>';
        foreach($list as $row) {          
                $output .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
        }
    } else {
        $output = '<option value="">Select</option>';
    }
    echo $output;
} 
希望这对您有所帮助。

请添加jquery.js。 你的html代码

    <select name="gender" id="gender" class="update">
        <option value="">Select one</option>
        <?php if (!empty($list)) { ?>
            <?php foreach($list as $row) { ?>
                <option value="<?php echo $row['id']; ?>">
                    <?php echo $row['name']; ?>
                </option>
            <?php } ?>
        <?php } ?>
    </select>

    <select name="category" id="category" class="update" disabled="disabled">
        <option value="">----</option>
    </select>

    <select name="colour" id="colour" class="update" disabled="disabled">
        <option value="">----</option>
    </select>
        //jquery code for source list
    <script type="text/javascript">
        $(document).ready(function(){
            $('#gender').change(function() {
              if ($(this).val()!='') {
                $("#category").load("postfile.php",{gender_id: $(this).val()});
                $("#category").removeAttr('disabled');
              }
            });
            //code on change of sel_source
            $('#category').change(function() {
              if ($(this).val()!='') {
                $("#colour").load("postfile.php",{category_id: $(this).val()});
                $("#colour").removeAttr('disabled');
              }

            });
        });
    </script> 



//postfile.php
//your mysql connection other things goes here
//code for category
$objDb = new PDO('mysql:host=localhost;dbname=dbname', 'ur_username', 'ur_password');
if(isset($_REQUEST['gender_id']) && !empty($_REQUEST['gender_id'])) {

           $sql = "SELECT * FROM `categories` WHERE `master` = ?";
    $statement = $objDb->prepare($sql);
    $statement->execute(array($_REQUEST['gender_id']));
    $list = $statement->fetchAll(PDO::FETCH_ASSOC);

    if(!empty($list)) {
        $output = '<option value="">Select</option>';
        foreach($list as $row) {              
                $output .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
        }
    } else {
        $output = '<option value="">Select</option>';
    }
    echo $output;
} 
//code for color

if(isset($_REQUEST['category_id']) && !empty($_REQUEST['category_id'])) {
    $sql = "SELECT * FROM `categories` WHERE `master` = ?";
    $statement = $objDb->prepare($sql);
    $statement->execute(array($_REQUEST['category_id']));
    $list = $statement->fetchAll(PDO::FETCH_ASSOC);
     if(!empty($list)) {
        $output = '<option value="">Select</option>';
        foreach($list as $row) {          
                $output .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
        }
    } else {
        $output = '<option value="">Select</option>';
    }
    echo $output;
} 

希望这能对您有所帮助。

请详细解释如何从可靠的下拉菜单中回显所选项目?@Adnan很抱歉,我已编辑了答案。很抱歉,但仍然不够清楚。是否希望PHP确定所选项目,以便在页面加载项目时已选中该项目?或者,您希望在用户选择某个内容后,它会转到PHP,然后回显到浏览器中吗?只需看看演示,我认为他想使用基于第一个下拉列表中所选项目的值更新第二个下拉列表。@krike,哦,真的吗?!如果这是他想要的,那么提出这个问题就没有意义了,演示已经有了代码,视频已经提供了解释。请进一步解释如何从可靠的下拉菜单中回显所选项目?@Adnan很抱歉不清楚,我已经编辑了答案。很抱歉,但仍然不够清楚。是否希望PHP确定所选项目,以便在页面加载项目时已选中该项目?或者,您希望在用户选择某个内容后,它会转到PHP,然后回显到浏览器中吗?只需看看演示,我认为他想使用基于第一个下拉列表中所选项目的值更新第二个下拉列表。@krike,哦,真的吗?!如果这是他想要的,那么提出这个问题就没有意义了,演示已经有了代码,视频已经提供了解释。如果不想离开页面,通过重新加载页面刷新get变量,也可以使用AJAX调用此函数,但这又增加了一些复杂性。您还可以在php while循环中添加一个if语句,通过将当前记录迭代与JavaScript函数创建的新URL中发布的get方法变量进行比较,将选定状态添加到选项标记中。这将保留用户在使用window.location方法重新加载页面之前选择的选项。如果不想离开页面以通过重新加载页面刷新get变量,也可以使用AJAX调用此函数,但这会增加另外几个步骤的复杂性。您还可以在php中添加if语句
ile循环,通过将当前记录迭代与JavaScript函数创建的新URL中发布的get方法变量进行比较,将所选状态添加到选项标记中。这将保留用户在使用window.location方法重新加载页面之前选择的选项。谢谢!我在找这个。虽然有一件事我看到您正在使用['gender_id']和['category_id'],但是MySQL表的设置不同,只有这个字段id、master_id、name。您可以在视频中看到:我正在将性别id和类别id从html传递到php,它们不是thd表字段。您必须在select查询中指定表字段。谢谢,我已经尽了一切可能。你能给我的emai发送一个工作源吗?我真的很感激,我的朋友。谢谢你!我在找这个。虽然有一件事我看到您正在使用['gender_id']和['category_id'],但是MySQL表的设置不同,只有这个字段id、master_id、name。您可以在视频中看到:我正在将性别id和类别id从html传递到php,它们不是thd表字段。您必须在select查询中指定表字段。谢谢,我已经尽了一切可能。你能给我的emai发送一个工作源吗?我真的很感激,我的朋友。