简体   繁体   English

PHP里面的JavaScript功能?

[英]Php inside a javascript function?

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 无论如何,如您从下面的代码中看到的那样,当选中此框时,该URL应该被加载: 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. 用户已选中该框的行的message_id假定要添加到url的末尾,然后加载我的控制器“ messages”和方法“ favourite_checked”,然后传递一个从URL抓取message_id的变量,进行存储然后在变量中将其发送给我的模型,并在mysql查询中使用它。

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. 基本上,我会更新我的消息表的“收藏夹”列,并将其设置为= 1,其中url中的message_id与数据库中消息表中的“ message_id”相匹配。 So yea, where the match is found the "favourite" column in that row is updated to 1. 1 = favourite 0 = not favourite. 是的,在找到匹配项的地方,该行中的“收藏夹”列更新为1。1 =收藏夹0 =不收藏。

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. 我的问题是,当我选中此复选框时,什么也没有发生,没有任何更新,因此我觉得在尝试将id添加到javascript函数中的url时,我一定做错了。

I've tried $(post) also.. nothing happens then also. 我也试过$(post)..

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>

I think your basic problem is some confusion about when the PHP is running vs the javascript. 我认为您的基本问题是,PHP与JavaScript何时运行有些混淆。

The PHP you put on the page is server side, it will load first, then the javascript will run client-side. 您在页面上放置的PHP在服务器端,它将首先加载,然后javascript将在客户端运行。

This part here: 这部分在这里:

$.get('http://mysite.com/messages/favourite_checked'+'<?php foreach ($query as $row):  ?><?php $row['id']; ?><?php endforeach; ?>');

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. 似乎您希望根据所检查的内容使它动态化,但是我看不到该url如何具体显示您要查找的内容。

About the PHP: 关于PHP:

I think you want to replace this: 我认为您想替换为:

<?php $row['id']; ?>    // does nothing

with this: 有了这个:

<?php echo $row['id']; ?>    // echo's the id

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... 尽管我不确定这是否会起作用,因为您在那里的循环会生成一个奇怪的网址,只需添加所有id即可。

About the javascript: 关于javascript:

I´m not familiar with the simpleImageCheck() function you are calling, but does it have an onClick or onChange event handler? 我不熟悉您正在调用的simpleImageCheck()函数,但是它具有onClickonChange事件处理程序吗? Otherwise I don´t see your code being run at all. 否则,我根本看不到您的代码正在运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM