简体   繁体   English

为什么这种形式不会调用JS函数? (试图获取使用PHP脚本和AJAX调用MySQL数据库的自动完成表单字段)

[英]Why this form won't call the JS function? (trying to get an autocomplete form field calling a MySQL database with a PHP script and AJAX)

I'm trying to use the .autocomplete() function of jQuery UI with a list of usernames taken from a MySQL database (using a PHP script), but for some reason is not working. 我正在尝试将jQuery UI.autocomplete()函数与从MySQL数据库获取的用户名列表结合使用(使用PHP脚本),但是由于某些原因无法正常工作。 The console won't show me any errors. 控制台不会显示任何错误。

The form have several items in it and it does work ok (the form is at the editarPerfil.php file, which gets processed by guardarConfigPerfil.php file). 该表单中包含多个项目,并且可以正常工作(该表单位于editarPerfil.php文件,该文件由guardarConfigPerfil.php文件处理)。

But for some reason, I don't get anything from the JS file, which should be called when the content of the userName field is changed. 但是由于某种原因,我没有从JS文件中得到任何东西,当userName字段的内容更改时应调用该文件。

This is my HTML form: 这是我的HTML表单:

<form method="POST" action ="guardarConfigPerfil.php">
<div class="form-group ui-widget">
<label for="userName">Username</label>
<input type="userName" class="form-control" value="<?php echo $userName; ?>" name="userName" id="userName" onchange="autocompletarUserNames();">
</div>

This is at my footer on that page: 这是我在该页面上的页脚:

<!-- jQuery y Bootstrap minificados -->
<script src="http://code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="/js/formularios.js"></script>

This is my formularios.js file: 这是我的Formularios.js文件:

function autocompletarUserNames() {
    $( "#userName" ).autocomplete({
                      source: '/perfil/autocompletarUsername.php'
                      });
};

This is my php file: 这是我的php文件:

<?php
session_start(); 
include $_SERVER['DOCUMENT_ROOT'].'/private/conectar.php';

$conectar = new PDO('mysql:host='.HOST.'; dbname='.DATABASE.'; charset=utf8', USER, PASS); 

    //get search term
    $user = $_GET['userName'];

    $buscarUsuario = $conectar->prepare("
                SELECT userName 
                FROM usuarios 
                WHERE userName 
                LIKE '%?%'
                ORDER BY userName ASC
                ");
    $buscarUsuario->bindParam(1, $user);
    $buscarUsuario->execute();
    $row = $buscarUsuario->fetchAll(PDO::FETCH_ASSOC);

    foreach ($row as $key => $value) {
        $data[] = $value['userName'];
    }

    //return json data
    echo json_encode($data);
?>

Now, when I get into the form, and I click on the userName field and type... nothing happens. 现在,当我进入表单时,我单击userName字段并键入...,什么都没有发生。 What am I doing wrong? 我究竟做错了什么?

The folder tree is set up like this: 文件夹树的设置如下:

public_html/perfil/autocompletarUsername.php     <--- the php script
public_html/perfil/editarPefil.php               <--- the form
public_html/js/formularios.js                    <--- the js file

You should only ever have to call once: 您只需要打一次电话:

$( "#userName" ).autocomplete({
    source: '/perfil/autocompletarUsername.php'
});

Most likely on page load, in the $(document).ready(); 最有可能在页面加载时,在$(document).ready();中;

It looks like when you make a change to the text box, autocomplete is initiated again and again. 看起来像当您更改文本框时,自动完成一次又一次地启动。 Try removing the onChange and calling autocomplete like this: 尝试删除onChange并像这样调用自动完成:

$(document).ready(function() {
    $( "#userName" ).autocomplete({
        source: '/perfil/autocompletarUsername.php'
     });
});

Once. 一旦。 This is assuming your file path is correct, of course. 当然,这是假设您的文件路径正确。

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

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