IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AJAX Discussion :

[AJAX] Script de mon moteur de recherche


Sujet :

AJAX

  1. #1
    Membre confirmé

    Profil pro
    Inscrit en
    Août 2008
    Messages
    1 191
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2008
    Messages : 1 191
    Points : 595
    Points
    595
    Par défaut [AJAX] Script de mon moteur de recherche
    Bonjour, je tente d'apprendre à faire un petit moteur de recherche
    en appelant un fichier php.

    le script ne fonctionne pas bien

    voici recherche_piece.php

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
      function send(mtocle)
      {
         var ajax = document.createElement("script");
         ajax.src = "ajax.php?motcle="+mtocle;
         ajax.type = "text/javascript";
         document.body.appendChild(ajax);
         document.body.removeChild(ajax);
      }
     
      function callback(motcle)
      {
          var resultalt = motcle;
          alert(motcle);
          //document.getElementById("resultat").innerHTML = motcle;
      }
     
      function recupMotCle()
      {
         var motcle = document.getElementById("motcle");
         send(motcle.value);
      }
    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    <h1>resultat</h1>
     
    <input id="motcle" type="text" value="" onkeyup="recupMotCle()"/>
    <div id="resultat">
    </div>

    voici appelle du fichier ajax.php

    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <?php header("Content-type:text/javascript")l;?>
     
    var motcle="<?php  echo $_GET['motcle']?>";
     
    <?php
    $simul_resultat = array("piece" => array("memoire","montre","marteau"));
    ?>
     
    var resultat = "<?php echo $resultat ?>";
    callback(resultat);


    j'aimerais remplir ma div de mes résultat trouvé,
    comment je dois m'y prendre pour y arriver ? lorsque je tape
    une lettre je n'ai pas de retour,
    si je mets callback(resultat) à la place de callback(motcle);


    j'ai bien mes lettre taper.
    merci d'avance pour les réponses

  2. #2
    Invité
    Invité(e)
    Par défaut
    Bonjour,
    Il faut dire que tu ne parles pas vraiment d'AJAX, mais de syntaxe PHP pour retrouver une lettre ou un morceau de mot dans un ARRAY.

    Ce que tu cherches a faire est strictement la même démarche que celle qui te ramène un auto-complément sur un imput.
    De là, tu pourrais adapter a ton besoins, très facilement.

    par exemple
    1) un SQL crées cette table
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
     
     
    CREATE TABLE IF NOT EXISTS `ajax_pays` (
      `id` int(6) NOT NULL AUTO_INCREMENT,
      `nom` varchar(250) NOT NULL DEFAULT '',
      PRIMARY KEY  (`id`)
    );
     
    INSERT INTO `ajax_pays` (`id`, `nom`) VALUES
    (1, 'Afghanistan'),
    (2, 'Aringland Islands'),
    (3, 'Albania'),
    (4, 'Algeria'),
    (5, 'American Samoa'),
    (6, 'Andorra'),
    (7, 'Angola'),
    (8, 'Anguilla'),
    (9, 'Antarctica'),
    (10, 'Antigua and Barbuda'),
    (11, 'Argentina'),
    (12, 'Armenia'),
    (13, 'Aruba'),
    (14, 'Australia'),
    (15, 'Austria'),
    (16, 'Azerbaijan'),
    (17, 'Bahamas'),
    (18, 'Bahrain'),
    (19, 'Bangladesh'),
    (20, 'Barbados'),
    (21, 'Belarus'),
    (22, 'Belgium'),
    (23, 'Belize'),
    (24, 'Benin'),
    (25, 'Bermuda'),
    (26, 'Bhutan'),
    (27, 'Bolivia'),
    (28, 'Bosnia and Herzegovina'),
    (29, 'Botswana'),
    (30, 'Bouvet Island'),
    (31, 'Brazil'),
    (32, 'British Indian Ocean territory'),
    (33, 'Brunei Darussalam'),
    (34, 'Bulgaria'),
    (35, 'Burkina Faso'),
    (36, 'Burundi'),
    (37, 'Cambodia'),
    (38, 'Cameroon'),
    (39, 'Canada'),
    (40, 'Cape Verde'),
    (41, 'Cayman Islands'),
    (42, 'Central African Republic'),
    (43, 'Chad'),
    (44, 'Chile'),
    (45, 'China'),
    (46, 'Christmas Island'),
    (47, 'Cocos (Keeling) Islands'),
    (48, 'Colombia'),
    (49, 'Comoros'),
    (50, 'Congo'),
    (51, 'Congo'),
    (52, ' Democratic Republic'),
    (53, 'Cook Islands'),
    (54, 'Costa Rica'),
    (55, 'Ivory Coast (Ivory Coast)'),
    (56, 'Croatia (Hrvatska)'),
    (57, 'Cuba'),
    (58, 'Cyprus'),
    (59, 'Czech Republic'),
    (60, 'Denmark'),
    (61, 'Djibouti'),
    (62, 'Dominica'),
    (63, 'Dominican Republic'),
    (64, 'East Timor'),
    (65, 'Ecuador'),
    (66, 'Egypt'),
    (67, 'El Salvador'),
    (68, 'Equatorial Guinea'),
    (69, 'Eritrea'),
    (70, 'Estonia'),
    (71, 'Ethiopia'),
    (72, 'Falkland Islands'),
    (73, 'Faroe Islands'),
    (74, 'Fiji'),
    (75, 'Finland'),
    (76, 'France'),
    (77, 'French Guiana'),
    (78, 'French Polynesia'),
    (79, 'French Southern Territories'),
    (80, 'Gabon'),
    (81, 'Gambia'),
    (82, 'Georgia'),
    (83, 'Germany'),
    (84, 'Ghana'),
    (85, 'Gibraltar'),
    (86, 'Greece'),
    (87, 'Greenland'),
    (88, 'Grenada'),
    (89, 'Guadeloupe'),
    (90, 'Guam'),
    (91, 'Guatemala'),
    (92, 'Guinea'),
    (93, 'Guinea-Bissau'),
    (94, 'Guyana'),
    (95, 'Haiti'),
    (96, 'Heard and McDonald Islands'),
    (97, 'Honduras'),
    (98, 'Hong Kong'),
    (99, 'Hungary'),
    (100, 'Iceland'),
    (101, 'India'),
    (102, 'Indonesia'),
    (103, 'Iran'),
    (104, 'Iraq'),
    (105, 'Ireland'),
    (106, 'Israel'),
    (107, 'Italy'),
    (108, 'Jamaica'),
    (109, 'Japan'),
    (110, 'Jordan'),
    (111, 'Kazakhstan'),
    (112, 'Kenya'),
    (113, 'Kiribati'),
    (114, 'Korea (north)'),
    (115, 'Korea (south)'),
    (116, 'Kuwait'),
    (117, 'Kyrgyzstan'),
    (118, 'Lao People''s Democratic Republic'),
    (119, 'Latvia'),
    (120, 'Lebanon'),
    (121, 'Lesotho'),
    (122, 'Liberia'),
    (123, 'Libyan Arab Jamahiriya'),
    (124, 'Liechtenstein'),
    (125, 'Lithuania'),
    (126, 'Luxembourg'),
    (127, 'Macao'),
    (128, 'Macedonia'),
    (129, 'Madagascar'),
    (130, 'Malawi'),
    (131, 'Malaysia'),
    (132, 'Maldives'),
    (133, 'Mali'),
    (134, 'Malta'),
    (135, 'Marshall Islands'),
    (136, 'Martinique'),
    (137, 'Mauritania'),
    (138, 'Mauritius'),
    (139, 'Mayotte'),
    (140, 'Mexico'),
    (141, 'Micronesia'),
    (142, 'Moldova'),
    (143, 'Monaco'),
    (144, 'Mongolia'),
    (145, 'Montserrat'),
    (146, 'Morocco'),
    (147, 'Mozambique'),
    (148, 'Myanmar'),
    (149, 'Namibia'),
    (150, 'Nauru'),
    (151, 'Nepal'),
    (152, 'Netherlands'),
    (153, 'Netherlands Antilles'),
    (154, 'New Caledonia'),
    (155, 'New Zealand'),
    (156, 'Nicaragua'),
    (157, 'Niger'),
    (158, 'Nigeria'),
    (159, 'Niue'),
    (160, 'Norfolk Island'),
    (161, 'Northern Mariana Islands'),
    (162, 'Norway'),
    (163, 'Oman'),
    (164, 'Pakistan'),
    (165, 'Palau'),
    (166, 'Palestinian Territories'),
    (167, 'Panama'),
    (168, 'Papua New Guinea'),
    (169, 'Paraguay'),
    (170, 'Peru'),
    (171, 'Philippines'),
    (172, 'Pitcairn'),
    (173, 'Poland'),
    (174, 'Portugal'),
    (175, 'Puerto Rico'),
    (176, 'Qatar'),
    (177, 'Runion'),
    (178, 'Romania'),
    (179, 'Russian Federation'),
    (180, 'Rwanda'),
    (181, 'Saint Helena'),
    (182, 'Saint Kitts and Nevis'),
    (183, 'Saint Lucia'),
    (184, 'Saint Pierre and Miquelon'),
    (185, 'Saint Vincent and the Grenadines'),
    (186, 'Samoa'),
    (187, 'San Marino'),
    (188, 'Sao Tome and Principe'),
    (189, 'Saudi Arabia'),
    (190, 'Senegal'),
    (191, 'Serbia and Montenegro'),
    (192, 'Seychelles'),
    (193, 'Sierra Leone'),
    (194, 'Singapore'),
    (195, 'Slovakia'),
    (196, 'Slovenia'),
    (197, 'Solomon Islands'),
    (198, 'Somalia'),
    (199, 'South Africa'),
    (200, 'South Georgia and the South Sandwich Islands'),
    (201, 'Spain'),
    (202, 'Sri Lanka'),
    (203, 'Sudan'),
    (204, 'Suriname'),
    (205, 'Svalbard and Jan Mayen Islands'),
    (206, 'Swaziland'),
    (207, 'Sweden'),
    (208, 'Switzerland'),
    (209, 'Syria'),
    (210, 'Taiwan'),
    (211, 'Tajikistan'),
    (212, 'Tanzania'),
    (213, 'Thailand'),
    (214, 'Togo'),
    (215, 'Tokelau'),
    (216, 'Tonga'),
    (217, 'Trinidad and Tobago'),
    (218, 'Tunisia'),
    (219, 'Turkey'),
    (220, 'Turkmenistan'),
    (221, 'Turks and Caicos Islands'),
    (222, 'Tuvalu'),
    (223, 'Uganda'),
    (224, 'Ukraine'),
    (225, 'United Arab Emirates'),
    (226, 'United Kingdom'),
    (227, 'United States of America'),
    (228, 'Uruguay'),
    (229, 'Uzbekistan'),
    (230, 'Vanuatu'),
    (231, 'Vatican City'),
    (232, 'Venezuela'),
    (233, 'Vietnam'),
    (234, 'Virgin Islands (British)'),
    (235, 'Virgin Islands (US)'),
    (236, 'Wallis and Futuna Islands'),
    (237, 'Western Sahara'),
    (238, 'Yemen'),
    (239, 'Zaire'),
    (240, 'Zambia'),
    (241, 'Zimbabwe');
    2) lance ce PHP qui genere ton AJAX
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
     
    <?php
    header('Content-type: text/html; charset=ISO-8859-1');
    ?>
    <script type="text/javascript">
    <!--
    function Capter(lui)
    {
    document.LeForm.LePays.value=lui;
    document.getElementById('LeRetour2').style.visibility="hidden";
    }
     
    function maFonctionAjax(LePays)
    {
      if(LePays.length != 0) {
      var MonAjax;
      if (window.XMLHttpRequest){ MonAjax = new XMLHttpRequest();}
      else if (window.ActiveXObject) {MonAjax = new ActiveXObject('Microsoft.XMLHTTP');}
      else {
        alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
        MonAjax = false;   }
      MonAjax.open('POST',"ajax_pays_pdo.php",true);
      MonAjax.onreadystatechange = function()
      {
          if (MonAjax.readyState == 4 && MonAjax.status==200)
          {
                  if (MonAjax.responseText !='')
                    {
                        document.getElementById('LeRetour2').innerHTML="<ul>"+MonAjax.responseText+"</ul>";
                    } else
                      {
                        document.getElementById('LeRetour2').innerHTML='';
                      }
          }
          else{
               document.getElementById('LeRetour2').style.visibility="visible";
     
               }
      }
      MonAjax.setRequestHeader('Content-type','application/x-www-form-urlencoded');
      MonAjax.send('LePays='+LePays);
     }
    }
    //-->
    </script>
     
    <div class='centre01'>
    <br /><br /><br />
    <form method='post'  action='#' id='LeForm' name='LeForm'>
    Le Pays:  <input class='le213'  name='LePays' id='LePays' type='text' autocomplete='off'  onkeyup='maFonctionAjax(this.value);return false;' value=''  ><br />
    <input type='submit' value='envoyer'  /><br />
    </form>
    <div class='LeRetour2' id='LeRetour2'>
    </div>
    <br /><br />
    </div>
    </body></html>
    3) il appel ce php
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
     
    <?php
    header('Content-type: text/html; charset=ISO-8859-1');
    try {
      $bdd = new PDO('mysql:host=localhost;dbname=jplyne', 'root', '');
      $bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     }
    catch(Exception $e) {
      die('Erreur : '.$e->getMessage());
     }
     
    $LePays = (isset($_POST['LePays'])) ? $_POST['LePays'] : '';
    if(strlen($LePays) >0)
      {
      $LeBuf="";
      $req = $bdd->query("SELECT nom FROM ajax_pays WHERE nom  LIKE '$LePays%' LIMIT 10");
       while ($row = $req->fetch())
        {
        $rowx=$row['nom'];
        $LeBuf .="<li  onClick=\"Capter('".$rowx."');\">".$rowx."</li>";
        }
      echo $LeBuf;
      }
    $bdd=NULL;
    ?>
    A++
    Christele
    Dernière modification par Invité ; 04/05/2013 à 09h43. Motif: ajout du code

Discussions similaires

  1. [MySQL] Mon moteur de recherche ne fonctionne pas sous ie
    Par ploufleouf74 dans le forum PHP & Base de données
    Réponses: 6
    Dernier message: 27/07/2009, 12h21
  2. correction l'erreur dans mon moteur de recherche
    Par hadjiphp dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 7
    Dernier message: 20/05/2009, 17h37
  3. [AJAX] création d'un moteur de recherche de données
    Par argon dans le forum Général JavaScript
    Réponses: 0
    Dernier message: 18/11/2008, 11h27
  4. [MySQL] Améliorer mon moteur de recherche
    Par younek dans le forum PHP & Base de données
    Réponses: 1
    Dernier message: 10/09/2008, 23h59
  5. Réponses: 2
    Dernier message: 12/12/2006, 16h33

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo