Sunday, December 11, 2011

How do you add table names from a database to a drop down list on the form?

The table is in mysql and for programming javascript is used.How do you add table names from a database to a drop down list on the form?
SQL:


show tables;





some server side script to populate your JavaScript select box from the result of the above queryHow do you add table names from a database to a drop down list on the form?
Hi.....





I guess this piece of snippet would work....i havent tried bt pretty sure it will solve ur problem.....let me knw if there are any issues with it !!!!!





%26lt;?php


// the very next line is used to make connection with the mysql database


$con = mysql_connect(';localhost';,';root';,';';); // $con = mysql_connect(';localhost';,';username';,';pa鈥?br>

// check whether the connection is made or else die(stops further execution of application)


if (!$con)


{


die('Could not connect: ' . mysql_error());


}





mysql_select_db(';sugarcrm';, $con); // database name: ';sugarcrm'; and the connectn link $con





$result =mysql_list_tables(sugarcrm); /// mysql_list_tables will result out all the tables in database sugarcrm and the resultset will be stored in ';result';





?%26gt;


%26lt;select name=';course'; style=';width:190px';%26gt;


%26lt;?php


// firstly created list box without php interaction(only by HTML) and then fill the list(populate) with the record from $result.





while($row = mysql_fetch_array($result))


{


echo ';%26lt;option %26gt;';.$row['0'].';%26lt;/option%26gt;';;





}


?%26gt;


%26lt;/select%26gt;


%26lt;?php


mysql_close($con);


?%26gt;





Hope it helps.....





www.dbwebsolutions.com
first you need a server side script to access mysql database like php.





if you are displaying drop down list on an event


you will have to access this sever side script from javascript script through Ajax.





you will have to fire query- show tables; in mysql





pseudo code-





sever side script(in php)- showtable.php


--------------------------------------鈥?br>

$resultset =mysql_query(';show tables';);





echo ';%26lt;select name='db_table' %26gt;';;





while($row=mysql_fetch_assoc($resultse鈥?br>




echo ';%26lt;option value= '$row[0]' %26gt;$row[0]%26lt;/option%26gt;





}


echo ';%26lt;/select%26gt;';;





--------------------------------------鈥?br>

javascript (Ajax)-





var xmlhttp;


function getXmlHttpObject(){


if(window.XMLHttpRequest)


return new XMLHttpRequest();


else if(window.ActiveXObject)


return new ActiveXObject(';Microsoft.XMLHTTP';);


else


return null;


}





function stateChanged_showtable(){


if (xmlhttp.readyState==4){





document.getElementById(';show_table';).in鈥? xmlhttp.responseText;








}





}





function showTable(){


xmlhttp=getXmlHttpObject();


if(xmlhttp==null){


alert(';Your browser does not support XMLHTTP!';);


return ;


}


xmlhttp.onreadystatechange=stateChanged_鈥?br>

xmlhttp.open(';GET';,';showtable.php';,true)鈥?br>

xmlhttp.send(null);


}


--------------------------------------鈥?br>




html form--





first call function- showTable() on an event like onBlur or onChange





%26lt;span id=';show_table';%26gt;%26lt;/span%26gt; //drop down list will be displayed here on an event on which you call showtable() function








-pankaj


www.dbwebsolutions.com

No comments:

Post a Comment