I am trying to save the value I get from a drop-down list to a PHP variable. I am currently able to do so but through another page. However, I want to do more than just echoing the variable, I want to use it as a parameter for one of my PHP functions on the same page.
I've tried several other solutions I found online but all of them had a submit button, which I don't want. I just want the variable to change on the same page as soon as the drop-down value is selected.
I also tried replacing 'ClientAjax.php' with my current page 'Month.php' but all I get is the same page inside the '#result' div.
Dropdown menu: (Month.php)
Used to populate my dropdown with values from a database.
<select id="clientselect">
<option selected>ALL</option>
<?php echo getClients(); ?>
</select>
<div id="result"></div>
AJAX: (Month.php)
I post twice since I need to show the default selected value as soon as I refresh.
<script>
$(function(){
var displayclient=$("#clientselect option:selected").text();
$.post('ClientAjax.php',{postclient:displayclient},
function(data){
$("#result").html(data)
});
$("#clientselect").change(function (){
var displayclient=$("#clientselect option:selected").text();
$.post('ClientAjax.php',{postclient:displayclient},
function(data){
$("#result").html(data)
});
})
})
</script>
ClientAjax.php
<?php
$client = $_POST['postclient'];
?>