2017-02-22 33 views
0

我發現了一個完全符合我的要求的示例。我唯一的問題是,這種語法調用book-suggestion.php的輔助文件如果可能,我想在一個頁面中執行所有此功能的方法。在一個項目中執行服務器端和客戶端操作

這裏是第1步 - 客戶端

function book_suggestion() 
{ 
var book = document.getElementById("book").value; 
var xhr; 
if (window.XMLHttpRequest) { // Mozilla, Safari, ... 
    xhr = new XMLHttpRequest(); 
} else if (window.ActiveXObject) { // IE 8 and older 
    xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
} 
var data = "book_name=" + book; 
    xhr.open("POST", "book-suggestion.php", true); 
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");     
    xhr.send(data); 
    xhr.onreadystatechange = display_data; 
    function display_data() { 
    if (xhr.readyState == 4) { 
     if (xhr.status == 200) { 
     //alert(xhr.responseText);  
     document.getElementById("suggestion").innerHTML = xhr.responseText; 
     } else { 
     alert('There was a problem with the request.'); 
     } 
    } 
    } 
} 

這裏是第2部分 - 服務器端

<?php 
    //provide your hostname, username and dbname 
    $host=""; 
    $username=""; 
    $password=""; 
    $db_name=""; 
    //$con=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    $con=mysql_connect("$host", "$username", "$password"); 
    mysql_select_db("$db_name"); 
    $book_name = $_POST['book_name']; 
    $sql = "select book_name from book_mast where book_name LIKE '$book_name%'"; 
    $result = mysql_query($sql); 
    while($row=mysql_fetch_array($result)) 
    { 
    echo "<p>".$row['book_name']."</p>"; 
    } 
?> 

什麼我必須這樣做,他們都是這些部分組合在一個文件中?

回答

0

你可以做到所有的一頁,我使用.htaccess重寫,其中一切都通過一個索引頁漏斗,基本上做同樣的事情。你只需做你的HTML和exit的輸出上面的PHP完成時:

的index.php

<?php 
# Create some defines 
define('DB_HOST','localhost'); 
define('DB_NAME','database'); 
define('DB_USER','root'); 
define('DB_PASS',''); 
# Create a PDO connection, mysql_* is out of date and unsafe 
# Review PDO, there are some presets to the connection that should be explored 
# like emulated prepares and other such niceties 
$con = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME,DB_USER,DB_PASS); 
# If there is a value posted, do action 
if(!empty($_POST['book_name'])) { 
    # Bind parameters 
    $sql = "select book_name from book_mast where book_name LIKE ?"; 
    $query = $con->prepare($sql); 
    $query->execute(array($book_name.'%')); 
    # Fetch normally 
    while($row = $query->fetch(PDO::FETCH_ASSOC)) { 
     echo "<p>".$row['book_name']."</p>"; 
    } 
    ##*****THE IMPORTANT PART ******## 
    # Stop so you don't process the rest of the page in the ajax 
    exit; 
} 
?> 
<!-- THE REST OF YOUR HTML HERE --> 
<script> 
function book_suggestion() 
    { 
     var book = document.getElementById("book").value; 
     var xhr; 
     if (window.XMLHttpRequest) { // Mozilla, Safari, ... 
      xhr = new XMLHttpRequest(); 
     } else if (window.ActiveXObject) { // IE 8 and older 
      xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 

     var data = "book_name=" + book; 
     xhr.open("POST", "index.php", true); 
     xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");     
     xhr.send(data); 
     xhr.onreadystatechange = display_data; 

     function display_data() { 
      if (xhr.readyState == 4) { 
       if (xhr.status == 200) { 
        //alert(xhr.responseText);  
        document.getElementById("suggestion").innerHTML = xhr.responseText; 
       } else { 
        alert('There was a problem with the request.'); 
       } 
      } 
     } 
    } 
</script> 
相關問題