0

I am new to using Session variables and have been struggling despite scouring the net to understand.

So basically I have a page (search.php). I load a dynamic data table based on a search form. Once the table is loaded, via AJAX I perform the following:

$.ajax({
    type:'POST',
    url:'/ITSMIS/data/asset/search.php',
    data:HardwareAsset,
    dataType: 'html',
    // When PHP / SQL Query Has Been Executed If Data Is Returned
    success:function(data){
        // Load The Data Table Results
        LoadDataTable(data);
        // Generate Hyperlinks For The Table Rows

        $('#data-table tr').click(function(){       
            var HyperlinkHardwareAssetID = $(this).data('id');
            $.ajax({
                type:'POST',
                url:'/ITSMIS/session.php',
                data: HyperlinkHardwareAssetID,
                success: function(data){
                    alert("great!");
                }
            });
            window.location = $(this).data('href');
        }); 

    }
})

As you can see on Success and on click of a table row record, the idea is I post a variable via AJAX to use later. And redirect the user to the update.php page.

I am expecting to then create a session variable in the session.php page using the POSTED AJAX variable.

session_start();
$_SESSION["HardwareAssetID"] = $_POST["HyperlinkHardwareAssetID"];

I am then expected when the update.php page loads to display the Session variable result on the page.

        $session = $_SERVER['DOCUMENT_ROOT'];
        $session .= "/ITSMIS/session.php";
        include_once($session);

        echo $_SESSION["HardwareAssetID"];

But I only get the following error:

Notice: Undefined index: HyperlinkHardwareAssetID in C:\xampp\htdocs\ITSMIS\session.php on line 3

Implying for some reason the AJAX POST on click of the table row hasnt worked. However the alert with the success is always triggered.

Any ideas???

2 Answers 2

1

To send variables to your php, you have to send an object in data property of $.ajax function, like:

$.ajax({
    data: {
        'var_name': 'value'
    }
});

and, in the PHP file:

$item = $_POST['var_name'];

So, in your code, you should use your $.ajax function this way:

$.ajax({
   type:'POST',
   url:'/ITSMIS/session.php',
   data: {
       'HyperlinkHardwareAssetID': HyperlinkHardwareAssetID
   },
   success: function(data){
       alert("great!");
   }
});

For more information, see jQuery.ajax() Documentation

Sign up to request clarification or add additional context in comments.

3 Comments

thanks, after you shared this info, it made sense why I needed to do this, I have done it everywhere else in my code, however this hasnt resolved my error message, any ideas?
Insert a print_r($_POST) in your PHP file, and in the $.ajax, insert a new data variable, like: data: {'HyperlinkHardwareAssetID': HyperlinkHardwareAssetID, 'hello': 'Hi there!'}. It seems like your HyperlinkHardwareAssetID has an undefined value, and those values doesn't appear in the $_POST variable. If you want to get the tr attribute with jQuery, use $(this).attr('id') instead of $(this).data('id'), and do the same with $(this).data('href')
On closer look at the code, when i try and check what "data" is in success, it says undefined....
1

You need to pass the ajax data param as an object {key:value}

   $.ajax({
            type:'POST',
            url:'/ITSMIS/session.php',
            data: {HyperlinkHardwareAssetID : HyperlinkHardwareAssetID  },
            success: function(data){
                alert("great!");
            }
    });

1 Comment

I thanks for the advice, i noticed i was originally missing this, I have since tried this but still the same error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.