note 27298 deleted from faq.mailinglist by jsheets

From: Date: Sat, 01 Feb 2003 19:59:37 +0000
Subject: note 27298 deleted from faq.mailinglist by jsheets
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-43268@lists.php.net to get a copy of this message
Hi Q : How we javascript variable in php ? Ans. : It is tricky.Just take a look on that code. Output : Two combo box/Dropdownlist drawn.These two combo box contain data from database.when user select a option then second combo box contain data according to selected text which is also came from database. //file :DropDownClass.php <?php class DropDownClass{ //Connect to database function connect($host,$user,$password,$database) { mysql_connect($host,$user,$password); mysql_select_db($database); } //Draw First DropDown List function brand_type(){ $this->connect("localhost","root","publish","ecom"); $query=stripSlashes("select distinct metal_type from metal_nt"); $result=mysql_query($query); for($i=0;$i<mysql_num_rows($result);$i++) { $key[]=mysql_result($result,$i,metal_type); print "<option value=$key[$i]>".$key[$i]; } } //Draw Second DropDown List function showComboBox($selectedOptionVar,$FormName,$comboInputName){ $this->connect("localhost","root","publish","ecom"); $query=stripSlashes("select distinct metal_type from metal_nt order by metal_type"); $result=mysql_query($query); $NumIF=1; while ($values = mysql_fetch_array($result)) { if ($NumIF == 1){ $NumName = 1; print (" if (" . $selectedOptionVar . " == \"" . $values['metal_type'] . "\") {\n"); }else{ $NumName = 1; print (" else if (" . $selectedOptionVar . " == \"" . $values['metal_type'] . "\") {\n"); } $name_query = stripSlashes("select metal_name from metal_nt where metal_type = '" . $values['metal_type'] . "' order by metal_name"); $name_result=mysql_query($name_query); print (" " . $FormName . "." . $comboInputName . ".options.length=null;"); while ($name_values = mysql_fetch_array($name_result)) { if ($NumName == 1) print (" " . $FormName . "." . $comboInputName . ".options[0] = new Option(\"" . PLEASE_SELECT . "\", \"\");\n"); print (" " . $FormName . "." . $comboInputName . ".options[$NumName] = new Option(\"" . $name_values['metal_name'] . "\", \"" . $name_values['metal_name'] . "\");\n"); $NumName++; } $NumIF++; print (" }\n"); } } } ?> //file : Test.php <html> <Head> <?php include_once("DropDownClass.php"); //Create a object of dropdownclass CLASS $obj=new DropDownClass(); ?> <script language="javascript"> function ct(list) { var selectedOption=""; selectedOption=list.options[list.selectedIndex].text; <? $obj->showComboBox("selectedOption","selectBrand","combo_id"); ?> } </script> </Head> <body> <form name="selectBrand" method="get"> <table width="660" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="220" height="56" valign="middle"><b>Metal</b></td> <td width="206" valign="middle"> <select name=BrandType onchange=ct(this)> <?php //Function to draw first dropdown List $obj->brand_type(); ?> </select> </td> </tr> <tr> <td height="56" valign="middle"><b>Quality</b></td> <td valign="middle">com</td> <td valign="middle"> <select name=combo_id> <option></option> </select> </td> </tr> </table> </form> </body> </html> There is two table under Ecom database. run the file test.php with your configured Apache/php

« previous php.notes (#43268) next »