Reputation:
How would I make a drop down menu post automatically when an option has been chosen?
<SELECT NAME="select_page"><?php echo $options1;?></SELECT>
What is the best way to achieve this?
Upvotes: 1
Views: 15425
Reputation: 3062
Note that jQuery is referencing the <select>
tag by its ID, so put one in the tag. Also, you can use AJAX to post your data if you wish to receive something back.
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#Sel').change(function() {
var opt = $(this).val();
$.ajax({
type: "POST",
url: "receiving_file.php",
data: 'selected_opt=' + opt,
success:function(data){
alert('This was sent back: ' + data);
}
});
});
});
</script>
</head>
<body>
<select id = "Sel">
<option value ="Song1">default value<br>
<option value ="Song2">Break on through<br>
<option value ="Song3">Time<br>
<option value ="Song4">Money<br>
<option value ="Song5">Saucerful of Secrets
</select>
Upvotes: 1
Reputation: 26167
You can utilize the onchange
event for the select
element, and code a form submit:
<form method="post">
<select name="myselect" onchange="this.form.submit();">
<option>blue</option>
<option>red</option>
</select>
</form>
That will automatically submit the form when the value is changed.
Upvotes: 9