To get the selected value of a select box without submitting the form in PHP, you can use JavaScript/jQuery.
Here’s an example of how you can achieve this using jQuery:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<div>
<select id="mySelect">
<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>
<button id="myButton">Get Selected Value</button>
<p id="result"></p>
</div>
<script>
// Bind click event to button
$("#myButton").click(function(){
// Get selected value from select box
var selectedValue = $("#mySelect").val();
// Display the selected value
$("#result").text("Selected value: " + selectedValue);
});
</script>
</body>
</html>
When you run this code, you will see a select box with some options and a button.
The JavaScript code binds a click event to the button. When the button is clicked,
it gets the selected value from the select box using jQuery’s .val()
function.
The selected value is then displayed in a paragraph element with the id “result”.
You can customize this code to fit your own needs. Additionally, you can use plain JavaScript instead of jQuery,
although the syntax will be slightly different.