2016-08-17 27 views
2

我在php中創建一個程序,用戶選擇一個字母,然後在屏幕上打印所有名字,這個字母存儲在我的mysql「presta_prova」數據庫中。 這裏是我的代碼(PHP文件):我可以使用ajax選擇mysqli數據庫行嗎?

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <style> 
 
    table { 
 
     width: 100%; 
 
     border-collapse: collapse; 
 
    } 
 
    table, 
 
    td, 
 
    th { 
 
     border: 1px solid black; 
 
     padding: 5px; 
 
    } 
 
    th { 
 
     text-align: left; 
 
    } 
 
    </style> 
 
    <script> 
 
    function showUser(str) { 
 
     if (str == "") { 
 
     document.getElementById("txtHint").innerHTML = ""; 
 
     return; 
 
     } else { 
 
     if (window.XMLHttpRequest) { 
 
      // code for IE7+, Firefox, Chrome, Opera, Safari 
 
      xmlhttp = new XMLHttpRequest(); 
 
     } else { 
 
      // code for IE6, IE5 
 
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
 
     } 
 
     xmlhttp.onreadystatechange = function() { 
 
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
 
      document.getElementById("txtHint").innerHTML = xmlhttp.responseText; 
 
      } 
 
     }; 
 
     xmlhttp.open("GET", "presta_prova.php?q=" + str, true); 
 
     xmlhttp.send(); 
 
     } 
 
    } 
 
    </script> 
 
</head> 
 

 
<body> 
 
    <form> 
 
    <select name="users" onchange="showUser(this.value)"> 
 
     <option value="">Scegliete una lettera:</option> 
 
     <option value="1">A</option> 
 
     <option value="2">B</option> 
 
     <option value="3">C</option> 
 
     <option value="4">D</option> 
 
    </select> 
 
    </form> 
 
    <br> 
 
    <div id="txtHint"><b>Vedi qui i tipi ti marche:</b> 
 
    </div> 
 
    <?php $q=i ntval($_GET[ 'q']); $con=m ysqli_connect('localhost', 'root', 'evolvia2016', 'presta_prova'); if (!$con) { die('Could not connect: ' . mysqli_error($con)); } mysqli_select_db($con, "presta_prova"); $sql="SELECT * FROM presta_prova WHERE marca LIKE '" 
 
    .$q%. "' "; $result=m ysqli_query($con,$sql); echo "<table> 
 
<tr> 
 
<th>Marca</th> 
 
<th>Descrizione</th> 
 
</tr>"; while($row=m ysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row[ "marca"] . "</td>"; echo "<td>" . $row[ "descrizione"] . "</td>"; echo "</tr>"; } echo "</table>"; mysqli_close($con); ?> 
 
</body> 
 

 
</html>
但是,當我選擇了一封信,這是我的輸出。而不是我的數據庫的行這個程序輸出的表頭。我做了我的代碼錯了嗎?也許這種方式不適用於mysqli數據庫?謝謝! 這裏是我的數據庫: enter image description here

enter image description here

+0

忽略會導致語法錯誤的隨機空格,'。$ q%.'也會導致語法錯誤。 '%',即通配符需要在SQL字符串中。 –

回答

0

如果目的是爲了填充新行的表,而不是追加的響應爲div這是它如何出現,那麼你可以重構代碼,並使用POST喜歡這個。

<?php 

    if($_SERVER['REQUEST_METHOD']=='POST'){ 
     ob_clean(); 

     /* Forgot to change this to POST! */ 
     $q=intval($_POST[ 'q']); 

     $con=mysqli_connect('localhost', 'root', 'evolvia2016', 'presta_prova'); 
     if (!$con)die('Could not connect: ' . mysqli_error($con)); 

     mysqli_select_db($con, "presta_prova"); 
     $sql="SELECT * FROM presta_prova WHERE marca LIKE '".$q."%' "; 
     $result=mysqli_query($con,$sql); 



     $html=array("<tr><th>Marca</th><th>Descrizione</th></tr>"); 

     while($row=mysqli_fetch_array($result)) { 
      $html[]="<tr><td>" . $row[ "marca"] . "</td><td>" . $row[ "descrizione"] . "</td></tr>"; 
     } 

     mysqli_close($con); 


     exit(implode(PHP_EOL,$html)); 
    } 

?> 


<!DOCTYPE html> 
<html> 
    <head> 
    <title>gotta have a title</title> 
     <style> 
     table { 
      width: 100%; 
      border-collapse: collapse; 
     } 
     table, 
     td, 
     th { 
      border: 1px solid black; 
      padding: 5px; 
     } 
     th { 
      text-align: left; 
     } 
     </style> 
     <script> 
     function showUser(str) { 
      if (str == "") { 
      document.getElementById("txtHint").innerHTML = ""; 
      return; 
      } else { 
      if (window.XMLHttpRequest) { 
       // code for IE7+, Firefox, Chrome, Opera, Safari 
       xmlhttp = new XMLHttpRequest(); 
      } else { 
       // code for IE6, IE5 
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
      xmlhttp.onreadystatechange = function() { 
       if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
       document.getElementById("results").innerHTML = xmlhttp.responseText; 
       } 
      }; 
      xmlhttp.open("POST", location.href, true);/*"presta_prova.php"*/ 
      /* send the content-type header */ 
      xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 

      /* my mistake here, this should be like this rather than object literal. */ 
      xmlhttp.send('q='+str); 

      } 
     } 
     </script> 
    </head> 
    <body> 
     <form> 
     <select name="users" onchange="showUser(this.value)"> 
      <option value="">Scegliete una lettera:</option> 
      <option value="1">A</option> 
      <option value="2">B</option> 
      <option value="3">C</option> 
      <option value="4">D</option> 
     </select> 
     </form> 
     <br> 
     <div id="txtHint"><b>Vedi qui i tipi ti marche:</b> 
     </div> 
     <table id='results'> 
     <tr> 
      <th>Marca</th> 
      <th>Descrizione</th> 
     </tr> 
     </table> 
    </body> 
</html> 

完整的工作示例沒有數據庫交互,但顯示選擇機制的工作。

<?php 
    if($_SERVER['REQUEST_METHOD']=='POST'){ 
     ob_clean(); 

     $q=intval($_POST[ 'q']); 
     $t=$_POST['text']; 

     /* emulated database search and results being generated */ 
     $html=array('<tr><th>Marca</th><th>Descrizione</th></tr>'); 
     for($i=0; $i < 10; $i++) $html[]='<tr><td>Marca:'.$i.' q='.$q.'</td><td>Descrizione:'.$i.' text='.$t.'</td></tr>'; 

     header('Content-Type: text/html'); 
     exit(implode(PHP_EOL, $html)); 
    } 
?> 
<!DOCTYPE html> 
<html> 
    <head> 
    <title>gotta have a title</title> 
     <style> 
     table { 
      width: 100%; 
      border-collapse: collapse; 
     } 
     table, 
     td, 
     th { 
      border: 1px solid black; 
      padding: 5px; 
     } 
     th { 
      text-align: left; 
     } 
     </style> 
     <script> 
     function showUser(o) { 
      if (o.value == '') { 
      document.getElementById("txtHint").innerHTML = ""; 
      return; 
      } else { 

      xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); 
      xmlhttp.onreadystatechange = function() { 
       if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
       document.getElementById("results").innerHTML = xmlhttp.responseText; 
       } 
      }; 
      xmlhttp.open("POST", location.href, true); 
      xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
      xmlhttp.send('q='+o.value+'&text='+o.options[o.options.selectedIndex].text); 
      } 
     } 
     </script> 
    </head> 
    <body> 
     <form> 
     <select name="users" onchange="showUser(this)"> 
      <option value="">Scegliete una lettera:</option> 

      <?php 
      /* Generate each letter of the alphabet as an option */ 
      for($i=0; $i<26; $i++){ 
       $char=chr($i + 65); 
       $int=$i+1; 
       echo "<option value='$int'>$char"; 
      } 
      ?> 
     </select> 
     </form> 
     <br> 
     <div id="txtHint"><b>Vedi qui i tipi ti marche:</b> 
     </div> 
     <table id='results'><!-- results will overwrite the table innerHTML --> 
     <tr> 
      <th>Marca</th> 
      <th>Descrizione</th> 
     </tr> 
     </table> 
    </body> 
</html> 
+0

確定這不是交互式的。我選了一封信,沒有任何反應。我不知道這是否適用於您的電腦。 –

+0

我必須承認在發送ajax請求中的參數時犯了一個小錯誤 - 我不認爲咖啡已經開始工作。當然,它應該是一個格式爲「q = A」等的字符串,也可以設置Content-Type頭。 – RamRaider

+0

也許我應該插入一些腳本來使其工作?我的本地主機上沒有任何內容。 –

相關問題