Jump to content

Linked comboboxes with php and mysql


mkli

Recommended Posts

Hi everybody I am a begginer in php language.

I have created a form to retrieve data from mysql database

I want to create two combo boxes for two tables of my data.

When I ma using the one everything is ok but when I add the second they do not work as I do not know what exactly to change in javascript and php code.

The code of the page is "linked-combo4.php":

<html>

<head>

<title>Linked comboboxes with php-mysql</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

<?php

// we need to have the previously made choice from a database

//or not if this form is accessed the first time

// change $passed_value and $passed_value2 to see the effect of the code 'ON LOAD'

if (!isset($passed_value)){ $passed_value=2;}// example

if (!isset($passed_value2)){ $passed_value2=3;}// 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)">

<?php

 

// standard conexxion  see the included file conexxion.php

include('conexxion.php');

// select the data from table 'hoofd_akt' for the main aktivities for the first combobox called "example"

$sql="select * from hoofd_akt";

//  select the data from 'sub_akt' for the linked sub category for the second combo box stage 2

$sql2="select * from sub_akt 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());

?>

<!-- --><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['akt_groep'].'</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['akt_titel'].'</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=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 hoofd_akt";

 

$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['akt_groep'].'","'.$newarray['idnumber'].'")'."\n";

$sql2="select * from sub_akt 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['akt_titel'].'","'.$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=new Option(group[eval(x)].text,group[eval(x)].value)

}

temp.options[d].selected=true

 

}

 

//-->

</script>

</form><!-- -->

</body>

</html>

The other tables that I want to add is:

table1: object with fields idnumber,code

table2:obj_description with fields idnumber,no,description

 

Thanks

Marina

Link to comment
https://forums.phpfreaks.com/topic/50958-linked-comboboxes-with-php-and-mysql/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.