When I click the Sort By Date Created
button, the string date
is passed to the function sort()
and alert 1 (see code) prints the string date
.
The string date
is stored in the session in the first if
statement and the alert 2 prints date
. The issue is date
is only being stored temporarily and alert 3 always alerts service
, no matter the type. If I change the order of the if statements, the last if statement's string somehow always is stored in the session.
<?php session_start(); ?>
<html>
<head>
<script>
function sort(type){
alert('<?php echo $_SESSION['sort']; ?>'); ///ALERT 1
if (type == 'date'){
<?php $_SESSION['sort'] = 'date'; ?>
alert('<?php echo $_SESSION['sort']; ?>'); ///ALERT 2
}
else if (type == 'cost'){
<?php $_SESSION['sort'] = 'cost'; ?>
alert('<?php echo $_SESSION['sort']; ?>');
}
else if (type == 'service'){
<?php $_SESSION['sort'] = 'service'; ?>
alert('<?php echo $_SESSION['sort']; ?>');
}
alert('<?php echo $_SESSION['sort']; ?>'); ///ALERT 3
}
</script>
</head>
<body>
<input type="button" onclick="sort('date');" value="Sort By Date Created">
<input type="button" onclick="sort('cost');" value="Sort By Cost">
<input type="button" onclick="sort('service');" value="Sort By Service">
</body>
</html>