The title may not be the best way to word this question, so please let me know if I can improve it.
What I am trying to do is use a drop down menu to change the value of a PHP object.
In the example below, I want $selected to be assigned as the "value" of the the menu option selected.
$selected = $_GET['program_select'];
Here is the drop down menu:
<select id="programs_select" name="programs_select">
<option>Select a Different Month</option>
<?
foreach($pagedata->programs as $programs){
?>
<option value="<?=$programs->id?>"><?=$programs->title?></option>
<?
}
?>
</select>
How can I send this value to the server and reload the page using the selected value?
For example, if the 2nd option is selected and it's id is 2 the result would be:
$selected = 2;
FYI, this is being used to dynamically generate the output data for a thumbnail menu. The "program" being displayed would be determined by the value in the option selected.
<select id="programs_select" name="programs_select" onchange="ihavechanges(this.options[this.selectedIndex].value)">
<option>Select a Different Month</option>
<?
foreach($pagedata->programs as $programs){
?>
<option value="<?=$programs->id?>"><?=$programs->title?></option>
<?
}
?>
</select>
<script type="text/javascript">
function ihavechanged(towhat)
{
document.location.href = "reload.php?id="+towhat;
}
</script>
<select id="programs_select" name="programs_select">
<option>Select a Different Month</option>
<?
$selected = $_GET['program_select'];
foreach($pagedata->programs as $programs){
$seld = ($selected == $program-id) ? 'selected' : '';
echo '<option value="'.$programs->id.'" '.$seld.'>'.$programs->title.'</option>';
}
?>
</select>
You are using GET in the php side. So, if you just want to know how to pass the value to your php file, then put your select tag into a form and add a submit button.
<form id="your_id" name="your_id" method="get" action="your_controller/your_function_name">
<select id="programs_select" name="programs_select">
<option>Select a Different Month</option>
<?
foreach($pagedata->programs as $programs){
?>
<option value="<?=$programs->id?>"><?=$programs->title?></option>
<?
}
?>
</select>
<input type="submit" id="submit" name="submit" value="GO">
</form>
In your controller, echo something based on the value.
$selected = $_GET['program_select'];
However, if your want to change the contents in the same page without refresh. You may need ajax call to do that, and I recommend the jQuery ajax api (http://api.jquery.com/jQuery.ajax/).
$("select#programs_select").live('change', function(){
var id = $("select#programs_select").val();
$.ajax({
type: "GET",
url: "your_controller/the_funstion_name?programs_select=" + id,
data: "",
contentType: "application/json; charset=utf-8",
success: function(msg) {
$("#your_thumbnail_menu_id").html(msg);
},
error: function() {
alert("Error! Ajax failed");
}
});
});
In your controller side, echo the html you need at last. eg:
$selected = $_GET['program_select'];
$html = "<ul><li>your menu $selected</li>";
$html .="<li>which depends on the id you got!</li></ul>";
echo $html;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.