【问题标题】:How do I get the date from a datepicker using PHP?如何使用 PHP 从日期选择器中获取日期?
【发布时间】:2015-03-05 10:05:42
【问题描述】:

如何从 jquery UI datepicker 中获取日期(如月份)并将其值保存在 php 中的变量中?

我见过很多类似<div id="calendar"></div> 的东西,我不明白。

谢谢。

这是我的代码

<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Select a Date Range</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
$( "#from" ).datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1,
onClose: function( selectedDate ) {
$( "#to" ).datepicker( "option", "minDate", selectedDate );
var day1 = $(this).datepicker('getDate').getDate();  
var month1 = $(this).datepicker('getDate').getMonth();  
var year1 = $(this).datepicker('getDate').getYear();
}
});
$( "#to" ).datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1,
onClose: function( selectedDate ) {
$( "#from" ).datepicker( "option", "maxDate", selectedDate );
var day2 = $(this).datepicker('getDate').getDate();  
var month2 = $(this).datepicker('getDate').getMonth();  
var year2 = $(this).datepicker('getDate').getYear();
}
});
});
</script>
</head>
<body>
<label for="from"> FROM </label>
<input type="text" id="from" name="from">
<label for="to"> TO </label>
<input type="text" name="username" id="to" name="to">
<input type="submit" name="boton" value="  Submit ">
</body>
</html>

【问题讨论】:

  • 您需要像往常一样发布表单,并根据请求的类型使用$_POST$_GET 检索传递的值。
  • 你刚刚做了什么?
  • 请不要破坏您的帖子

标签: php jquery html variables datepicker


【解决方案1】:

HTML

<input type="text" name="datepicker" value="26-10-15" />

PHP

如果有isset则提交表示提交表单

<?php if($_REQUEST['datepicker'])){    
echo $_POST['datepicker'];    
}   ?>

【讨论】:

  • 如果设置了submit,但没有设置datepicker
【解决方案2】:

您必须将日期选择器应用于输入字段,例如

<input type="text" value="<?php echo date("Y-m-d"); ?>" name="selectedDate"/>

与 PHP 相比,只需检查来自 $_GET$_POST 的值:

$selectedDate = isset($_REQUEST['selectedDate']) ? $_REQUEST['selectedDate'] : null;

你有 HTML:

<input type="text" name="username" id="to" name="to">

为此,您调用 datepicker:

$( "#to" ).datepicker({/* ... */});

现在你需要做的就是:

使用&lt;form action="mySubmitScript.php" method="post"&gt;&lt;/form&gt; 包装您的输入字段,另外添加&lt;input type="submit" value="submit"&gt;

在 PHP 中你可以这样做:

if (isset($_POST['to'])) {
    $to = $_POST['to'];
}

您可以使用$.ajax()jQuery方法提交表单而无需重新加载页面。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多