Need help with one line of code

Discussion in 'PHP' started by ccxll4e, Mar 29, 2011.

  1. #1
    Hey guys on my site (myroommatesucks.org) I am trying to change the favorite option. Currently we have it as a star when things are unfavorited (upper right corner of the story box) then it changes to "Added to favorites" when you click the star.
    I am trying to make it so when you click the star all it does it change from one image to another and I am not sure how to do that. I am not the coder, but I believe I found the line to change

    echo json_encode("Added to favorites");

    Is that the correct line? If so what would i need to change it to for it to produce the image which is located images/box-faved.jpg

    Here is the code in which the current star is located
    <a class="favoritebtn" href="#favorite-<?=$msg['msg_id']?>"><img src="images/box-fav.jpg" alt="Add to my favorites" width="16" height="16" border="0" align="absmiddle" /></a>
     
    ccxll4e, Mar 29, 2011 IP
  2. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #2
    Yes, that is indeed the correct line to change.
    If you'd take a look at your javascript file, you'd be able to see that the click function calls up the AJAX request function, which then replaces the current content with the content gathered from the requested page.
    $(targetElement).html(data);
    Code (markup):
    Changing
    echo json_encode("Added to favorites");
    Code (markup):
    To
    echo json_encode("<img src=\"images/images/box-faved.jpg\" alt=\"Added to favorites\" width=\"16\" height=\"16\" border=\"0\" align=\"absmiddle\" />");
    Code (markup):
    Should work!
     
    Last edited: Mar 29, 2011
    Sky AK47, Mar 29, 2011 IP
  3. ccxll4e

    ccxll4e Member

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #3
    Thank you that did work. However it doesn't stay. Meaning after you refresh the page it just goes back to the empty star. I would like it to stay filled in after they favorite it, any idea on how to do that?



    // Check if user already favorited this story

    if($user->is_favorite($msg_id)) die(json_encode("Story is already in your favorite list."));


    That's the coding for the already favorited list
     
    ccxll4e, Mar 29, 2011 IP
  4. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #4
    For that you'd need to add some code above;
    <a class="favoritebtn" href="#favorite-<?=$msg['msg_id']?>"><img src="images/box-fav.jpg" alt="Add to my favorites" width="16" height="16" border="0" align="absmiddle" /></a>
    Code (markup):
    Replace this ^ with;
    <a class="favoritebtn" href="#favorite-<?=$msg['msg_id']?>"><img src="images/<?=$imgFav?>.jpg" alt="Add to my favorites" width="16" height="16" border="0" align="absmiddle" /></a>
    Code (markup):
    Place this above it:
    if($user->is_favorite($msg_id)){ $imgFav = "box-faved";
    }else{ $imgFav = "box-fav"; }
    Code (markup):
    This might however not work as I do not know what variables are passed in the file, as I assume it's a completely different file.
     
    Sky AK47, Mar 29, 2011 IP