0

code:

$(document).ready(function(){
    $(".uni_type").click(function(){
        uni_type = this.id;
        location.href = "filter-colleges.php?uni_type="+uni_type;
    });
});

html code:

<ul>
    <li class="uni_type" id="central university">Central University</li>
    <li class="uni_type" id="state university">State University</li>
    <li class="uni_type" id="private university">Private University</li>
    <li class="uni_type" id="deemed university">Deemed University</li>
    <li class="uni_type" id="open university">Open University</li>
    <li class="uni_type" id="autonomus">Autonomus Institute</li>
</ul>

filter-college.php

<?php
    include('conn.php');
    $uni_type = $_GET['id'];
    echo '<script>alert('.$uni_type.')</script>';
?>

In this code I want to get uni_type value from the URL but when I click on any list item I'm moved to next page (i.e filter-colleges.php) and given an alert to $uni_type on filter-colleges.php page, but no value is displayed in the alert box. How can I get value from URL or alert $uni_type?

2 Answers 2

3

You're fetching the wrong information in your $_GET:

<?php
   include('conn.php');
   $uni_type = $_GET['uni_type'];
   echo '<script>alert('.$uni_type.')</script>';

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

Comments

0

Seems you are getting wrong id of <li>.Use $(this).attr('id') to get 'id' attribute of <li>. Follow it -

<script>
    $(document).ready(function(){
        $(".uni_type").click(function(){
            var uni_type = $(this).attr('id');
            location.href = "filter-colleges.php?uni_type="+uni_type;
        });
    });
</script>

And also you have put wrong id ($_GET['id'] instead of $_GET['uni_type']) in your php code. See it -

<?php
   include('conn.php');
   $uni_type = $_GET['uni_type'];
   echo '<script>alert('.$uni_type.')</script>';
?>

And dont keep space in id,class or other attribute of tag. For best practice. It can cause unwanted output.

1 Comment

this.id is going to be the same as attribute id.

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.