如何通过datepicker获取日期并通过GET传递?

时间:2012-02-09 01:43:37

标签: php jquery datepicker

我有问题通过GET网址传递日期参数。我从两个输入字段中获取数据,并将datepicker与之关联。 这是代码:

$(".options input[type='submit']").click(function() {
    $('#From').datepicker();
    $('#To').datepicker();
    var $From = $('#From').datepicker('getDate').getDate();
    var $To = $('#To').datepicker('getDate').getDate();
    $('#placeholder').html('<img src="php/jpgraph/example2.php?From=' + $From + '&To=' + $To + '" />');
});

Html页面:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" href="layout.css" type="text/css"/>
        <link rel="stylesheet" href="jquery-ui-1.8.17.custom.css" type="text/css" />
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript" src="jquery-ui.js"></script>
        <script type="text/javascript" src="newjavascript.js"></script>
    </head>
    <body>
        <div id="container">
            <div id="sidemenu">
                <div class="options">
                    Датум од:<input type="text" id="From" name="From" size="10"/><br />
                    Датум до:<input type="text" id="To" name="To" size="10"/>
                </div>   
                <br />    
                <div class="options">
                    <input type="submit" value="Show">
                </div>
            </div>
            <div id="content">
                <div id="placeholder" style="width:600px;height:300px;"></div>
            </div>
        </div><!-- divContainer --> 
    </body>
</html>

当我从datepicker图像显示选择日期时。但如果我不选择datepicker的日期,那么什么也没有显示。也许我可以检查是否没有选择日期,然后将值设置为空字符串。像这样:

if ($From == null) {
   $From = '';
}
if ($To == null) {
   $To = '';
}

我尝试了这个,但它不起作用。什么可以解决问题?

编辑:目前,我没有在服务器端处理GET参数,以简化问题,但请看一下:

<?php
require_once ('jpgraph.php');
require_once ('jpgraph_line.php');

$con = mysql_connect("localhost", "user", "pass");
if (!$con) {
    die('Could not connect:' . mysql_error());
}
mysql_select_db("db", $con);
$result = mysql_query("select Temperature from TEMPERATURE");
$niz = array();
while ($row = mysql_fetch_array($result)) {
    $niz[] = $row['Temperature'];
}

$graph = new Graph(600,400);
$graph->SetScale('intint');
$graph->title->set('Title');
$graph->xaxis->title->Set('Day');
$graph->yaxis->title->Set('Temp');
$lineplot=new LinePlot($niz);
$lineplot->SetColor('blue');
$graph->Add($lineplot);
$graph->Stroke();
?>

1 个答案:

答案 0 :(得分:2)

试试这个。

if($('#From').val()){
   //Get the date here
   var fromDate = $('#From').datepicker('getDate');
}

if($('#To').val()){
   //Get the date here
   var toDate = $('#To').datepicker('getDate');
}