查看有关选择更改的客户信息

时间:2014-06-26 16:27:00

标签: php jquery ajax mysqli

我正在创建一个页面,允许管理员从下拉列表中选择一个用户,该列表从数据库中填充。选择此人后,将在页面上查看与该人相关的信息。我已经有一个select语句选择所有信息,下拉菜单正确填充。但是,我不确定如何在选择后让所选用户的信息显示在页面上。我是否需要执行完全不同的select语句和查询来检查选择了哪个客户?或者还有另一种方式吗?

customer.php

<div id="view_form" class="view">
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        <fieldset>
            <label for="viewCustomer">Select Customer</label>
            <?php
                echo "<select name='selectCust' id='selectCust'>";
                echo "<option></option>";
                while($row = mysqli_fetch_assoc($custResult)){
                    $name = "{$row['fName']} {$row['lName']}";
                    echo "<option>$name</option>";
                }
                echo "</select>";
            ?>
        </fieldset>
    </form>
</div>

viewUser.php

if(isset($search)){
    $select = "SELECT * FROM $cust WHERE acctNum='{$search}'";
    $result = mysqli_query($db, $select);
    if(mysqli_num_rows($result) > 0){
        if($row = mysqli_fetch_assoc($result)){
            $acct = "{$row['acctNum']}";
            echo $acct;
        }
    }
}

的script.js

$(document).ready(function(){
    function searchAjax(){
        var search = $('#selectCust').val();
        $.post('includes/viewUser.php', {searchUsers: search}, function(data){
            $('#view_form').append(data);
        })
    }       
    $('#selectCust').on('change', function(ev){
        ev.preventDefault();
        searchAjax();
    })
})

2 个答案:

答案 0 :(得分:1)

的search.php

    <script type="text/javascript "src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>

    <script type='text/javascript'>
    $(document).ready(function(){

        $(".dropdown-users").on("change",function(event){
        event.preventDefault();
        search_ajax_way();
        });

    });

    function search_ajax_way(){
        var search_this=$("dropdown-users").val();

        $.post("Ajaxsearch.php", {searchusers : search_this}, function(data){
            $(".results").html(data);   
        })
    }
    </script>

    <div id="view_form" class="view">
        <form method="post">
            <fieldset>
                <label for="viewCustomer">Select Customer</label>
                <?php
                    echo "<select class="dropdown-users">";
                    echo "<option></option>";
                    while($row = mysqli_fetch_assoc($custResult)){
                        $name = "{$row['fName']} {$row['lName']}";
                        $acct = $row['acctNum'];
                        echo "<option value="$acct">$name ($acct)</option>";
                    }
                    echo "</select>";
                ?>
            </fieldset>
        </form>
    </div>



    <label>Enter</label>
    <input type="text" name="search_query" id="search_query" placeholder="What You Are Looking For?" size="50"/>
    <input type="<span id="IL_AD1" class="IL_AD">submit</span>" <span id="IL_AD6" class="IL_AD">value</span>="Search" id="button_find" />

    <div class="results"></div>

// <强> ****************************************** ************************************************** *************** ************** / /

Ajaxsearch.php

    <?php
    $con = mysqli_connect("localhost","my_user","my_password","my_db"); // Enter your information here
    $term = $_POST['searchusers']
    $term = mysqli_real_escape_string($con, $term);
    if($term == "")
        echo "Enter Something to search";
    else {
        $query = mysqli_query($con, "select * from USERDATEBASEHERE where ID = '{$term}' ");
        $string = '';

        if (mysqli_num_rows($query) > 0) {

            if (($row = mysqli_fetch_assoc($query)) !== false) {
               $string = "{$row['ID']}";
            }

        } else {
            $string = "This Person does not exist";
        }

            echo $string;
    }
    ?>

答案 1 :(得分:0)

<div id="view_form" class="view">
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        <fieldset>
            <label for="viewCustomer">Select Customer</label>
            <?php
                echo "<select name=\"somename\" onchange=\"this.form.submit();\">";
                echo "<option value=\"\">Select User</option>";
                while($row = mysqli_fetch_assoc($custResult)){
                    $name = "{$row['fName']} {$row['lName']}";
                    $acct = $row['acctNum'];
                    echo '<option value="'.$acct.'">$name ($acct)</option>';
                }
                echo "</select>";
            ?>
        </fieldset>
    </form>
</div>

选项必须有一些引用值,通过它可以检索所选用户的详细信息,只要未启动选项的值,该选项的默认值将是选项的标签。