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>
<option>
needs to have value
<option value="test">
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 selected
key does not exist in the $_GET
collection.
At the top of your test.php
file:
<?php
if(array_key_exists('selected', $_GET))
{
echo $_GET['selected'];
die();
}
?>
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.
What is echo $_GET['selected'];
supposed to display?
Can you give us some PHP code, as well?
And if you have an undefinex index for GET
, it means that the array $_GET
contains no key named selected
. Where do selected
come from?
If it comes from the JavaScript you gave us, then it should be select
instead. (Also, naming your select
with the keyword select
is not recommended.)