H.H.
H.H.

Reputation: 49

Using Ajax with JQuery .on()

I am trying to combine these two functions but my knowledge of Ajax and JS isn't that strong yet and I don't know how i would go about doing it.

$(document).on('click', '.list_item', function() {
    var indx = $(this).index();
});

$.ajax({
    type: 'POST',
    url: 'NavBar.php',
    data: {'indx': pat_id},
});

basically I want to send the JS variable indx to the php variable pat_id.

When the user clicks on the li it would be received as something like this.

<li class= "list_item" onclick ="<?php $pat_id = $_POST ['indx']; ?>">

this would all be happening inside the same php file: NavBar.php.

Upvotes: 1

Views: 61

Answers (1)

Dhara Parmar
Dhara Parmar

Reputation: 8101

Try this:

    $(document).on('click', '.list_item', function() {
        var indx = $(this).index();
        $.ajax({ // add ajax code here
        type: 'POST',
        url: 'NavBar.php',
        data: {pat_id: indx}, // send parameter like this
        success: function(response) {
               console.log(response);
        }
        });
    });

So in NavBar.php you can access pat_id like this:

echo $_POST['pat_id'];

Upvotes: 2

Related Questions