将选择的日期值从datepicker传递到php [英] Pass selected date values from datepicker to php
问题描述
我的HTML是这样的:
< form name =myFormaction =createevent.phponsubmit =return validateForm()method =get>
< p> Date< input type =textid =mydate/>< / p>
< input type =submitname =submitvalue =Submit/>
< / form>
Javascript:
code> $(function(){
$ .datepicker.setDefaults($。extend($。datepicker.regional []));
$(#mydate)。datepicker ({
onSelect:function(dateText,inst){
var dateAsString = dateText;
alert(dateAsString); //打印出正确的值
alert(mydate。值); //该值与dateText相同
}
});
createevent.php
function CreateEvent()
{
if(isset($ _ GET [ 'submit']))
{
$ mydate = $ _ GET ['mydate'];
echo $ mydate;
}
}
当我提醒(mydate.value)时,它提供正确的值,但是当我在php文件中回复$ mydate时,没有任何显示任何帮助,谢谢!
更新:
谢谢你们!愚蠢的错误,添加名称属性的作品,我有一个追踪任务我也有一个timepicker,它返回像01:21。我的日期返回类似01/01/2013。有没有办法组合这两个,使它成为一个datetime类型,所以我可以添加到我的数据库?非常感谢你!
由于您似乎在 onSelect之后的日期什么也不做
已经触发,唯一错误的是我看到的是你的输入缺少name属性,它不会在 $ _ GET
superglobal
将输入更改为:
< input type =textid =mydatename =mydate/>
它应该可以工作。
如果您计划将日期和时间存储在MySQL数据库中,您可以执行此操作。首先是你的形式:
< form name =myFormaction =createevent.phponsubmit =return validateForm )method =get>
< p> Date< input type =textid =mydatename =mydate/>< / p>
< p>时间< input type =textid =mytimename =mytime/>< / p>
< input type =hiddenid =mydatetimename =mydatetime/>
< input type =submitname =submitvalue =Submit/>
< / form>
然后,在你的 validateForm()
函数,我会添加如下:
var date = $('#mydate')。val(),
time = $('#mytime')。val();
var datetime = [date.replace('/',' - '),time] .join(''); // 01-01-2013 01:21
$('#mydatetime')。val(datetime);
I found similar questions on here but most of the solutions don't work for me. I have a form with a datepicker and I want to pass the selected date to my php file but it doesn't seem to work.
My html is something like this:
<form name="myForm" action="createevent.php" onsubmit="return validateForm()" method="get">
<p>Date<input type="text" id="mydate" /></p>
<input type="submit" name="submit" value="Submit" />
</form>
Javascript:
$(function() {
$.datepicker.setDefaults($.extend($.datepicker.regional[""]));
$("#mydate").datepicker({
onSelect: function(dateText, inst) {
var dateAsString = dateText;
alert (dateAsString); //this prints out the right value.
alert (mydate.value); //this value is the same as dateText
}
});
createevent.php
function CreateEvent()
{
if(isset($_GET['submit']))
{
$mydate=$_GET['mydate'];
echo $mydate;
}
}
When I alert (mydate.value), it gives the correct value. But when I echo $mydate in my php file, it doesn't show anything. Any help is appreciated!
Update:
Thank you guys! I made a silly mistake. Adding "name" attribute works. I have a follow up question. I have a timepicker as well and it returns something like 01:21. My date returns something like 01/01/2013. Is there a way to combine these two and make it a datetime type so I can add to my database? Thank you so much!
Since it seems you're doing nothing with the date after the onSelect
has fired, the only thing wrong I see is that your input is lacking the name attribute, and it won't be visible in the $_GET
superglobal
Change your input to something like:
<input type="text" id="mydate" name="mydate"/>
And it should work.
If you plan to store your date and time in a MySQL database, you could do something like this. First in your form:
<form name="myForm" action="createevent.php" onsubmit="return validateForm()" method="get">
<p>Date<input type="text" id="mydate" name="mydate"/></p>
<p>Time<input type="text" id="mytime" name="mytime"/></p>
<input type="hidden" id="mydatetime" name="mydatetime"/>
<input type="submit" name="submit" value="Submit" />
</form>
Then, in your validateForm()
function, I would add something like this:
var date = $('#mydate').val(),
time = $('#mytime').val();
var datetime = [date.replace('/','-'),time].join(' '); // 01-01-2013 01:21
$('#mydatetime').val(datetime);
这篇关于将选择的日期值从datepicker传递到php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!