简体   繁体   中英

PHP AJAX Session Variable Not Working

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???

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!");
            }
    });

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM