My HTML code creates select options
<select name ="sortBy" id="sortBy" onchange="getValue(this)">
<option value=""></option>
<option value="total">Total Medals</option>
<option value="gold">Gold Medal</option>
<option value="silver">Silver Medal</option>
<option value="bronze">Bronze Medal</option>
</select>
My attempt at using Ajax to read the value from the select box
function getValue(obj){
$("#sortBy").on( 'click', function () {
$.ajax({
type: 'post',
url: 'main.php',
data: { source1: obj.value },
success: function( data ) {
console.log( data );
}
});
});
}
My PHP code that creates a variable
$sortBy = $_POST['source1'];
echo $sortBy;
However I get an error message saying source1 is an undefined index in the PHP code. I assume it isn't getting the variable from JS.
All of these are on the same document (main.php). I'd like the PHP variable to changed depending on the variable gotten in JS. How can I fix this? Thanks How can I fix this?