4

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 3

    1

    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

    Sign up to request clarification or add additional context in comments.

    Comments

    0

    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.

    8 Comments

    i've already tried this solution and there are multiple sulotion to send category id to javascript function , my problem is not with getting category id from current button to java script , my problem is with sending it to destination page
    What do you mean by destination page? when a link is clicked, where are you trying to make the ajax post request to?
    Current page is : index.php?page=home , distenation page is :index.php?page=quiz
    It is possible the page is loading before the ajax request had a chance to execute or run to completion. I will update my answer, see above.
    i mentioned above that i can send it like this index.php?page=quiz&cat=1 but i dont need to show the cat id for security purpose i need anoher way to send it , Ok i will see it now
    |
    0

    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

    3 Comments

    ok, I think I missunderstood ur question.Do u want a redirect to the quiz page and show the questions with the clicked category?
    of course i need session wait another comment to describe index page
    my index page is page showing php page according to that variable in the $_REQUEST var in the url that called 'page' i have tried your solution but may be ajax cannot be loaded before, and the page still with value = home, yes i need to redirect to quiz page but only by index.php?page=quiz and only by index.php not by making another page like quiz.php or something

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.