简体   繁体   English

jQuery AJAX单击链接,然后重新加载页面

[英]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. 我查看了其他帖子,并且对我有所帮助,但是由于我不精通Jquery或AJAX,因此我想确保自己在正确地做自己需要的事情。

On my main page (base url), I have 2 different outputs based on the view that a user wants to see. 在我的主页(基本URL)上,基于用户想要查看的视图,我有2个不同的输出。 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. 当前,当单击链接时,将调用我的PHP,使用选定的视图设置会话变量并返回指定的视图。 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. 这将更改我的URL,即www.mysite.com/view/A,然后单击另一个切换链接会将URL更改为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. 我的问题是我正在使用分页,并且需要为正确的页面导航指定正确的URL段,但对于普通用户导航,如果他们离开主页然后返回,则将显示正确的视图(因为选择的视图是在会话变量中),但URL可能是www.mysite.com等。我不想编写一堆逻辑来确定页面是基本URL还是ww.mysite.com/view/A等。

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. 我想向该函数添加逻辑以调用我的PHP代码,该代码将设置会话变量而不更改URL,然后刷新页面以显示所选视图。 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. 据我所知,您想完成两个任务:(1)您想弄清楚如何使用jQuery AJAX将会话变量发布到PHP,以及(2)您要确保切换基于视图更改位置。

K, so on (1), the following enables you to pass in your script a session variable. K,依此类推(1),以下内容使您可以在脚本中传递会话变量。

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: 在你的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. 我建议使用jQuery的.css()来控制切换按钮的显示方式以及使用适当逻辑的位置。

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

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