使用Ajax和PHP的多个文本框填充

时间:2014-03-24 14:10:35

标签: php jquery ajax

您好我有一个代码,必须基于Ajax和PHP做多个文本框。

因此,每个文本框都必须使用MySQL中的数据填充它自己。

除了从差异表输入数据时所有文本框都使用相同的输出外,它的工作正常。如何将它们分开,以便它们各自显示数据库中的表格结果?

$ query和$ query2是文本框。

以下是代码:

<?php
    $db = new mysqli('localhost', 'root' ,'*', 'records');
    if(!$db){
        // Show error if we cannot connect.        
        echo 'ERROR: Could not connect to the database.';
    }     
    else 
    {
        // Is there a posted query string?
        if(isset($_POST['queryString'])) 
        {
            $queryString = $db->real_escape_string($_POST['queryString']);
            // Is the string length greater than 0?
            if(strlen($queryString) >0) 
            {
                $query = $db->query("SELECT naam_klant FROM overboekingen WHERE naam_klant LIKE '$queryString%' LIMIT 10");
                $query2 = $db->query("SELECT boekingsnummer FROM overboekingen WHERE boekingsnummer LIKE '$queryString%' LIMIT 10");

               if($query) 
               {
                   while ($result = $query ->fetch_object()) 
                   {
                      echo '<li onClick="fill(\''.$result->naam_klant.'\');">'.$result->naam_klant.'</li>';
                   }
               }   

               if($query2) 
               {
                   while ($result = $query2 ->fetch_object()) 
                   {
                       echo '<li onClick="fill(\''.$result->boekingsnummer.'\');">'.$result->boekingsnummer.'</li>';
                   }
               }
               else{
                   echo 'ERROR: There was a problem with the query.';
               }
          } 
          else{
          } // There is a queryString.
     } 
     else {
         echo 'There should be no direct access to this script!';
     }   
 }
?>

HTML:

<label>Boekingsnummer</label> 
<input type="text" name="boekingsnummer" size="20" class="inputString" onkeyup="lookup(this.value);" onblur="fill();" >    
<div id="suggestionsBox" class="suggestions" style="display: none;">
<div id="suggestionList" class="autoSuggestionsList">
</div>
</div>

<label>Naam klant</label> 
<input type="text" name="naam_klant" size="20" class="inputString" onkeyup="lookup(this.value);" onblur="fill();" >    
<div id="suggestionsBox" class="suggestions" style="display: none;">
<div id="suggestionList" class="autoSuggestionsList">
</div>
</div>

0 个答案:

没有答案