使用表单输入从该另一个URL获取数据并读取该数据

时间:2017-06-27 09:29:00

标签: php jquery datepicker get

我正在尝试做一些我认为应该相对简单的事情,但我不确定如何做到这一点,尽管经过大量搜索和查看示例,我仍然不知道该怎么做。

我有一个表单,其中包含日期输入字段和日期(7/14)选择器,当用户更改日期或天数时,它应查询单独的URL,然后返回结果。

我遇到的问题是:

如何在表单中选择日期/天但读取网址而不是重定向用户,但刷新页面以回应阅读网址的结果?

我已经汇总了一个简单的例子来说明我的意思:

<head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>$( function() { $( "#datepicker" ).datepicker(); } ); </script>
</head>
<body>

<?php
// Example fully formatted URL 
http://www.angelfishbooking.co.uk/feeds/roomavailability.aspx?id=Belle_Tout&date=2017-06-23&days=7
// http://www.angelfishbooking.co.uk/feeds/roomavailability.aspx?id=Belle_Tout&date=[DATESELECTED]&days=[DAYSSELECTED]
?>

<form action="http://www.angelfishbooking.co.uk/feeds/roomavailability.aspx?id=Belle_Tout&" method="GET">
<input class="datepicker" type="text" id="datepicker" name="date" value="">
<select><option value="7" id="days" name="days">7 days</option><option value="14">14 days</option></select>
<input type="hidden" name="c" value="3" /> 
<input type="submit" /> 
</form>
</body>

<?php
// Read contents of the URL
// dateselected = datepicker output
//$angelfish = file_get_contents('http://www.angelfishbooking.co.uk/feeds/roomavailability.aspx?id=Belle_Tout&date=' . $dateselected . '&days=' . $days);
// Display contents of the URL, i.e.

echo '<hr>';
echo 'Show example output that we are trying to achieve using above 
selection but with defaults of date today and days of 7.';
echo '<br /><br />';
$dateselected = date('Y-m-d');
$daysselected = '7';
$angelfish = file_get_contents('http://www.angelfishbooking.co.uk/feeds/roomavailability.aspx?id=Belle_Tout&date=' . $dateselected . '&days=' . $daysselected);
echo $angelfish;

//At this point we'll format nicely with coloured rows, etc
?>
</body>

有人能指出我正确的方向吗?

非常感谢,

罗布

1 个答案:

答案 0 :(得分:2)

<pre>Please use below code <?php
$dateselected = date('Y-m-d');
$daysselected = '7';
if(isset($_POST['date']) && isset($_POST['days'])){
    $dateselected=date('Y-m-d',strtotime($_POST['date']));
    $daysselected = $_POST['days'];
}
?>
<head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>$( function() { $( "#datepicker" ).datepicker(); } ); </script>
</head>
<body>

<?php
// Example fully formatted URL 
http://www.angelfishbooking.co.uk/feeds/roomavailability.aspx?id=Belle_Tout&date=2017-06-23&days=7
// http://www.angelfishbooking.co.uk/feeds/roomavailability.aspx?id=Belle_Tout&date=[DATESELECTED]&days=[DAYSSELECTED]
?>

<form name="form1" method="POST">
<input class="datepicker" type="text" id="datepicker" name="date" value="">
<select name="days"><option value="7" id="days" name="days">7 days</option><option value="14">14 days</option></select>
<input type="hidden" name="c" value="3" /> 
<input type="submit" /> 
</form>
</body>

<?php
// Read contents of the URL
// dateselected = datepicker output
//$angelfish = file_get_contents('http://www.angelfishbooking.co.uk/feeds/roomavailability.aspx?id=Belle_Tout&date=' . $dateselected . '&days=' . $days);
// Display contents of the URL, i.e.

echo '<hr>';
echo 'Show example output that we are trying to achieve using above 
selection but with defaults of date today and days of 7.';
echo '<br /><br />';
echo 'http://www.angelfishbooking.co.uk/feeds/roomavailability.aspx?id=Belle_Tout&date=' . $dateselected . '&days=' . $daysselected."<bR>";
 $angelfish = file_get_contents('http://www.angelfishbooking.co.uk/feeds/roomavailability.aspx?id=Belle_Tout&date=' . $dateselected . '&days=' . $daysselected);
echo $angelfish;

//At this point we'll format nicely with coloured rows, etc
?>
</body></pre>