开发者

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

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开发者_C百科 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.

The PHP you put on the page is server side, it will load first, then the javascript will run client-side.

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.


About the 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...

About the javascript:

I´m not familiar with the simpleImageCheck() function you are calling, but does it have an onClick or onChange event handler? Otherwise I don´t see your code being run at all.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜