PHP里面有一个javascript函数?
我的用户收件箱中有一个功能,允许用户选中/取消选中收件箱中他们想要收藏的邮件。
我目前正在测试当用户选中该框时会发生什么(单击图像并使其从灰色变为彩色,意味着该框被选中)。
无论如何,正如您从下面的代码中看到的那样,当选中该框时,应该加载此网址: http://mysite. com/messages/favourite_checked
用户选中复选框的行的 message_id 应该添加到 url 的末尾,然后加载我的控制器“messages”和方法“favourite_checked”,然后传递一个变量从 url 中获取 message_id,将其存储在变量中,然后将其发送到我的模型,并在 mysql 查询中使用。
基本上,我更新消息表的收藏夹列并将其设置为 = 1,其中 url 中的 message_id 与数据库中的消息表中的消息 ID 匹配。所以是的,如果找到匹配,该行中的“最喜欢”列将更新为 1。1 = 最喜欢 0 = 不最喜欢。
我只是想我会弄清楚发生了什么..
我的问题是当我选中该框时没有任何反应,没有任何更新,所以我觉得我在尝试将 id 添加到 javascript 中的 url 时一定做错了什么功能。
我也尝试过 $(post) ..然后也没有发生任何事情。
也许有人可以发现它,因为我真的不知道问题是什么。
<script type="text/javascript">
// favourite check box
$('input.favourite:checkbox').simpleImageCheck({
image: '<?php echo base_url()?>images/messages/check.png',
imageChecked: '<?php echo base_url()?>images/messages/unchecked.png',
afterCheck: function(isChecked) {
if (isChecked) {
//query to db from php to update favourite number to 1
$.get('http://mysite.com/messages/favourite_checked'+'<?php foreach ($query as $row): ?><?php $row['id']; ?><?php endforeach; ?>');
}
// else (!isChecked)
// {
// //query to db from php to update favourite number to 0
// $.get('http://mysite.com/messages/favourite_unchecked');
// }
}
});
</script>
I have a feature on my users inbox that allows users to check/uncheck messages in their inbox that they want to make favourite.
I'm currently testing what happens when a user checks the box (clicks on the image and causes it to go from greyed out to colour meaning the box is checked).
Anyway as you can see from the code below when the box ischecked this url is suppose to be loaded: http://mysite.com/messages/favourite_checked
The message_id of the row the user has checked the box on is suppose to be added onto the end of the url this then loads my controller "messages" and method "favourite_checked" which then passes a variable that grabs the message_id from the url, stores it in a variable then sends it the my model and it is used in a mysql query.
Basically I update the favourites column of my messages table and set it to = 1 where the message_id from url matches the one in the messages table in my database. So yea, where the match is found the "favourite" column in that row is updated to 1. 1 = favourite 0 = not favourite.
Any I just thought I would make it clear what was happening..
My problem is nothing happens when I check the box, nothing is updated so I feel I must be doing something wrong where I try to add the id to the url in the javascript function.
I've tried $(post) also.. nothing happens then also.
Maybe someone can spot it because I really don't know what the problem is.
<script type="text/javascript">
// favourite check box
$('input.favourite:checkbox').simpleImageCheck({
image: '<?php echo base_url()?>images/messages/check.png',
imageChecked: '<?php echo base_url()?>images/messages/unchecked.png',
afterCheck: function(isChecked) {
if (isChecked) {
//query to db from php to update favourite number to 1
$.get('http://mysite.com/messages/favourite_checked'+'<?php foreach ($query as $row): ?><?php $row['id']; ?><?php endforeach; ?>');
}
// else (!isChecked)
// {
// //query to db from php to update favourite number to 0
// $.get('http://mysite.com/messages/favourite_unchecked');
// }
}
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你的基本问题是对 PHP 与 javascript 何时运行的一些混淆。
您在页面上放置的 PHP 是服务器端的,它将首先加载,然后 javascript 将在客户端运行。
这部分在这里:
似乎您希望根据您检查的内容是动态的,但我不知道该网址将如何具体显示您正在寻找的内容。
I think your basic problem is some confusion about when the PHP is running vs the javascript.
The PHP you put on the page is server side, it will load first, then the javascript will run client-side.
This part here:
Seems like you are wanting this to be dynamic based on what you checked, but I don't see how that url is going to show specifically what you are looking for.
关于PHP:
我想你想用这个替换这个
::
虽然我不确定这是否会起作用,因为你在那里的循环会生成一个奇怪的网址,只需添加所有id即可..
关于 javascript:
我不熟悉您正在调用的
simpleImageCheck()
函数,但它是否有onClick
或>onChange
事件处理程序?否则我根本看不到你的代码正在运行。About the PHP:
I think you want to replace this:
with this:
Although I´m not sure that that will work as the loop you have there will generate a strange url, just adding all id's...
About the javascript:
I´m not familiar with the
simpleImageCheck()
function you are calling, but does it have anonClick
oronChange
event handler? Otherwise I don´t see your code being run at all.