2014-01-25 177 views
0

我在http://www.w3schools.com/php/php_ajax_database.asp處發現了以下腳本。但我發現很難通過wordpress實現,這在本地主機完美工作,但在wordpress失敗。我已經完成搜索w /相同的過程,但仍然無法弄清楚。我可以一步一步詢問如何通過wordpress調用ajax腳本嗎?wordpress ajax - 通過wordpress通過AJAX從數據庫獲取信息

**的我的自定義的FrontPage * *

<?php 
/* 
Template Name: Custom Template 
*/ 
?> 
<?php 
/** 
* The template for displaying all pages. 
* 
* This is the template that displays all pages by default. 
* Please note that this is the WordPress construct of pages 
* and that other 'pages' on your WordPress site will use a 
* different template. 
* 
* @package WordPress 
* @subpackage Twenty_Eleven 
* @since Twenty Eleven 1.0 
*/ 

get_header(); ?> 
<script> 
function showUser(str) 
{ 
if (str=="") 
    { 
    document.getElementById("txtHint").innerHTML=""; 
    return; 
    } 
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","getuser.php?q="+str,true); 
xmlhttp.send(); 
} 
</script> 
     <div id="primary"> 
      <div id="content" role="main"> 

<form> 
<select name = "users" onChange = "showUser(this.value)"> 
         <?php 
          include 'dbconfig.php'; 
          $result=mysql_query("SELECT DISTINCT lastname FROM people ORDER BY lastname ASC;"); 
          echo '<option value="">' . 'Select an Agent' . '</option>'; 

          while ($row=mysql_fetch_array($result)) 
           { 
           echo '<option value="' . $row['lastname'] . '">' . $row['lastname'] . '</option>'; 
           } 
         ?> 
         </select> 
</form> 
<br> 
<div id="txtHint"><b>Person info will be listed here.</b></div> 

      </div><!-- #content --> 
     </div><!-- #primary --> 

<?php get_footer(); ?> 

PHP文件(getuser.php)

<?php 
$q = intval($_GET['q']); 

include 'dbconfig.php'; // PHP File to login credentials 
$sql="SELECT * FROM people WHERE id = '".$q."'"; 

$result = mysql_query($sql); 

echo "<table border='1'> 
<tr> 
<th>Firstname</th> 
<th>Lastname</th> 
<th>Age</th> 
<th>Hometown</th> 
<th>Job</th> 
</tr>"; 

while($row = mysql_fetch_array($result)) 
    { 
    echo "<tr>"; 
    echo "<td>" . $row['FirstName'] . "</td>"; 
    echo "<td>" . $row['LastName'] . "</td>"; 
    echo "<td>" . $row['Age'] . "</td>"; 
    echo "<td>" . $row['Hometown'] . "</td>"; 
    echo "<td>" . $row['Job'] . "</td>"; 
    echo "</tr>"; 
    } 
echo "</table>"; 

mysql_close($con); 
?> 

<?php 
$q = intval($_GET['q']); 

include 'dbconfig.php'; // PHP File to login credentials 
$sql="SELECT * FROM people WHERE id = '".$q."'"; 

$result = mysql_query($sql); 

echo "<table border='1'> 
<tr> 
<th>Firstname</th> 
<th>Lastname</th> 
<th>Age</th> 
<th>Hometown</th> 
<th>Job</th> 
</tr>"; 

while($row = mysql_fetch_array($result)) 
    { 
    echo "<tr>"; 
    echo "<td>" . $row['firstname'] . "</td>"; 
    echo "<td>" . $row['lastname'] . "</td>"; 
    echo "<td>" . $row['age'] . "</td>"; 
    echo "<td>" . $row['hometown'] . "</td>"; 
    echo "<td>" . $row['job'] . "</td>"; 
    echo "</tr>"; 
    } 
echo "</table>"; 

mysql_close($con); 
?> 

採取的行動: 轉換(的getUser。 PHP)作爲函數,然後添加到主題functions.php。然後打電話給wp add_action hook.please看下面

function getuser(){ 
    //get the data from ajax() call 
    //copied script from getuser.php 
    } 
    // creating Ajax call for WordPress 
    add_action('wp_ajax_nopriv_getuser', 'getuser'); 
    add_action('wp_ajax_getuser', 'getuser'); 

您的幫助非常感謝。謝謝。解決

+0

[見這個答案](http://stackoverflow.com/questions/20831673/send-a-form-from-jquery-to-php-with-ajax-wordpress/20831780#20831780),它可能會有一些見解。 – Ohgodwhy

回答

0

第一個問題:在管理-ajax.php

那些ADD_ACTION走,然後可以觸發functions.php的功能,它看起來好像你寫那些functions.php的?這甚至對於面向Ajax的客戶來說也是如此,儘管這並不一定有意義。

幾個其它點:

考慮使用jquery或Ajax庫至少在第一簡化的東西,在客戶端側。

考慮在這裏使用優秀的JSON api:http://wordpress.org/plugins/json-api/,我手動編寫了大量的wap ajax函數,然後發現該函數庫解決了我想做的工作少得多的工作。

編輯:

這是從我的網站採用了棱角分明的$ HTTP方法和上面的JSON API庫的實例方法。此示例獲取最新的3篇帖子,其中指定了自定義屬性和值,只返回一些數據(以節省帶寬)。

var baseUrl = http://localhost:3000 // Your Site Url Here 

$http({ 
     method:'GET', 
     url:baseUrl, 
     params:{ 
      json:'get_posts', 
      count:3, 
      include:'excerpt,thumbnail,title,slug,date,categories', 
      meta_key: 'featured', 
      meta_value: 'projects_yes' 
     } 
    }) 
     .success(function(data, status, headers, config){ 
      console.log(data); 
     }) 
     .error(function(data,status,headers,config){ 
      console.log(data,',\n', status, headers, config); 
     }); 
+0

謝謝阿德魯的建議,你能告訴我如何使用ajax庫或如何使它在任何方法中工作?實際上,我對PHP很陌生,wordpress只是學習如何連接腳本。更強大! – AbeDupa

+0

編輯包含角度和上述庫的示例。我花了好幾天的時間,並且無法強調這是用ajax調用wordpress數據的最簡單方法。 –