0

I am trying to get an id value of a link with jquery then send that id to a php script (in this case sending it to a php sql query).

I have a link like this on the main page:

<a href="#" id="category1">Category One</a>

when this link is clicked, I would like jquery to grab the id value ('category1') and place it in a seperate php file that holds my db queries.

in other words the id value would be inserted into the query below once the link is clicked so I don't have to manually enter in the category part of the query:

SELECT * FROM maindb WHERE category="category1"

Any help on this would be great, thanks.

1 Answer 1

2
<a class='foo' id='category1'>Category One</a>
<a class='foo' id='category2'>Category Two</a>
<a class='foo' id='category3'>Category Three</a>

<script>
$(document).ready(function() {
    $('.foo').click(function() {
        // You might do:
        window.location='somefile.php?id=' + this.id;
        // or pass it as an argument to 
        $.getJSON('somefile', {id: this.id}, function(result) { alert('Success') });
    });
});
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

If I use the pass argument, is there a way I can get the id in my PHP file? How would I grab it in the PHP file?

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.