【问题标题】:jQuery $.post() Logging Me Out On Key DownjQuery $.post() 在按键按下时让我退出
【发布时间】:2015-09-25 17:04:30
【问题描述】:

我有一个动态下拉搜索栏,可以搜索我网页上表单内的数据库成员。要查看此网页,您必须先登录。当我在我的域上构建站点时,一切正常。但是,当我将文件转移到另一个域并使用相同的数据库对其进行配置时,一切正常,除了我以这种形式进行动态搜索。如果我在搜索中输入我的名字(有时是与工作不同的奇怪名字),一切正常,但如果我输入其他任何人,它似乎应该留在页面上,但它会注销我并重新加载顶部的登录表单包括我正在输入的表格在内的所有其他内容。我正在使用 jQuery .post() 使搜索动态化。我将在下面提供代码

index.php

  <script>
        // this is the jQuery function used to post to the search document on key up
        function searchUserQ(){
            var searchTxt = $("input[name='userSearch']").val();
            console.log(searchTxt);
            if (searchTxt != '') {

                $.post("includes/search.php", {searchVal:searchTxt},
                    function(output){
                        $("#userResults").html(output);
                    });
            }
        }
    </script>


  <h1 class="editUser">Edit User</h1>
  <form class="editUser" action="index.php" method="post">
    <h1>Search For Employee</h1>
    <input type="text" name="userSearch" id="userSearch" placeholder="Search For Employee By First Name" onkeyup="searchUserQ();" />
    <submit type="submit" />
    <div id="userResults">

    </div>
 </form>

搜索.php

<?php
    // Connect To Secure Login
    $cfgProgDir = '../phpSecurePages/';
    include($cfgProgDir . "secure.php");
    //These are the includes needed to make the php page run
    // this file connects to the database
    include("connect.inc.php");

    if(isset($_POST['searchVal'])){
        // turn that the user searched into a varible
        $searchQ = $_POST['searchVal'];
        // delete any symbols for security
        $searchQ = preg_replace("#[^0-9a-z]#i", "", $searchQ);

        $output      = "";
        $link        = "";
        $searchArray = array();
        $searchIndex = 0;

        // Search through these columns inside the main database
        $userSearchQuery = mysql_query("SELECT * FROM dealerEmployees WHERE 
            firstName   LIKE '%$searchQ%' 
        ");

        // count the number of results
        $userCount = mysql_num_rows($userSearchQuery);
        if($userCount == 0){
            // $output = "There Were No Search Results";
        }else{
            while($row = mysql_fetch_array($userSearchQuery)){
                // define dynamic varibles for each loop iteration
                $id         = $row['id'];
                $firstName  = $row['firstName'];
                $lastName   = $row['lastName'];
                $address    = $row['address'];
                $phone      = $row['phone'];
                $email      = $row['email'];
                $password   = $row['password'];
                $permission = $row['permission'];
                $photo      = "images/" . $row['profilePhoto'];

                $output .= "<li><div class='employeeSearch' style=\"background: url('$photo'); width: 75px; height: 75px\"></div><h6>" . $firstName  . "</h6>" . " " .  "<h6>" . $lastName . "</h6><a href='#' class='employee' data-firstName='$firstName' data-lastName='$lastName' data-address='$address' data-phone='$phone' data-email='$email' data-password='$password' data-permission='$permission' data-id='$id'>Select Employee</a></li>";
            }
        }
    }

    echo $output;

【问题讨论】:

    标签: javascript php jquery html session


    【解决方案1】:

    你可以试试这个吗?

    > "SELECT id, firstName, lastName, address, phone, email, password,
    > permission profilePhone  FROM dealerEmployees WHERE 
    >             firstName = '".$searchQ."' Limit 1"
    

    也许类似的条件给出了超过 1 个结果。

    【讨论】:

    • 当我使用您在上面提供的内容时,它不会退出,但如果我现在删除一封信时搜索某些用户,它会导致与以前相同的问题
    【解决方案2】:

    也许您可以从dealerEmployees where firstName = '".$searchQ."' 中选择count(id) 作为id,并使用if 子句检查计数。也许您遇到的问题是同名用户太多。

    【讨论】:

      【解决方案3】:

      所以经过一些测试后,我发现了发生了什么。当搜索加载搜索结果时,它会将会话中的密码变量更改为搜索中出现的用户的密码,因为两个变量具有相同的名称,我需要做的就是更改密码变量的 var 名称在搜索结果中与会话密码不同。

      感谢大家的帮助!!!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-23
        • 1970-01-01
        • 2013-07-11
        • 1970-01-01
        • 2011-01-22
        • 1970-01-01
        • 2015-10-25
        • 1970-01-01
        相关资源
        最近更新 更多