【问题标题】:Getting data from another URL using form input and reading that data使用表单输入从另一个 URL 获取数据并读取该数据
【发布时间】:2017-06-27 09:29:00
【问题描述】:

我正在尝试做一些我认为应该相对简单的事情,但我不确定如何去做,尽管搜索和查看了很多示例,但我仍然不确定如何去做。

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

我遇到的问题是:

如何在表单中选择日期/日期但读取 URL 而不是重定向用户而是刷新页面以回显读取 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>

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

非常感谢,

罗伯

【问题讨论】:

    标签: php jquery datepicker get


    【解决方案1】:
    <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>
    

    【讨论】:

    • 杰克逊,太棒了,非常感谢,我很感动。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多