Input text with autocomplete – Uncaught TypeError: $.ajax is not a function in PHP, MySQL and jQuery

huangapple go评论70阅读模式
英文:

Input text with autocomplete - Uncaught TypeError: $.ajax is not a function in PHP, MySQL and jQuery

问题

我正在尝试在jQuery中使用PHP和MySQL从数据库中获取用户信息来创建一个自动完成文本输入框。我遇到了以下错误:

Uncaught TypeError: $.ajax is not a function
at HTMLInputElement.<anonymous> (file.php:332)
at HTMLInputElement.dispatch (jquery-3.3.1.slim.min.js:2)
at HTMLInputElement.v.handle (jquery-3.3.1.slim.min.js:2)

我在所有页面中包含的header.php文件中导入了jQuery:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

这是我的file.php文件,其中包含脚本和输入部分,我只会留下部分代码,当然,输入框位于一个表单内:

<input type="text" name="autor" id="autor" placeholder="Escreve o nome do autor" />
<div id="autorLista"></div>

<script>
    $(document).ready(function() {
        $('#autor').keyup(function() {
            var query = $(this).val();
            console.log(query);

            if (query != '') {
                $.ajax({
                    url: "./components/search.php",
                    method: "POST",
                    data: {
                        query: query
                    },
                    success: function(data) {
                        $('#autorLista').fadeIn();
                        $('#autorLista').html(data);
                    }
                });
            }
        });

        $(document).on('click', 'li', function() {
            $('#autor').val($(this).text());
            $('#autorLista').fadeOut();
        });
    });
</script>

我甚至不知道PHP代码是否正常工作,但我认为是的。我将在下面留下它(search.php):

<?php

if (isset($_POST['query'])) {
    $link = new_db_connection();
    $stmt = mysqli_stmt_init($link);

    $output = '';

    $query = "SELECT id_user, nome_user FROM users WHERE nome_user LIKE ?";

    if (mysqli_stmt_prepare($stmt, $query)) {
        $output = "<ul>";

        if (mysqli_stmt_num_rows($stmt) > 0) {
            mysqli_stmt_bind_param($stmt, 's', $nome_user);

            mysqli_stmt_execute($stmt);

            mysqli_stmt_bind_result($stmt, $id_user, $nome_user);

            if (mysqli_stmt_fetch($stmt)) {
                $output .= "<li>" . $nome_user . "</li>";
            }
        } else {
            $output = '<li>O utilizador que procura não existe.</li>';
        }
        $output .= "</ul>";
        echo $output;
    } else {
        echo 'erro';
    }
}

我已经尝试更改jQuery版本,检查了slim版本等等,但都没有使它工作...有什么想法吗?如果有任何疑问或者如果我表述不清楚,请告诉我。谢谢!

英文:

I'm trying to do a autocomplete text input in jQuery with user information from database in PHP and MySQL.
I'm getting the following error:

Uncaught TypeError: $.ajax is not a function
at HTMLInputElement.&lt;anonymous&gt; (file.php:332)
at HTMLInputElement.dispatch (jquery-3.3.1.slim.min.js:2)
at HTMLInputElement.v.handle (jquery-3.3.1.slim.min.js:2)

I import jQuery on header.php that is included in all pages:

&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js&quot;&gt;&lt;/script&gt;

This is my file.php which is the file with the script and the input, I'll leave just a part of the code, of course the input is inside a form

&lt;input type=&quot;text&quot; name=&quot;autor&quot; id=&quot;autor&quot; placeholder=&quot;Escreve o nome do autor&quot; /&gt;
&lt;div id=&quot;autorLista&quot;&gt;&lt;/div&gt;

&lt;script&gt;
    $(document).ready(function() {
        $(&#39;#autor&#39;).keyup(function() {
            var query = $(this).val();;
            console.log(query);

            if (query != &#39;&#39;) {
                $.ajax({
                    url: &quot;./components/search.php&quot;,
                    method: &quot;POST&quot;,
                    data: {
                        query: query
                    },
                    success: function(data) {
                        $(&#39;#autorLista&#39;).fadeIn();
                        $(&#39;#autorLista&#39;).html(data);
                    }
                });
            }
        });

        $(document).on(&#39;click&#39;, &#39;li&#39;, function() {
            $(&#39;#autor&#39;).val($(this).text());
            $(&#39;#autorLista&#39;).fadeOut();
        });
    });
&lt;/script&gt;

I don't even know if php code is working correctly but I think it is. I'll leave it below (search.php):

&lt;?php

if (isset($_POST[&#39;query&#39;])) {
    $link = new_db_connection();
    $stmt = mysqli_stmt_init($link);

    $output = &#39;&#39;;

    $query = &quot;SELECT id_user, nome_user FROM users WHERE nome_user LIKE ?&quot;;

    if (mysqli_stmt_prepare($stmt, $query)) {
        $output = &quot;&lt;ul&gt;&quot;;

        if (mysqli_stmt_num_rows($stmt) &gt; 0) {
            mysqli_stmt_bind_param($stmt, &#39;s&#39;, $nome_user);

            mysqli_stmt_execute($stmt);

            mysqli_stmt_bind_result($stmt, $id_user, $nome_user);

            if (mysqli_stmt_fetch($stmt)) {
                $output .= &quot;&lt;li&gt;&quot; . $nome_user . &quot;&lt;/li&gt;&quot;;
            }
        } else {
            $output = &#39;&lt;li&gt;O utilizador que procura n&#227;o existe.&lt;/li&gt;&#39;;
        }
        $output .= &quot;&lt;/ul&gt;&quot;;
        echo $output;
    } else {
        echo &#39;erro&#39;;
    }
}

I've tried changing versions of jquery, checked the slim version, etc and nothing makes this work... Any idea, please? Any doubts or if I wasn't clear, just tell me please. Thank you!

答案1

得分: 1

In your error it is showing you are using jQuery slim, which doesn't have Ajax.

Please check you are using ONLY normal jQuery, not the slim version.

英文:

In your error it is showing you are using jQuery slim, which doesn't have Ajax.

> (jquery-3.3.1.slim.min.js:2)

Please check you are using ONLY normal jQuery, not the slim version.

答案2

得分: 0

I was importing a slim version when importing bootstrap because it was suggested on their webpage.
现在我使用这个

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

而不是

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>

英文:

I was importing a slim version when importing bootstrap because it was suggested on their webpage.
Now I'm using this

&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js&quot;&gt;&lt;/script&gt;

Instead of

&lt;script src=&quot;https://code.jquery.com/jquery-3.3.1.slim.min.js&quot; integrity=&quot;sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;

huangapple
  • 本文由 发表于 2020年1月7日 01:39:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定