jquery 不使用 $.post 调用 php
我有一个 jquery,当单击复选框时会调用它。 jquery 执行,但是 PHP 文件没有被调用(至少我不认为它被调用)。
我不能在 PHP 函数中放入“echo”语句,因为 PHP 没有返回任何内容(它只是在 jquery 调用时更新表)+我也不知道它会在哪里回显!
- 有人能告诉我为什么 PHP 函数没有被调用吗?
- 假设我不能只回显变量,如何“调试”通过 jquery 以这种方式调用的 PHP 函数?
jquery:
$('#notify_checkbox').click(function(){
if($('#notify_checkbox').is(':checked'))
{
$.post("/upic/update_notify", { checked: "y", email: "<?php echo $this->session->userdata('email');?>" });
$( "#notifyresult" ).html( "<p>Awesome, we'll send you an email!</p>" );
}
else
{
$.post("/upic/update_notify", { checked: "n", email: "<?php echo $this->session->userdata('email');?>" });
$( "#notifyresult" ).html( "<p>Okay, we won't email you.</p>" );
}
});
和 PHP(我正在使用 CodeIgniter 框架):
function update_notify()
{
// Passed through AJAX
$notify = $_POST['checked'];
$email = $_POST['email'];
$this->load->model('musers');
$query = $this->musers->update_user_notify($email, $notify);
}
编辑:在 $.post 中使用完整的 URL (http://.../upic/update_notify)。但仅调用控制器而函数则不然。
I have a jquery that gets called when a checkbox is clicked. The jquery executes, however the PHP file isn't being called (at least I don't think it's being called).
I can't put "echo" statements in the PHP function since nothing is returned from PHP (it just updates a table when called by jquery) + I don't know where it would echo too!
- Can someone tell me why the PHP function isn't being called?
- How do I "debug" a PHP function being called this way through jquery assuming I can't just echo out variables?
The jquery:
$('#notify_checkbox').click(function(){
if($('#notify_checkbox').is(':checked'))
{
$.post("/upic/update_notify", { checked: "y", email: "<?php echo $this->session->userdata('email');?>" });
$( "#notifyresult" ).html( "<p>Awesome, we'll send you an email!</p>" );
}
else
{
$.post("/upic/update_notify", { checked: "n", email: "<?php echo $this->session->userdata('email');?>" });
$( "#notifyresult" ).html( "<p>Okay, we won't email you.</p>" );
}
});
And the PHP (I'm using the CodeIgniter framework):
function update_notify()
{
// Passed through AJAX
$notify = $_POST['checked'];
$email = $_POST['email'];
$this->load->model('musers');
$query = $this->musers->update_user_notify($email, $notify);
}
EDIT: Using the full URL (http://.../upic/update_notify) in the $.post works. But calling just the controller and the function does not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现在使用 CodeIgniter 进行调试时使用 log_message 很有用,请查看此处: http://codeigniter.com /user_guide/general/errors.html
除此之外,您认为为什么没有调用该操作?
I find using log_message useful when doing debugging with CodeIgniter, take a look here: http://codeigniter.com/user_guide/general/errors.html
Apart from that, why do you think that the action is not being called?
你最好使用:
You better to use: