How to get date with datepicker and pass via GET?

你。 提交于 2019-12-24 10:46:12

问题


I have problem passing date parameters via GET url. I fetch data from two input fields with datepicker associated to it. Here is code:

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

<!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>

When I choose date from datepicker image shows. But if I don't choose date with datepicker then nothing shows. Maybe I could examine if date is not choosen, then to set values to empty string. Something like this:

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

I tried this but it doesn't work. What could be solution for problem?

EDIT: For now, I don't handle GET params on the server side, to simplify problem, but take a look:

<?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:


Try this.

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

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


来源:https://stackoverflow.com/questions/9204313/how-to-get-date-with-datepicker-and-pass-via-get

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!