Guys this is driving me crazy. Everyone closing my question saying its been asked but I am unable to get it correct as I am new to ajax and php.
I am making a ajax call on drop down select event :
$('#username').change(function () {
var user = $('#username').val();
$.ajax({
type: 'POST',
url: 'sessionvar.php',
data: {username: user},
success: function (msg) {
alert('Success');
}
});
});
I am getting success alert. But on php page the variable is not getting set.
PHP code is:
<?php
session_start();
if( isset($_POST['username']) ){
$_SESSION['cellValue'] = $_POST['username'];
}else{
$_SESSION['cellValue'] = "good";
}
echo $_SESSION['cellValue'];
?>
Code is always printing value good.
My whole html code:
<!DOCTYPE html>
<html>
<head>
<title>FusionCharts Column 2D Sample</title>
</head>
<body>
<?php
session_start();
?>
<div>
// This will echo a select with name username. and when a value is
// selected the below ajax call is made.
<?php
require("userList.php");
?>
</div>
<button type="button">Click Me</button>
<div id="chart-container">LOADING....</div>
<script src="js/jquery-2.2.4.js"/>
<script src="js/fusioncharts.js"/>
<script src="js/fusioncharts.charts.js"/>
<script src="js/themes/fusioncharts.theme.zune.js"/>
<script type="text/javascript">
$(document).ready(function(){
$('#username').change(function () {
var user = $('#username').val();
$.ajax({
type: 'POST',
url: 'sessionvar.php',
data: {username: user},
success: function (msg) {
alert(msg);
}
});
});
});
</script>
</body>
</html>