jquery 不使用 $.post 调用 php

发布于 2024-11-08 21:30:39 字数 1198 浏览 0 评论 0原文

我有一个 jquery,当单击复选框时会调用它。 jquery 执行,但是 PHP 文件没有被调用(至少我不认为它被调用)。

我不能在 PHP 函数中放入“echo”语句,因为 PHP 没有返回任何内容(它只是在 jquery 调用时更新表)+我也不知道它会在哪里回显!

  1. 有人能告诉我为什么 PHP 函数没有被调用吗?
  2. 假设我不能只回显变量,如何“调试”通过 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!

  1. Can someone tell me why the PHP function isn't being called?
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最偏执的依靠 2024-11-15 21:30:39

我发现在使用 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?

面如桃花 2024-11-15 21:30:39

你最好使用:

$.post('<?=site_url('controller/function');?>',{data here})

You better to use:

$.post('<?=site_url('controller/function');?>',{data here})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文