2013-03-14 60 views
0

正在開發房地產網站,並在您搜索我們的數據庫中的財產的頁面中,我使它成爲打印表中的結果,但現在我想要更多的結果設計的方式。我使用此代碼生成XML我想打印使用javascript的mysql查詢的結果

<?php function parseToXML($htmlStr) 
    { 
     $xmlStr=str_replace('<','&lt;',$htmlStr); 
     $xmlStr=str_replace('>','&gt;',$xmlStr); 
     $xmlStr=str_replace('"','&quot;',$xmlStr); 
     $xmlStr=str_replace("'",'&#39;',$xmlStr); 
     $xmlStr=str_replace("&",'&amp;',$xmlStr); 
    return $xmlStr; 
    } 
// Opens a connection to a MySQL server 
$connection=mysql_connect ('localhost', 'root', ''); 
if (!$connection) { 
die('Not connected : ' . mysql_error()); 
    } 

// Set the active MySQL database 
$db_selected = mysql_select_db('realestate', $connection); 
if (!$db_selected) { 
die ('Can\'t use db : ' . mysql_error()); 
} 

// Select all the rows in the markers table 
    $locality = htmlentities($_POST['locality']); 

$sellorrent = htmlentities($_POST['sell']); 
$price = htmlentities($_POST['price']); 





$type = htmlentities($_POST['ptype']); 



    $query = "SELECT * FROM `property` WHERE locality= '".$locality."' and sellorrent='".$sellorrent."' and type='".$type."' and price<= '".$price."' "; 



$result = mysql_query($query, $connection); 


$row = mysql_fetch_array($result); 

    header("Content-type: text/xml"); 

    // Start XML file, echo parent node 
    echo '<properties>'; 

// Iterate through the rows, printing XML nodes for each 

    while ($row = @mysql_fetch_assoc($result)){ 



    // ADD TO XML DOCUMENT NODE 







    echo '<property '; 
    echo 'id="' . parseToXML($row['id']) . '" '; 
    echo 'seller_id="' . parseToXML($row['seller_id']) . '" '; 
    echo 'bedrooms="' . parseToXML($row['bedrooms']) . '" '; 
    echo 'year="' . parseToXML($row['year']) . '" '; 
    echo 'locality="' .parseToXML($row['locality']) . '" '; 
    echo 'type="' .parseToXML($row['type']) . '" '; 
    echo 'price="' .parseToXML($row['price']) . '" '; 
    echo 'sellorrent="' .parseToXML($row['sellorrent']) . '" '; 


    echo '/>'; 
} 

// End XML file 
echo '</properties>'; 



?> 

xml文件的工作berfect並得到從形式 頁面的搜索結果,現在我創造這個網頁使用JavaScript來出把結果從XML

<!DOCTYPE html > 

<head> 

<script type="text/javascript"> 


function load() { 
downloadUrl("searchxml.php", function(data) { 
    var xml = data.responseXML; 
    var properties= xml.documentElement.getElementsByTagName("property"); 
    for (var i = 0; i < properties.length; i++) { 
     var id = properties[i].getAttribute("locality"); 
     var address = properties[i].getAttribute("price"); 


     var bedrooms = properties[i].getAttribute("bedrooms"); 
     var seller_id = properties[i].getAttribute("seller_id"); 

     var property_id = properties[i].getAttribute("id"); 
      var type = properties[i].getAttribute("type"); 
     var year = properties[i].getAttribute("year"); 
     var sell = properties[i].getAttribute("sellorrent"); 
     document.write(type + " for " + sell + " in " + id + "</h3><br>"); 

     } 
    }); 
    } 
    function downloadUrl(url, callback) { 
    var request = window.ActiveXObject ? 
     new ActiveXObject('Microsoft.XMLHTTP') : 
     new XMLHttpRequest; 

    request.onreadystatechange = function() { 
    if (request.readyState == 4) { 
     request.onreadystatechange = doNothing; 
     callback(request, request.status); 
    } 
    }; 

    request.open('GET', url, true); 
    request.send(null); 
} 

function doNothing() {} 

     </script> 
</head> 

<body onload="load()"> 

     </body> 
     </html> 

但沒有顯示我

+0

爲什麼你不使用像** jQuery **或** Prototype **這樣的Ajax庫來幫助你處理'XMLHttpRequest'和DOM操作...... [jQuery](http://jquery.com /) – JoDev 2013-03-14 08:41:33

+0

如何使用它們?給我舉個例子或者hent – 2013-03-17 20:22:52

回答

0

要回答你的問題,只是讀jQuery ajax doc ... 但對於一個簡單的答案,它會被somethink像

$(document).ready(function() { 
    $.ajax({ 
     type:'POST', 
     url:"searchxml.php", 
     data : {}, //you can add posted data, in an inline Javascript array form 
     success : function(response) { 
      var xml = response; 
      var properties= xml.documentElement.getElementsByTagName("property"); 
      for (var i = 0; i < properties.length; i++) { 
       var id = properties[i].getAttribute("locality"); 
       var address = properties[i].getAttribute("price"); 

       var bedrooms = properties[i].getAttribute("bedrooms"); 
       var seller_id = properties[i].getAttribute("seller_id"); 

       var property_id = properties[i].getAttribute("id"); 
       var type = properties[i].getAttribute("type"); 
       var year = properties[i].getAttribute("year"); 
       var sell = properties[i].getAttribute("sellorrent"); 

       $('body').append($('<h3/>').text(type + " for " + sell + " in " + id + "<br />")); 
     } 
    }); 

});