2016-12-14 5 views
1

ユーザーがテキストを書き込んだ場合、それは顧客の名前検索は、Ajax、PHPを使用して、ドロップダウンリストになります

<script> 
    function custlist() { 
     $.ajax({ 
      url: "custlist.php", 
      success: function(result) { 
       $("#customerlist").html(result); 
      }   
     }); 
    } 
    function showCustomers(str) { 
     $.ajax({ 
      type: "GET", 
      url: "customerlist.php", 
      data:'q='+str, 
      success: function(result) { 
       $("#customerlist").html(result); 
      }   
     }); 
    } 
</script> 

<input type="text" oninput="showCustomers(this.value)" placeholder="Search here" name="CustomerNo" /> 
<select name="Cno" id="customerlist" onfocus="custlist()"> 
    <option value="">Customer Name</option> 
</select> 

custlist.phpのドロップダウンリストが表示されるはずです、入力テキストボックスがあるはずです

<?php 
    $sql2 = 'SELECT Customer_Name as Cname,No from customers order by Customer_Name'; 
    $result2 = mysqli_query($connection, $sql2); 

    if (mysqli_num_rows($result2) > 0) { ?> 
     <option value="">Customer Names</option>     
     <?php // output data of each row 
      while($row2 = mysqli_fetch_assoc($result2)) { ?> 
       <option value="<?php echo $row2['No']; ?>"><?php echo $row2["Cname"]; ?> 
       </option> 
     <?php } ?> 
<?php } ?> 

customerlist.php

<?php   
    $q = $_REQUEST["q"]; 
    // lookup all hints from array if $q is different from "" 
    if ($q !== "") { 
     $sql2 = "SELECT Customer_Name as Cname,No from customers where Customer_Name like '".$q."%s' order by Customer_Name"; 
     $result2 = mysqli_query($connection, $sql2); 

     if (mysqli_num_rows($result2) > 0) { ?> 
      <option value="">Customer Names</option>     
      <?php // output data of each row 
       while($row2 = mysqli_fetch_assoc($result2)) { ?> 
        <option value="<?php echo $row2['No']; ?>"><?php echo $row2["Cname"]; ?> 
        </option> 
      <?php } ?> 
    <?php } ?> 
<?php } ?> 

私はドロップダウンにデータを取得していますが、テキストボックスに何かを書き込むと自動的にその文字と一致するドロップダウンが表示されます。

そして、私が持っているもう一つの問題...

第二号: - 私は最初、「ABD」を入力すると、「それは「ABD」で始まる顧客名を示すが自動的にそれはその後、「AB」で始まる次の名前が表示されますそれでは空です。 なぜですか?

ありがとうございます。

$.ajax({ 
    type: "GET", 
    url: "customerlist.php", 
    data:'q='+str, 
    success: function(result){ 
     $("#customerlist").html(result); 
    } 
}); 

とPHP:代わりに のJavascriptの

+1

PHPタグを '.php'ファイルであまり開いたり閉じたりする必要はありません。ただ代わりにHTMLをエコーする –

答えて

0

<?php 

$q = $_REQUEST["q"]; 

このJavascriptをやってみてください。

$.ajax({ 
    type: "POST", 
    url: "customerlist.php", 
    data: {q: str}, 
    success: function(result){ 
     $("#customerlist").html(result); 
    } 
}); 

とPHP:

<?php 

$q = $_POST['q']; 

希望します。

+1

私はこれを試したが運がなかった最初にそれは正しい結果をhttp://prntscr.com/dj9m8nこのようにhttp://prntscr.com/dj9luj –

+0

と私はテキストボックスとドロップダウンをマージすることができますか? –

+0

customerlist.phpには、次の行があります。 ajax呼び出しでjson_encode()をすべてエコーしてjquery/javascriptで使用する必要があるため、これは動作しないと思います –

関連する問題