Index undefined in php when obtaining the past value of AJAX

advertisements

I'm trying to get the value that I passed in AJAX to PHP. It shows an alert that says, "Success!" however it when I try to display the value in PHP, it says undefined index. Also I am passing it in the same page.

Whenever I click a button, it opens a modal and I also passing values from that button to the modal. This is evident in my JS code.

<script>
        $(document).ready(function(){
          $('#editModal').on('show.bs.modal', function (e) {

            var id = $(e.relatedTarget).data('id'); //im trying to pass this value to php, e.g. 5
            var time = $(e.relatedTarget).data('time');
            var name = $(e.relatedTarget).data('name');
            var stat = $(e.relatedTarget).data('stat');
            var desc = $(e.relatedTarget).data('desc');
            alert(id);
            $("#task_id_edit").val(id);
            $("#new-taskID").val(id);

            $("#allotted_time_edit").val(time);
            $("#task_name_edit").val(name);
            $("#task_status_edit").val(stat);
            $("#task_desc_edit").val(desc);
            $("#task_id_ref2").val(id);

           //AJAX CODE HERE
           $(function() {
                $.ajax({
                    type: "POST",
                    url: 'tasks.php?id='<?php $id = isset($_GET['id']) ? $_GET['id'] : ""; echo $id; ?>,
                    data: { "userID" : id },
                    success: function(data)
                    {
                        alert("success!"); //this display
                    }
                });
           });

           }); // line 1131
        });
</script>

PHP CODE:

<?php

   $uid = $_POST['userID'];
   echo $uid." is the value"; 

?>

It keeps getting an error that says, undefined index: userID. I am confused. Please help me how to fix this. Your help will be much appreciated! Thank you!


Echo the number in the javascript string.

Currently you will get:

url:'tasks.php?id='1,

The 1 should be concatenated or inside the quote. Try:

url: 'tasks.php?id=<?php $id = isset($_GET['id']) ? $_GET['id'] : ""; echo $id; ?>',

or take that parameter out since it doesn't appear to be being used.