jquery AJAX requests not updating php variable

前端 未结 1 1998
我寻月下人不归
我寻月下人不归 2021-01-29 02:31

I have a comics website, Hitting Trees with Sticks, that allows a user to get next, previous, or random comic id by pressing next or simply pressing arrow keys.

Since im

相关标签:
1条回答
  • 2021-01-29 02:36

    I think the problem with your code is that you are using your Ajax code after the page has already loaded, while trying to change the $_get variables for the initial page load. AFAIK, you need to update the entire page for the "Facebook like" button to change it's ID. Another option would be to use an Iframe. From what I can see, the button's data-href attributes always leads to http://hittingtreeswithsticks.com/ - and that can only be changed by reloading the page using a different attribute.

    If you don't mind loading a page for each picture, this can work out for you:

    <!-- This is the Like button code -->
    <div [...] data-href="http://www.hittingtreeswithsticks.com/<?php echo $_get['id']; ?>"></div>
    

    and the address for this page would be: http://www.hittingtreeswithsticks.com/?id=PAGE_ID

    EDIT

    Using AJAX, you are calling for data from the backend to be used in the client side, without having to reload the entire page. This data can then be used to alter the code in the client side. In your code, the data is being sent back to you but you are not using it at all, that's why it doesn't work:

    $.get('./templates/viewimage.php', { _newImgId : imgIndex },
        function(data) {
            // This is where you should make use of the data received 
        }
    );
    

    EDIT #2

    If you want to dynamically change the Like button's url, take a look at this answer.

    Here is a fiddle of the working example: http://jsfiddle.net/TkFma/4/

    0 讨论(0)
提交回复
热议问题