donglinxin8765 2016-12-23 14:59
浏览 77
已采纳

具有2个参数的数组,用于自动完成jquery

Here is my code :

<script type="text/javascript">
$("#rechercheVilleChargement").autocomplete({
    source : 'http://localhost:8080/SuiviCollectes/MODEL/autocomplete.php',
    select: function (event, ui) {
        $("#rechercheVilleChargement").val(ui.item.label); // display the selected text
        $("#rechercheVilleChargementDPT").val(ui.item.value);
    }
});

The source code :

<?php

try
{
    $bdd = new PDO('mysql:host=localhost;dbname=x;charset=utf8', 'x', 'x');
    array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
}
catch(Exception $e)
{
    die('Erreur: ' . $e->getMessage());
}

$term = $_GET['term'];

$requete = $bdd->prepare('SELECT ville_nom, ville_departement FROM villes WHERE ville_nom LIKE :term ORDER BY ville_population_2012 DESC LIMIT 15'); // j'effectue ma requête SQL grâce au mot-clé LIKE
$requete->execute(array('term' => '%'.$term.'%'));

$array = array(); // Création du tableau

/* styles PDOStatement::fetch */

while($donnee = $requete->fetch())
{
    array_push($array, $donnee['ville_nom'] . ' (' . $donnee['ville_departement'] . ')'); // Ajout de la donnée au tableau
}

echo json_encode($result); //Conversion en Json

My question is , how can i get 2 values from php? I tried to do :

$result = $requete->fetchall(PDO::FETCH_ASSOC);

echo json_encode($result); //Conversion en Json

But when i'm trying something like :

$("#rechercheVilleChargement").val(ui.item.ville_nom); // display the selected text
$("#rechercheVilleChargementDPT").val(ui.item.ville_departement);

The autocomplete list is empty.

I would like $("#rechercheVilleChargement").val(ui.item.label) to be a value of one sql column and $("#rechercheVilleChargementCP").val(ui.item.value) to be another one in order to put the value in the input with id : rechercheVilleChargementCP.

2nd question : Can I create something like $("#rechercheVilleChargement").val(ui.item.cityname)?

Or ui.item are not alterable?

Just a precision, the first code I put is working well, but I get only one value to put in the input affected by autocompletion.

Thank you very much for your help.

  • 写回答

1条回答 默认 最新

  • dsd57259 2016-12-23 15:25
    关注

    I found the solution i was looking for. I am sorry, i should try harder before posting.

    Here is my solution :

    try
    {
        $bdd = new PDO('mysql:host=localhost;dbname=SuiviCollectesLPR;charset=utf8', 'root', '');
        array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
    }
    catch(Exception $e)
    {
        die('Erreur: ' . $e->getMessage());
    }
    
    $term = $_GET['term'];
    
    $requete = $bdd->prepare('SELECT * FROM villes WHERE ville_nom LIKE :term ORDER BY ville_population_2012 DESC LIMIT 15'); // j'effectue ma requête SQL grâce au mot-clé LIKE
    $requete->execute(array('term' => '%'.$term.'%'));
    
    while ($donnee = $requete->fetch(PDO::FETCH_ASSOC)) {
    $return_arr[] = array(
        'label'=>$donnee['ville_nom'],
        'dpt'=>$donnee['ville_departement']);
    }
    //array_push($return_arr,$row_array);
    
    echo json_encode($return_arr); //Conversion en Json
    

    JS :

    <script type="text/javascript">
    $("#rechercheVilleChargement").autocomplete({
        source : 'http://localhost:8080/SuiviCollectes/MODEL/autocomplete.php',
        select: function (event, ui) {
            $("#rechercheVilleChargement").val(ui.item.label); // display the selected text
            $("#rechercheVilleChargementDPT").val(ui.item.dpt); // display the selected text
        }
    });
    

    Thank you anyway.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛