jquery posting data attribute to the next page

后端 未结 1 1192
不知归路
不知归路 2021-01-28 09:16

I am trying to post data stored in a data-attr to the next page, here is my code

First i add a class and data-attr to all anchor tags like this:

jQuery(\         


        
1条回答
  •  再見小時候
    2021-01-28 09:24

    If I am reading this correctly, you are trying to carry some variable from page to page, when you click an anchor tag.

    $.ajax sends the request to another page without moving to the page. $.ajax is used a lot for updating or getting data asynchronously, from other places. Without having to refresh the page.

    You can store the data-attr in a sessionStorage variable.

    $('body').on('click','a',function(e){
        e.preventDefault();
    
        var location = $(this).attr('href');
        var data = $(this).attr('data-wptl_ip');
    
        sessionStorage.setItem('wptl_ip', data);
        location.href= location;
    });
    

    then you can access the data info on the next page with

    var myData = sessionStorage.getItem('wptl_ip');
    

    This is assuming you want to move to the next page, along with the data.

    Posting to a php file to insert in Database:

    $('body').on('click','a',function(e){
        e.preventDefault();
    
        var location = $(this).attr('href');
        var data = $(this).attr('data-wptl_ip');
    
        $.ajax({
            url: 'somePage.php',
            data: {someData: data} 
        }).done(function(response){
            sessionStorage.setItem('wptl_ip', data);
            location.href= location;
        });
    
    
    });
    

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