Ok this code seems to be working now (ajax getting the current selection) . But i now have another problem. When i use php $_GET method (for later database search), the output isnt just dropdown chosen word, but also generates another dropdown menu. There is also WAMP error – undefinex index for GET.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#select").change(function(){
$.ajax({
url: "test.php?selected=" +$(this).val(),
success:function(data){
$("#results").html(data);
}
}
)
})
});
</script>
<select id="select">
<option> something </option>
<option> something2 </option>
<option> something3 </option>
</select>
<?php
echo $_GET['selected'];
?>
<div id="results"></div>
The problem is arising because you’re self-referencing in the ajax call without accounting for the postback. Also the initial page load will throw an undefined index error because the
selectedkey does not exist in the$_GETcollection.At the top of your
test.phpfile:Then remove your echo later on in your example.
Note that this is only to make your example work and show why it failed. Not to give a well-formed example of an AJAX request.