Skip to content

Instantly share code, notes, and snippets.

@dilumdesilva
Created December 16, 2019 22:54
Show Gist options
  • Save dilumdesilva/93c6cca08a3d7451315c29f82acebca0 to your computer and use it in GitHub Desktop.
Save dilumdesilva/93c6cca08a3d7451315c29f82acebca0 to your computer and use it in GitHub Desktop.
How to send a simple ajax call from front end to a php backend (php file) to add a value to the $_SESSION
<?php
if(!isset($_SESSION)) { session_start(); }
$username = $_POST['user_name'];
$_SESSION['usernames'] = $username;
?>
function sendAJAX(name){
$.ajax({
url: './add_to_session.php',
type: 'post',
dataType: 'json',
data: { "user_name": "name" },
success: function (response) {
console.log('Username '+name+'has been added to the session' )
}
});
}
<?php
echo '
$name = 'DilumDeSilva';
<button class="sample-class" id="sample-btn" onclick("sendAJAX($name)")>Click Me</button>
';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment