Cyber
Cyber

Reputation: 2704

How to do an Ajax request to parse table row column data to PHP on click of table row?

I have a datatable with an numeric value in the first column of the <tr><td> row.

I want to click on that row and parse this value to a PHP page in order to assign this number into a session.

I have below code snippet, but this isn't do the push.

selected is the class of the row when I click on it.

$(".selected td").click(function(parsegroupid) {
    var group_id = $(this).attr('data');

    $.ajax({
        url: "./includes/indexPage/assign_session.php",
        type: "post",
        data: {
            id: group_id
        },
        success: function(response) {
        }
    });
});

This is my PHP code:

<?php
ob_start();
if (session_id() == '') {
    session_start();
}


$GrpID = $_POST['group_id'];

$_SESSION['grp'] = $GrpID;


ob_end_flush();
?>

Below is a screenshot of the row when selected.enter image description here

Upvotes: 0

Views: 257

Answers (4)

mufax
mufax

Reputation: 81

First, you need to check if the ajax call is made. Based on your previous comments, it seems this is not happening.

You can try to specify the table id in your onclick event:

$("#table_id tbody").on( 'click', 'tr', function (){

    var group_id = $(this).find("td:first-child").text();

    $.ajax({
        url: "./includes/indexPage/assign_session.php",
        type: "post",
        data: {
            id: group_id
        },
        success: function(response) {
        }
    });
}

Upvotes: 2

Ivan G.
Ivan G.

Reputation: 146

If the ID is in the cell of the and not as an attribute, try this:

var group_id = $(this).html();

Upvotes: 1

Nirav Joshi
Nirav Joshi

Reputation: 2960

Go with this code.

You are posting id and group_id is your value and you added $_POST['group_id'] instead of $_POST['id'].

Hope this will helps you :)

<?php
ob_start();
if (session_id() == '') {
    session_start();
}


$GrpID = $_POST['id']; // Change here

$_SESSION['grp'] = $GrpID;


ob_end_flush();
?>

Upvotes: 0

Narayan
Narayan

Reputation: 1668

Make the change in your assign_session.php file your have pass the value in id and trying to access value of group_id.

Instead of $_POST['group_id'] use $_POST['id']

change the code like below

<?php
ob_start();
if (session_id() == '') {
    session_start();
}


$GrpID = $_POST['id']; // change 

$_SESSION['grp'] = $GrpID;


ob_end_flush();
?>

Upvotes: 1

Related Questions