2013-03-14 47 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 />")); 
     } 
    }); 

});