PHP Set $_SESSION variable value according to (Button/Link) click

Viewed 2748

I have the following Code :

<ul>
 <li>
  <a href="index.php?page=quiz" class="cat" >
    <h3>Category 1</h3>
    <p>(explain here)</p>
  </a>
 </li>
 <li>
  <a href="index.php?page=quiz" class="cat" >
    <h3>Category 2</h3>
    <p>(explain here)</p>
  </a>
 </li>
</ul>

What I need is to send the category number to the page index.php?page=quiz via $_POST or anything else without sending it via $_REQUEST like this index.php?page=quiz&cat=1
OR assigning the category number value to a $_SESSION variable according to current clicked link to get the value at destination page index.php?page=quiz.


I have already tried this solution:

  • Via javascript by sending the variable value to the page

    var num=1; /*or put the current clicked category number */
    $.ajax({
            url: 'index.php?page=quiz',
            type: 'post',
            dataType: 'json',
            data: {
                category_no: num,
            }
        })
    

    There seems to be a problem with the sending process and the value of page=quiz is not send to the destination and we are still on the current page where page=home.

    Do you have any helpful ideas?

    NOTE: My problem is not getting the category id from current clicked (button/link) to javascript, but sending it to destination page with any of the ways mentioned above.

  • 3 Answers

    Explaining Solution:
    The problem was there is no exit() and json_encode() as results into my function which cannot be applied inside page index.php code which mean that
    the whole function must be moved to another file

    the problem solved like following:

    my page:

    <ul>
     <li>
      <a  href="" data-no="1" class="cat" >
        <h3>Category 1</h3>
        <p>(explain here)</p>
      </a>
     </li>
     <li>
      <a  href="" data-no="2" class="cat" >
        <h3>Category 2</h3>
        <p>(explain here)</p>
      </a>
     </li>
    </ul>
    

    JavaScript:

    $('.cat').on('click', function (e) {
        // Prevent default action of link click
        e.preventDefault();
        var cat = $(this).data('no');
        var curr='set_var.php';
        $.ajax({
            url: curr,
            type: 'POST',
            dataType: 'json',
            data: {
                category: cat
    
            }
        }).done(function(res) {
                if (res.valid) {
                document.location.href = 'index.php?page=quiz';
            }
            });
    });
    

    set_var.php :

    <?php 
    session_start();
        if(isset($_POST['category'])){  
            $_SESSION['cat']=$_POST['category'];
            $result['valid'] = TRUE;
        }
        echo json_encode($result);
        exit();
     ?>
    

    Thanks for all your answers

    Add the category id as a data attribute like this:

    <ul>
     <li>
      <a data-category-id="1" href="index.php?page=quiz" class="cat" >
        <h3>Category 1</h3>
        <p>(explain here)</p>
      </a>
     </li>
     <li>
      <a  data-category-id="2" href="index.php?page=quiz" class="cat" >
        <h3>Category 2</h3>
        <p>(explain here)</p>
      </a>
     </li>
    </ul>
    

    Then handle the click event like this to make your ajax call (dynamically get the clicked category link's url and it's id):

    // When page loads
    $(document).ready(function()
    {
        // Handle category click event
        $('a.cat').click(function(e)
        {
            // Prevent default action of link click
            e.preventDefault();
    
            // Get the url
            var categoryUrl = $(this).prop('href');
    
            // Get the category id
            var categoryId = $(this).data('category-id');
    
            // Make ajax call
            $.ajax({
                url: categoryUrl,
                type: 'post',
                dataType: 'json',
                data: {
                    category_no: categoryId
                }
            })
            .done(function() {
                location.href = categoryUrl;
            });
        })
    });
    

    This way, when you click the link (default action of going to the url doesn't run). Instead; an ajax POST request is made to clicked url (in your case always index.php?page=quiz) first and when the request completes, then you are redirected to that page.

    If num is your clicked category, I think it should work like:

    var num=1;
    $.ajax({
        url: 'index.php?page=quiz',
        type: 'post',
        dataType: 'json',
        data: {
            num,
        }
    });
    

    And in your 'index.php':

    session_start();
    $_SESSION['catID'] = $_POST['num'];
    

    If this doesnt' work, it would be helpful to see your index.php.

    Edit: Lets try it with another side .. just for testing.

    Create a new php called getCatID.php and change AJAX to:

    var num=1;
    
        $.ajax({
            url: 'getCatID.php',
            type: 'post',
            dataType: 'json',
            data: {
                num,
            }
        });
       window.location = "index.php?page=quiz"
    

    In the getCatID.php:

    <?php
        session_start();
        $_SESSION['catID'] = $_POST['num'];
    ?>
    

    And in your index.php try to

    echo $_SESSION['catID'];
    

    PS: You need to trigger the Ajax Call in a way

    Related