Problem:
When entering two form values (min/max price) does not update presented query results. When first entered the results are correctly presented, but when form values are changed thereafter with new parameters, the html code is not updated to reflect these parameters.
HTML code:
<input type="text" size="5" placeholder="Min. price" id="price_min">
<input type="text" size="5" placeholder="Max. price" id="price_max">
<input type="button" onclick="getHotelInfo()" value="Get" />
<div id="hotel_info"></div>
JavaScript code:
function getHotelInfo() {
$.get('hotelservice.php?priceMin=' + $('input#price_min').val() + '&priceMax=' + $('input#price_max').val(), function(data) {
var hotelInfo = JSON.parse(data);
content = '<table style="font-size:10px;width:100%;"><tr><th>Name</th><th>Stars</th><th>Price</th></tr>';
for (var hotel=0; hotel<5;hotel++)
{
content += '<tr><td><a href="' + hotelInfo[hotel].externalLink + '">' + hotelInfo[hotel].name +'</a></td><td><center>' + hotelInfo[hotel].stars + '</center></td><td><center>' + hotelInfo[hotel].price + '</center></td></tr>';
}
content += '</table>';
$('#hotel_info').replaceWith(content);
});
}
PHP code:
$priceMin = $_GET['priceMin'];
$priceMax = $_GET['priceMax'];
$xml_source = file_get_contents('http://www.kayak.com/h/rss/hotelrss/SE/vaxjo?mc=EUR');
$xml = simplexml_load_string($xml_source);
$result = array();
foreach ($xml->channel->item as $item) {
$kyk = $item->children('http://www.kayak.com/h/rss/hotelextension');
$price = (int)$kyk->price;
if ($price < $priceMax && $price > $priceMin) {
$entry = new stdClass();
$entry->name = (string)$item->title;
$entry->externalLink = (string)$item->link;
$entry->price = $price;
$entry->stars = (int)$kyk->stars;
$result[] = $entry;
}
}
echo json_encode($result);
Desired output: When form values are changed and getHotelInfo() is called, the new results should be reflected.
var hotelInfo = JSON.parse(data);since data may already be an object as jQuery.get guesses the dataType is JSON