简体   繁体   中英

Jquery AJAX on click link then reload page

I reviewed other post and some helped me but as I am not proficient in Jquery or AJAX, I want to make sure I am doing what I need correctly.

On my main page (base url), I have 2 different outputs based on the view that a user wants to see. I have a toggle button on the screen and a link on either side of the button. Currently when a link is clicked, my PHP is called, sets a session variable with the selected view and returns the specified view. This changes my URL, ie www.mysite.com/view/A and clicking the other toggle link will change the URL to ww.mysite.com/view/B.

My issue is that I am using pagination and I need to specify the correct URL segment for proper page navigation but to normal user navigation, if they leave the main page and then come back, the correct view will be displayed (as the selected view is in the session variable) but the URL could be www.mysite.com, etc. I do not want to write a bunch of logic to determine if the page is the base URL or ww.mysite.com/view/A, etc.

The below function currently changes my toggle button (from left to right) based on which view was selected/clicked. I want to add logic to this function to call my PHP code which would set the session variable without changing the URL, then refresh the page to show the selected view. I know this is simple and I have a similar example which I found online but I want to make sure I am doing this right.

<script>
    $('#toggle-control a').click(function(){
        $(this).next('ul').slideToggle('500');
        $(this).find('i').toggleClass('fa-bars fa-arrow-left');
    });
    </script>

Ok, so I'm not sure I follow and I apologize about that though you could be a little more clear. From what I grasp you want to accomplish two tasks: (1) you want to figure how to post a session variable to your PHP using jQuery AJAX and (2) you want to make sure your toggle changes position based on the view.

K, so on (1), the following enables you to pass in your script a session variable.

var sessionVar = 1;
var data = {};
data.sessionVariable = sessionVar;

          jQuery.ajax({
                type: "POST",
                url: YOUR PHP FILE HERE
                data: data,
                success: function(data) {
                },
            });       

In your PHP:

if ($_POST) {
    $sessionVar  = $_POST['sessionVariable'];

I would recommend using jQuery's .css() to control how the toggle button appears and where using the logic you have in place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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