Reputation:
I have a <select>
element with 3 options within it and also have 3 PHP functions. How can I set it so that when each option is pressed a different PHP function is called? By my understanding, the select options are client side and the functions are server side. Does this mean that AJAX is necessary? Is there a way to do this without AJAX?
<select name="mySelect">
<option value="filter-oa" disabled="disabled" selected>Filter by Open Access</option>
<option value="full">Fully Open Access</option>
<option value="offers">Offers Open Access</option>
<option value="none">No Open Access</option>
</select>
My 3 functions are: getProOA(), getProFullOA(), and getProNoOA()
Example: User selects the "Fully Open Access" option within the drop-down menu and the getProFullOA()
function is called and lists the products on the page.
Upvotes: 1
Views: 3141
Reputation: 11
<select name="mySelect">
<option value="filter-oa" disabled="disabled" selected>Filter by Open Access</option>
<option value="full">Fully Open Access</option>
<option value="offers">Offers Open Access</option>
<option value="none">No Open Access</option>
</select>
Here try to use a simple ajax call:
$(select[name='mySelect']).on('change', function(){
let val = $('select').find('option[value="' + this.value + '"]');
})
let dataString = "callFunction=" + val
$.ajax({
url: "Your_php_file.php",
type: POST,
data: dataString
}).done(function(result) {
$('.where-you-want-the-output').html(result);
});
Just add this to your code!
https://api.jquery.com/jquery.ajax/
Upvotes: 1
Reputation: 1380
For this to work you'll need to post the form to the server:
<?php
if (!empty($_POST['mySelect'])) {
// this block is only executed if values are received
switch ($_POST['mySelect']) {
case 'filter-oa':
getProOA();
break;
case 'full':
getProFullOA();
break;
case 'none':
getProNoOA();
break;
case 'offers':
echo 'some offer';
break;
default:
echo 'ERROR';
}
die();
}
?>
<form method="POST">
<select name="mySelect">
<option value="filter-oa" disabled="disabled" selected>Filter by Open Access</option>
<option value="full">Fully Open Access</option>
<option value="offers">Offers Open Access</option>
<option value="none">No Open Access</option>
</select>
<button type="submit">
</form>
Upvotes: 0