I have tied for hours now to get the values value="oneyearanalysis" and value="oneyearanalysis", of each button when i press them, i tried with ajax. I think i am close but i get no output GetData but i do in my console.log(clikYear);. Hope you can help me
Best regards
Index.php
<div class="container" style="margin-top: 50px;">
<select name="button" id="button">
<option value="">Select...</option>
<option value="oneyearanalysis">1 year analyse</option>
<option value="fiveyearanalysis">5 year analyse</option>
</select>
</div>
$(document).ready(function(){
$("#button").on("change",function() {
var clikYear = $(this).val();
console.log(clikYear);
if (clikYear !== "") {
var getTableCall2 = $.ajax({
url : "GetData.php",
type:"POST",
cache:false,
data:{clikYear:clikYear},
})
}
});
getTableCall2.done(function(data){
console.log(data);
console.log(getTableCall2);
console.log(clikYear);
})
GetData.php
if(isset($_POST['clikYear']))
{
$year= $_POST['clikYear'];
}
$yearOfChoice = $year;
2
Answers
The ID for your Select is "button" but you are using Class Selector instead. Try replacing . with # in front of button
HTML
JQuery
You were quite close to make it work. If you apply the simple recommandations below, you should save yourself that kind of trouble in the future.
You would have noticed the
getTableCall2
variable is declared onselect
change
. But "used" before that when trying to apply the.done()
promise handler.Below, I made no change to you code except the indentation.
The order of execution on document ready is:
change
handler (not executed)class
andid
naming..button
used on a<select>
just can lead to confusion. You can use the name you want. Make it meaningful! I’m sure you do not use the worddoor
when you talk about awindow
in the real life…So here is something that should work:
And to test it, make sure the
GetData.php
outputs something in both cases: