php 如何在表单POST方法中传递jquery datepicker值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13923264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass the jquery datepicker value in form POST method?
提问by Allen
I have a input text in the form for date and i use JQuery datepicker to select date.
我在日期表单中有一个输入文本,我使用 JQuery datepicker 来选择日期。
<input type="text" id="datepicker" name="datepicker" value="Date"/>
when i use form post to post the values into another php for calculation. i don't get the values from this input text alone. i get an empty value instead of selected date.
当我使用表单发布将值发布到另一个 php 进行计算时。我不会单独从这个输入文本中获取值。我得到一个空值而不是选定的日期。
$date=$_POST['datepicker'];
i'm not sure of how to pass the datepicker value in form POST. Can someone help me out?
我不确定如何在表单 POST 中传递 datepicker 值。有人可以帮我吗?
回答by Sinetheta
jQuery's datepicker has nothing to do with posting your form. If your form is set to "post" then whatever is left in that input field after the user has selected a date will be sent with the form.
jQuery 的日期选择器与发布表单无关。如果您的表单设置为“发布”,则用户选择日期后该输入字段中剩余的任何内容都将与表单一起发送。
<form action="process.php" method="post">
<input type="text" id="datepicker" name="datepicker" value="Date"/>
</form>
Gives you:
给你:
$date = $_POST['datepicker'];
However if your form is being sent as a "get" request you'll need to access it as such.
但是,如果您的表单是作为“获取”请求发送的,则您需要这样访问它。
<form action="process.php" method="get">
<input type="text" id="datepicker" name="datepicker" value="Date"/>
</form>
Gives you:
给你:
$date = $_GET['datepicker'];
回答by Eamonn Kenny
$( "#currentDate" ).datepicker("setDate", new Date("<?php echo $_POST['myCurrentDate'] ?>"));
works for me, read at least 10 very complicated ways of doing it and then realised this works. You are instantiating a new value each time. you store the date somewhere in your php scripts, using form input or some other technique and then recycle it back into the above statement. So reloading doesn't affect the result.
对我有用,阅读至少 10 种非常复杂的方法,然后意识到这是可行的。您每次都在实例化一个新值。您将日期存储在 php 脚本中的某处,使用表单输入或其他一些技术,然后将其回收回上述语句。所以重新加载不会影响结果。
If myCurrentDate is unset it will set it by default. Getting it to be set to today's date requires a php if statement to set it the first time (or reload the page) to new Date() and the else statement can be used to include the above code.
如果未设置 myCurrentDate,它将默认设置它。将它设置为今天的日期需要一个 php if 语句将它第一次(或重新加载页面)设置为 new Date() 并且 else 语句可用于包含上述代码。
回答by vorillaz
That's a simple form with a datepicker
这是一个带有日期选择器的简单表单
<form id="myform" name="myform">
<input type="text" id="datepicker" name="datepicker" value="Date"/>
<input type="button" id="submitMe">
</form>
Now your jquery,we are going to use some AJAX stuff:
现在你的 jquery,我们将使用一些 AJAX 的东西:
//init datepicker
$(function() {
$("#datepicker").datepicker({
showButtonPanel: true
});
});
//pass deatepicker to php
$("#submitMe").click(function() {
$.ajax({
type: 'POST',
url: "pass.php",
dataType: "json",
data: $('#myform').serialize(),
success: function(data) {
console.log("Done");
}
});
return false;
});?
And finally your pass.php file
最后你的 pass.php 文件
<?php
//note that date may have slashes or dots so we url decode it
$date = urldecode ($_GET['datepicker']);
echo "chosen date is: ".$date;
?>

