How to get the value of the selected option in php
<select id="select" name="select">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
<option value="option4">Option 4</option>
</select>
PHP is a server-side language. So without submitting anything to the server, you can't find what option has been selected. However, if you want to find it with JavaScript or the jQuery library, you could do something like this:
Using vanilla JavaScript:
document.getElementById("select").onchange = function() {
// find what option the user changed to!
var option = document.getElementById("select").value;
// alert with the option
alert("Select changed to: " + option);
};
Using jQuery library:
// uses jQuery's .change() for a <select> element
$("#select").change(function() {
// find what option the user changed to!
var option = $("#select option:selected").text();
// alert with the option
alert("Select changed to: " + option);
});
You have to make a php file which creates a database table( mysql or mariadb can be used) then in that table you have to redirect the value of form by assigning the php file to the action button and then the value of that option will get restored in that database. I will prefer to use phpmyadmin, you can create database in it without code and can link it to your file. But if you want to really learn this then i will suggest that first learn php and mysql from a decent website like w3schools because if you simply copy the code than you will not understand anything.
PHP is serverside programming language and it can get values from post , get and cookies and ... . you have to submit form to send values through GET or ... . Best way to do this is using ajax .
`$('#select').on('change',function(){
$.ajax(
/* your code here */
);
}).