user896692
user896692

Reputation: 2371

Two Selectboxes - load by selection

I want, if I select a value in selectbox 1, that selectbox2 is loaded with values that are dependet on selectbox1.

Here´s how I tried:

<head>
<script src="jquery/jquery-1.8.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
 $("#selectbox1").change(function(){
 var value = $(this).children('option.selected').val(); 
 $("#selectbox2").load("php/loadteams.php", {value: value} );

});
});

</script>
</head>


<body>

<?php 

include 'php/db.php';
$res = mysql_query("select * from liga", $connection);
echo '<select id="selectbox1">';
while($val = mysql_fetch_array($res)) {
    echo '<option value="'.$val['id'] .'">' . $val['name'] . '</option>';
}
echo '</select>';
mysql_close($connection);
?>



<select id="selectbox2"> </select>

Like you see, I tried with jquery. The file loadteams.php looks like that:

<?php
include 'php/db.php';
$ligaid = mysql_real_escape_string($_POST['value']);

$result = mysql_query("select * from mannschaft where liga = '" . $ligaid . "'", $connection);

while($te = mysql_fetch_array($result)){
 echo '<option> ' . $te . '</option>';  
}

?>

I don´t know where my mistake is - can you help me?

Upvotes: 0

Views: 124

Answers (1)

Christofer Eliasson
Christofer Eliasson

Reputation: 33865

You should try using the :selected selector, instead of .selected which would match a class with the name selected.

Also, I believe the .load() method posts data through HTTP GET, so I believe you would have to use $_GET["value"] instead of $_POST['value'].

Correction:

As you are providing the data as an object, it IS posted through HTTP POST, so strike my above statement. From the jQuery documentation:

The POST method is used if data is provided as an object; otherwise, GET is assumed.

Upvotes: 2

Related Questions