我在页面上有一个简单的复选框,允许用户说他们是否想接收电子邮件通知。我使用jquery来调用一些php代码,当复选框发生变化时。然而,即使调用jquery函数,我也没有多少运气(单击checkbox什么也不做),更不用说测试后端的功能了。如果复选框发生变化,请使用jquery调用php
任何帮助指出错误会很好。谢谢。
复选框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() {
{);
在你的PHP,你应该引用您的数组键:'$ _POST ['checked']'和'$ _POST ['email']''。 – 2011-05-19 19:38:25
很好,谢谢。 – zee 2011-05-19 19:53:33