La page php écrit du javascript avec les données enregistrées dans la base de données.
pour l'instant j'emploie 2 listes déroulantens, mais en pricipe le nombre n'est pas limité.
Et va écrir le choix fait par l'utilisateur dans la base de données.
Le code est assez complexe pour débutants. mais je me ferais un plaisir de l'implementer.
Pour autant que j'ai cherché ce code est unique.
Source / Exemple :
<html>
<head>
<title>Double Linked comboboxes with php-mysql</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<?php
if (!isset($passed_value)){ $passed_value=1;}// example
if (!isset($passed_value2)){ $passed_value2=1;}// example
if (!isset($passed_value3)){ $passed_value3=1;}// example
if (!isset($passed_value4)){ $passed_value4=1;}// example
// in the body onload sequence we call a function 'redirect' to set our comboboxes
?>
<body onload="redirect(<?php echo $passed_value.','.$passed_value2?>,2);redirect(<?php echo $passed_value3.','.$passed_value4?>,2)">
<?php
// standard conexxion see the included file conexxion.php
include('conexxion.php');
$id_link = @mysql_connect($dbhost, $loginname, $loginpass);
if( ! $id_link ){
//echo 'Cannot login';
}else{
//echo 'Succesfull conected to db';
}
mysql_select_db($dbname, $id_link) or die(mysql_error);
// select the data from table 'hoofd_akt' for the main aktivities for the first combobox called "example"
$sql="select * from landen";
// select the data from 'sub_akt' for the linked sub category for the second combo box stage 2
$sql2="select * from provincies WHERE vr_sleutel=".$passed_value;
$result=mysql_query($sql,$id_link) or die (mysql_error());
$result2=mysql_query($sql2,$id_link) or die (mysql_error());
echo 'result2'.$result2.'<br>';
?>
<!-- --><form name="doublecombo">
<select name="example" size="1" onChange="redirect(this.options.selectedIndex,<?php echo $passed_value.','.$passed_value2?>)">
<option>-select-</option>
<?php
// echo the options for box 1 and 2
// as the idnumber is stored in the table with all results, here we compare to set SELECTED
// this is a standard formula for retrieving stored info about a box selection
while ($newarray=mysql_fetch_array($result)) {
echo '<option value="'.$newarray['idnumber'].'" ';
if ($newarray['idnumber']==$passed_value) {echo "SELECTED";};
echo '>'.$newarray['land_naam'].'</option>'.'\n';
} ?>
</select>
<select name="stage2" size="1">
<?php
// here we do the same for the second box
while ($newarray=mysql_fetch_array($result2)) {
echo '<option value="'.$newarray['idnumber'].'" ';
if ($newarray['idnumber']==$passed_value2) {echo "SELECTED";};
echo '>'.$newarray['prov_naam'].'</option>';
$x=$x+1;} ?>
</select>
<script>
<!--
/*
The original JScript can be found in nearly every JScript website
I'm just mentioning one :)
Courtesy of SimplytheBest.net - http://simplythebest.net
var groups=document.doublecombo.example.options.length
var group=new Array(groups)
for (i=0; i<groups; i++){
group[i]=new Array
}
<?php
//Here comes the data from the two linked tables in a format suited for JScripting
// into the script mentioned here above
$sql="select * from landen";
$result=mysql_query($sql,$id_link) or die (mysql_error());
$x=0;$y=0;$m=0;$z=1;
while ($newarray=mysql_fetch_array($result)) {
echo "group[0][$m]=new Option(\"".$newarray['land_naam'].'","'.$newarray['idnumber'].'")'."\n";
$sql2="select * from provincies WHERE vr_sleutel=".$newarray['idnumber']." order by idnumber asc";
$result2=mysql_query($sql2,$id_link) or die (mysql_error());
while ($newarray2=mysql_fetch_array($result2)) {
echo "group[$z][$y]=new Option(\"".$newarray2['prov_naam'].'","'.$newarray2['idnumber'].'")'."\n";
$y=$y+1;
}
$z=$z+1;
$y=0;
$m=$m+1;
}
?>
var temp=document.doublecombo.stage2
function redirect(x,d,f){
for (m=temp.options.length-1;m>0;m--)
temp.options[m]=null
for (i=0;i<group[x].length;i++){
temp.options[i]=new Option(group[eval(x)][i].text,group[eval(x)][i].value)
}
temp.options[d].selected=true
}
//-->
</script>
<?php
//sql voor de 2e set comboboxen
$sql="select * from landen";
// select the data from 'sub_akt' for the linked sub category for the second combo box stage 2
$sql2="select * from provincies WHERE vr_sleutel=".$passed_value;
$result=mysql_query($sql,$id_link) or die (mysql_error());
$result2=mysql_query($sql2,$id_link) or die (mysql_error());
echo 'result2'.$result2.'<br>';
?>
<select name="example2" size="1" onChange="redirect2(this.options.selectedIndex,<?php echo $passed_value3.','.$passed_value4?>)">
<option>-select-</option>
<?php
// echo the options for box 3 and 4
// as the idnumber is stored in the table with all results, here we compare to set SELECTED
// this is a standard formula for retrieving stored info about a box selection
while ($newarray=mysql_fetch_array($result)) {
echo '<option value="'.$newarray['idnumber'].'" ';
if ($newarray['idnumber']==$passed_value3) {echo "SELECTED";};
echo '>'.$newarray['land_naam'].'</option>'.'\n';
} ?>
</select>
<select name="stage22" size="1">
<?php
// here we do the same for the second box
while ($newarray=mysql_fetch_array($result2)) {
echo '<option value="'.$newarray['idnumber'].'" ';
if ($newarray['idnumber']==$passed_value4) {echo "SELECTED";};
echo '>'.$newarray['prov_naam'].'</option>';
$x=$x+1;} ?>
</select>
<script>
var groups2=document.doublecombo.example2.options.length
var group2=new Array(groups2)
for (i=0; i<groups2; i++){
group2[i]=new Array
}
<?php
//Here comes the data from the two linked tables in a format suited for JScripting
// into the script mentioned here above
$sql="select * from landen";
$result=mysql_query($sql,$id_link) or die (mysql_error());
$x=0;$y=0;$m=0;$z=1;
while ($newarray=mysql_fetch_array($result)) {
echo "group2[0][$m]=new Option(\"".$newarray['land_naam'].'","'.$newarray['idnumber'].'")'."\n";
$sql2="select * from provincies WHERE vr_sleutel=".$newarray['idnumber']." order by idnumber asc";
$result2=mysql_query($sql2,$id_link) or die (mysql_error());
while ($newarray2=mysql_fetch_array($result2)) {
echo "group2[$z][$y]=new Option(\"".$newarray2['prov_naam'].'","'.$newarray2['idnumber'].'")'."\n";
$y=$y+1;
}
$z=$z+1;
$y=0;
$m=$m+1;
}
?>
var temp2=document.doublecombo.stage22
function redirect2(x2,d2,f2){
for (m=temp2.options.length-1;m>0;m--)
temp2.options[m]=null
for (i=0;i<group2[x2].length;i++){
temp2.options[i]=new Option(group2[eval(x2)][i].text,group2[eval(x2)][i].value)
}
temp2.options[d2].selected=true
}
//-->
</script>
</form><!-- -->
</body>
</html>
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.