通过先前的选择确定下一个选择菜单

时间:2016-02-21 05:54:32

标签: php html post drop-down-menu

我有3个选择下拉菜单,当我选择第一个,第二个显示,第二个选择时,第三个显示,使用if(isset($_post[first_one])),第三个使用{ {1}}

SQL:

if(isset($_post[second_one]))

PHP / HTML:

$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM sp_meeting_log ";
$result1 = $conn->query($sql);

1 个答案:

答案 0 :(得分:1)

基本上这就是这个想法。您希望第一页只从第二页获取并将结果发送回第一页到正确的位置:

<强> page1.php中

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<form>
    <label>Org
        <select id="org" name="org">
            <option value="1">One</option>
            <option value="2">Two</option>
        </select>
    </label>
    <!-- This is where the forum html will drop into after ajax runs -->
    <div id="forum"></div>
    <!-- This is where the user html will drop into after ajax runs -->
    <div id="user"></div>
</form>
<script type="text/javascript">
    $(document).ready(function() {
        // On change of a select menu
        $(this).on('change','select',function(e){
            // Assign selection
            var thisSelect  =   $(this);
            // Get the id name, this will tell page two
            //  what it's receiving
            var sendType    =   thisSelect.attr('id');
            // Get the actual value of the selection 
            var sendVal     =   thisSelect.val();
            // Create essentially a POST
            var sendData    =   { field: sendType, value: sendVal };
            $.ajax({
                // Send to page 2
                url : '/page2.php',
                // Use post method
                type: "POST",
                // Use post data from above
                data : sendData,
                // This is what will run on success
                success:function(response){
                    // Parse the json coming back for placement
                    var jSon    =   JSON.parse(response);
                    // Save the correct html into the correct drop spot
                    $('#'+jSon.type).html(jSon.html);
                },
                error: function(response){
                    console.log(response);
                }
            });
        });
    });
</script>

<强>使page2.php

if(!empty($_POST)) {
    $data   =   '';
    ob_start();
    if(isset($_POST['field'])) {
        if($_POST['field'] == 'org') {
            $type   =   'forum';
?>
    <label>Forum
        <select id="forum" name="forum">
            <option value="1">One</option>
            <option value="2">Two</option>
        </select>
    </label>
<?php
    }
    elseif($_POST['field'] == 'forum') {
        $type   =   'user';
?>
    <label>user
        <select id="user" name="user">
            <option value="1">One</option>
            <option value="2">Two</option>
        </select>
    </label>
<?php   }
        $data   =   ob_get_contents();
        ob_end_clean();

        die(json_encode(array('type'=>$type,'html'=>$data)));
    }

    die(json_encode(array('type'=>'error','html'=>false)));
}