如何使用日期选择器获取日期并通过get传递


How to get date with datepicker and pass via GET?

我在通过GET url传递日期参数时遇到问题。我从两个带有日期选择器的输入字段中提取数据。这是代码:

$(".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>

当我从日期选择器中选择日期时,图像显示。但如果我不使用日期选择器选择日期,那么什么都不会显示。也许我可以检查是否未选择日期,然后将值设置为空字符串。类似这样的东西:

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();
?>

试试这个。

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