简体   繁体   English

PHP数据库ajax​​无法更新

[英]PHP Database ajax not updating

A while ago i made a search function with ajax and php. 前一阵子我用ajax和php进行了搜索。 You could fill in a textbox with text and it would try to find a match among all countries stored in the database. 您可以在文本框中填写文本,然后尝试在数据库中存储的所有国家/地区中找到匹配项。 Now i am refining the code and making it PDO, but i broke something and i cant find out what. 现在,我正在完善代码并使其成为PDO,但是我弄坏了一些东西,却找不到。

this is my plain HTML 这是我的纯HTML

<head>
    <title>Ajax</title>
    <link href="style/style.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript" src="scripts/Javascript.js"></script>
</head>

<body>
    <div id="main">
            <h1 class="title">Enter your country please</h1>

        <input type="text" id="search" autocomplete="off" onchange="">
            <h4 id="results-text">Showing results for: <b id="search-string">Array</b></h4>

        <ul id="results"></ul>
    </div>
</body>

here is my Jquery and javascript. 这是我的Jquery和javascript。 note i have not changed anything to the HTML nor javascript so it can not by a type error. 请注意,我没有更改HTML或javascript的任何内容,因此它不会出现类型错误。

$(document).ready(function() {
alert('asdf');

function search() {
    var query_value = $('input#search').val();
    $('b#search-string').html(query_value);

    if(query_value !== ''){
        $.ajax({
            type: "POST",
            url: "search.php",
            data: { query: query_value },
            cache: false,
            success: function(html){
                $("ul#results").html(html);
            }
        });
    }

    return false;
}

$("input#search").live("keyup", function(e) {
    clearTimeout($.data(this, 'timer'));
    var search_string = $(this).val();

    if (search_string == '') {
        $("ul#results").fadeOut();
        $('h4#results-text').fadeOut();
    }

    else {
        $("ul#results").fadeIn();
        $('h4#results-text').fadeIn();
        $(this).data('timer', setTimeout(search, 100));
        };
    });
});

And here is my Search.PHP 这是我的Search.PHP

<?php
class SearchEngine{

    private $html;

    public function __construct($conn){

        $this->html = '<li class="result">
                            <h3>NameReplace</h3>
                            <a target="_blank" href="ULRReplace"></a>
                        </li>';

        if (isset($_POST["query"])) {
            $search_string = mysql_real_escape_string($search_string);
        }

        else{
            $search_string = 'b';
        }

        if (strlen($search_string) >= 1 && $search_string !== ' ') {

            $query = 'SELECT * FROM country WHERE name LIKE "%' . $search_string . '%"';
            $result = $conn->prepare($query);
            $result->execute();
            $result_array = $result->fetchAll();

                foreach ($result_array as $result) {
                    $display_name = preg_replace("/" . $search_string . "/i", "<b>" . $search_string . "</b>", $result['name']);
                    $display_url = 'sadf';

                    $output = str_replace('NameReplace', $display_name, $this->html);
                    $output = str_replace('ULRReplace', $display_url, $output);
                    echo($output);
                }
        }
    }

    }
?>

The problems: 问题:

  1. the Post query is never created, for this i made a isset so for now when there is no Post Query created. 永远不会创建Post查询,为此,我进行了一个isset设置,因此现在没有创建Post Query时。 It will create a Post Query with value "B". 它将创建一个值为“ B”的后查询。

  2. I think the page never gets updated, but i cant be 100% sure since the post never gets created so there is never a update to the query. 我认为该页面永远不会更新,但是我不能100%确定,因为该帖子永远不会被创建,所以查询永远不会更新。 And for some reason the results are not placed in there correct spot. 由于某种原因,结果没有放在正确的位置。

Any help will be much appreciated. 任何帮助都感激不尽。 Please be gentle i am new to Ajax and i rather want to understand than have the solution. 请保持谦虚,我是Ajax的新手,我想了解而不是找到解决方案。 Thank you 谢谢

Are you really posting your search string to a PHP class? 您是否真的将搜索字符串发布到PHP类?

That's not how it works, you need to create an instance of that class and use it. 这不是它的工作方式,您需要创建该类的实例并使用它。

Create a separate php file for that class, and included it in search.php 为该类创建一个单独的php文件,并将其包含在search.php

You can have a simple class looking like the following: 您可以拥有一个类似于以下内容的简单类:

search_engine.php search_engine.php

<?php
class SearchEngine{

    private $conn;

    function __construct($conn){
        $this->conn = $conn;
    }

    function get_search_results($search_string){
        $query = 'SELECT * FROM country WHERE name LIKE :search';
        $result = $this->conn->prepare($query);
        $result->execute(array(':search'=>'%'.$search_string.'%'));
        $result_array = $result->fetchAll();

        return $result_array;   
    }
}
?>

search.php search.php中

if (isset($_POST["query"])) {
    $search_string = $_POST["query"];
    if(strlen($search_string) >= 1 && $search_string !== ' ') {
        include 'search_engine.php'
        $engine = new SearchEngine($conn);
        $results = $engine->get_search_results($search_string);
        foreach($results as $result){
          //do something
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM