如果checkbox发生变化,使用jquery调用php

发布于 2024-11-09 01:07:44 字数 1264 浏览 0 评论 0原文

我在页面上有一个简单的复选框,允许用户说出他们是否愿意接收电子邮件通知。我使用 jquery 在复选框更改时调用一些 php 代码。然而,我什至调用 jquery 函数(单击复选框不执行任何操作)都没有太多运气,更不用说测试后端功能了。

任何指出错误的帮助都会很棒。谢谢。

复选框 HTML:

<input id="notify_checkbox" type="checkbox" value="y" name="notify">

jquery:

$('#notify_checkbox').change(function(){

    if($('#notify_checkbox').attr('checked'))
    {
        $.post("/update_notify", { checked: "y", email: "<?php echo $this->session->userdata('email');?>" });
        $( "#notifyresult" ).html( "<p>Awesome, we'll send you an email!</p>" );
    }
    else
    {
        $.post("/update_notify", { checked: "n", email: "<?php echo $this->session->userdata('email');?>" });
        $( "#notifyresult" ).html( "<p>Okay, we won't email you.</p>" );
    }
});

最后是 PHP:

function update_notify()
{
    // Passed through AJAX
    $notify = $_POST[checked];
    $email = $_POST[email];

    $this->load->model('musers');
    $query = $this->musers->update_user_notify($email, $notify);
}

解决方案: 下面的评论很有帮助,但不是最终的解决方案。解决方案是在我的代码周围添加以下内容。

$(document).ready(function() {
{);

I have a simple checkbox on a page that allows a user to say if they'd like to receive email notifications. I am using jquery for this to call some php code when the checkbox changes. However, I am not having much luck even calling the jquery function (clicking the checkbox does nothing) let alone test the backend functionality.

Any help in pointing out the error would be great. Thanks.

The checkbox HTML:

<input id="notify_checkbox" type="checkbox" value="y" name="notify">

The jquery:

$('#notify_checkbox').change(function(){

    if($('#notify_checkbox').attr('checked'))
    {
        $.post("/update_notify", { checked: "y", email: "<?php echo $this->session->userdata('email');?>" });
        $( "#notifyresult" ).html( "<p>Awesome, we'll send you an email!</p>" );
    }
    else
    {
        $.post("/update_notify", { checked: "n", email: "<?php echo $this->session->userdata('email');?>" });
        $( "#notifyresult" ).html( "<p>Okay, we won't email you.</p>" );
    }
});

And finally the PHP:

function update_notify()
{
    // Passed through AJAX
    $notify = $_POST[checked];
    $email = $_POST[email];

    $this->load->model('musers');
    $query = $this->musers->update_user_notify($email, $notify);
}

RESOLUTION: The comments below were helpful but not the ultimate solution. The solution was to add the following around my code.

$(document).ready(function() {
{);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

無心 2024-11-16 01:07:44

为什么不使用 .click() 来代替呢?

JSFIDDLE

另外,正如您在我的 JSFiddle 示例中看到的,使用 .is(': check') 而不是 attr('checked')

@Rocket 对您的帖子发表评论后进行编辑

您确实应该引用您的 $_POST php 中的 值!我自己没有注意到,归功于 rocket

Why not use .click() instead?

JSFIDDLE

Also, as you can see in my JSFiddle example, use .is(':checked') instead of attr('checked').

edit after @Rocket commented on your post:

You should indeed quote your $_POST values in your php! Didn't notice it myself, credits to rocket

温柔女人霸气范 2024-11-16 01:07:44

你的控制器叫什么名字?您需要将其放入 URL 中。

 $.post("/controller/update_notify", ...

What's the name of your controller? You need to put that in the URL.

 $.post("/controller/update_notify", ...
鹿港小镇 2024-11-16 01:07:44

问题在于 jQuery 1.6 中对 attr 函数的重新定义,以及属性和属性之间的差异。

对于属性(通过 attr 检索),checked= 的值“checked” 或其缺失保持不变,无论元素是否实际被检查。

使用属性(从 jQuery 1.6 开始使用 prop 检索),实际状态找到该元素。这相当于检查元素的 checked 属性(这是更好的选择,因为您不需要执行新的 jQuery 选择)。最好的解决方案如下:

if (this.checked) {

请参阅 jsFiddles 显示此内容:

The problem is with the redefinition of the attr function in jQuery 1.6, and with the difference between attributes and properties.

With attributes (retrieved with attr), the value of checked="checked" or its absence stays the same, regardless of whether the element is actually checked or not.

With properties (retrieved with prop as of jQuery 1.6), the actual state of the element is found. This is equivalent to checking the checked property of the element (which is preferable because you don't need to do a new jQuery selection). The best soltion would be as follows:

if (this.checked) {

See jsFiddles showing this:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文