将变量从 javascript 传递到控制器 Codeigniter 时出现问题
我正在为此苦苦挣扎,但似乎无法让它发挥作用。
需要将当前用户日期(cdate)变量传递到我的控制器,尽管我的警报窗口显示正确的值,但该值永远不会到达控制器。
这是 javascript 代码:
$(document).ready(function() {
$('#submit').click(function() {
var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)
year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()+1
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
if (month<10)
month="0"+month
var hours=mydate.getHours()
var minutes=mydate.getMinutes()
var seconds=mydate.getSeconds()
var cdate=+month+"/"+daym+"/"+year
$.post('user/available', {curdate: cdate});
alert(cdate);
});
});
Controller:
$curdate=$this->input->post('curdate');
View:
echo form_open('user/available');
echo form_input('dateav','',$dateav);
input type="image" src="echo base_url();images/send.png" id="submit" alt="Submit button"
echo form_close();
谁能告诉我我做错了什么?
I'm struggling with this and just can't seem to make it work.
Need to pass the current users date (cdate) variable to my controller and in spite my alert windows shows the correct value, that value never reaches the controller.
Here's the javascript code:
$(document).ready(function() {
$('#submit').click(function() {
var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)
year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()+1
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
if (month<10)
month="0"+month
var hours=mydate.getHours()
var minutes=mydate.getMinutes()
var seconds=mydate.getSeconds()
var cdate=+month+"/"+daym+"/"+year
$.post('user/available', {curdate: cdate});
alert(cdate);
});
});
Controller:
$curdate=$this->input->post('curdate');
View:
echo form_open('user/available');
echo form_input('dateav','',$dateav);
input type="image" src="echo base_url();images/send.png" id="submit" alt="Submit button"
echo form_close();
Can anyone tell me what i'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议将整个请求转储到 stdout 或其他东西,这样您就可以看到使用了什么 URL 以及所有其他参数。也许配置中有错字。
[编辑] 该函数应该
返回 false
,否则会发生坏事(TM)(例如表单将被发布两次等)。如果没有任何反应,但出现了警报,则
post()
调用失败。 阅读文档,尤其是关于使用 .ajaxError()I suggest to dump the whole request to stdout or something so you can see what URL is used and all the other parameters. Maybe there is a typo in the config.
[EDIT] The function should
return false
, otherwise Bad Things(TM) will happen (like the form will be posted twice and such).If nothing happens but the alert showing up, then the
post()
call fails. Read the documentation and especially the part about error handling with .ajaxError()