1

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.

1
  • Put the ajax call inside the click? Commented Jun 16, 2016 at 13:08

1 Answer 1

2

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'];
Sign up to request clarification or add additional context in comments.

3 Comments

Just to clarify in the it would look something like this <li onclick = echo $_POST ['pat_id']> and the variable pat_id would have the value of the JS variable? or would I have to write the POST command outside of the li?
$_POST['pat_id'] will contain the indx js variable's value, and yes you can use it in li using $_POST['pat_id']....
it's giving me an error saying that pat_id is not defined but the console reacts with XHR finished loading POST followed by errors similar to: Fatal error: Uncaught exception 'Zend_Acl_Exception' with message 'Resource 'shiprequest_NavBar.php' not found...I don't see why there's a pathing issue when I am sending the post to the same url containing the code

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.