0

我正在开发房地产网站,并且在您在我们的数据库中搜索房产的页面中,我在将结果打印到表格之前制作了它,但现在我希望以更设计的方式获得结果。我使用此代码生成 xml

<?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 文件完美运行并从表单页面获取结果现在我使用 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>

但什么都没有显示给我

4

1 回答 1

0

要回答您的问题,只需阅读jQuery ajax doc ...但对于简短的回答,它会有点像

$(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 />"));
        }
    });

});
于 2013-03-18T08:40:44.633 回答