2014-01-21 87 views
0

我有一個頁面,我想每隔一分鐘刷新一次div,而不刷新整個page.Div從一個php文件中獲取數據,他們在另一個xml文件中計算更大的價格。我讀了最好的方法是ajax請求。ajax請求響應虛假數據

我的Ajax代碼:

<script type="text/javascript"> 
$(function(){ 
var name = $("#product").val(); 
    $.ajax({ 
    type: "GET", 
    url: "maxPrice.php", 
    dataType: "text", 
    data: {'product_name' : name}, 
    error: function(XMLHttpRequest, textStatus, errorThrown) { 
    alert(textStatus); 
    }, 
    success:function(result) { 
     alert(result); 
    } 
    }); 
}); 
</script> 

php文件:

<?php 
$product_name = $_GET['product_name']; 

    $loop = 0; 
    $auction_price_db = 0; 
    $id_product = id_from_product_name($product_name); 
    $doc = new DOMDocument(); 
    $doc->load('C:/wamp/www/store/connections/auctions.xml'); 
    $xpath = new DOMXPath($doc); 

    $xml_offers = simplexml_load_file('C:/wamp/www/store/connections/offers.xml'); 
    $xml_auctions = simplexml_load_file('C:/wamp/www/store/connections/auctions.xml'); 

    foreach ($xml_auctions->auction as $auction){ 
     if ($auction->auction_id == $id_product) { 
      $auction_price_db = (string)$auction->start_price; 
      $item = $loop; 
      break; 
     } 
     $loop++; 
    } 

     foreach ($xml_offers->offer as $offer){ 
     if ($offer->id_product == $id_product) { 
      $user_offer = (string)$offer->price; 
      if ($user_offer > $auction_price_db) { 
       $parent = $doc->getElementsByTagName('auction')->item($item); 
       $query = $xpath->query('start_price',$parent); 
       $query->item(0)->nodeValue = $user_offer; 
       $doc->save('C:/wamp/www/store/connections/auctions.xml'); 
       $xml_products = simplexml_load_file('C:/wamp/www/store/connections/products.xml'); 
       foreach ($xml_products->product as $product){ 
         if ($id_product == $product->product_id) { 
          $product->price = $user_offer; 
          $xml_products->asXML('connections/products.xml'); 
         } 
       } 
       $response = "{\"user_offer\":\"".$user_offer."\"}"; 
      } 
     } 
    } 
echo <<<HERE_DOC 
$response 
HERE_DOC; 

?> 

在阿賈克斯的成功,我有一個警惕,如果從阿賈克斯的數據與此right.But它的響應。 。

<br /> 
<font size='1'><table class='xdebug-error xe-fatal-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'> 
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>(!)</span> Fatal error: Call to undefined function id_from_product_name() in C:\wamp\www\store\maxPrice.php on line <i>6</i></th></tr> 
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr> 
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr> 
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>692568</td><td bgcolor='#eeeeec'>{main}()</td><td title='C:\wamp\www\store\maxPrice.php' bgcolor='#eeeeec'>..\maxPrice.php<b>:</b>0</td></tr> 
</table></font> 

爲什麼我得到這個反應呢?如果我嘗試刪除maxPrice所有代碼和回聲只有$ PRODUCT_NAME,我在ajax請求中傳遞它我得到正確的數據。

+3

'未定義功能id_from_product_name()在C:\ WAMP \ WWW \店\ maxPrice.php上線6'似乎成爲問題,所以'$ id_product = id_from_product_name($ product_name);'正在轟炸 – MonkeyZeus

+1

有一個錯誤,你正在發送回調用堆棧。錯誤是在那裏「致命的錯誤:調用未定義的函數id_from_product_name()在C:\ wamp \ www \ store \ maxPrice.php在線..」 – Gjohn

+0

爲什麼?我知道這個函數工作正常.id_from_product_name是一個另一個PHP文件。我必須把它放在相同的? – user3071235

回答

0

這是來自服務器的Xdebug格式錯誤。如果您在第3行仔細觀察,你就會從服務器上看到此錯誤消息:

Fatal error: Call to undefined function id_from_product_name() in C:\wamp\www\store\maxPrice.php on line 6