pinky_rose Posted October 27, 2008 Share Posted October 27, 2008 i have problem to display data from table by using drop down menu...the example that i found is using javascript... http://javascript.internet.com/forms/auto-drop-down.html but i want to use php code and the option is from the database... can somebody show me the way? Quote Link to comment https://forums.phpfreaks.com/topic/130293-auto-drop-down-from-database/ Share on other sites More sharing options...
MadTechie Posted October 27, 2008 Share Posted October 27, 2008 kinda old code but still may help http://www.phpfreaks.com/forums/index.php/topic,155984.0.html Quote Link to comment https://forums.phpfreaks.com/topic/130293-auto-drop-down-from-database/#findComment-675727 Share on other sites More sharing options...
pinky_rose Posted October 27, 2008 Author Share Posted October 27, 2008 thanks for the code....but i'm still confiuse which coding should i use...i'm still new in php.. in my database has a table named Jawatan: KodGredSSM | NamaJawatan | V25 | Pensyarah| V26 | Professor | here is my coding: <select name="KodGredSSM" id="KodGredSSM" > <option value="<? if(isset($_POST['KodGredSSM'])) echo"".$_POST['KodGredSSM'].""; ?>" selected="selected"><? if(isset($_POST['KodGredSSM'])) echo"".$_POST['KodGredSSM'].""; ?></option> <?php $query="SELECT * FROM jawatan"; $result=mysql_query($query) or die('Data tidak dapat dipanggil.'); while($row=mysql_fetch_array($result)) { ?> <option value="<?php echo "{$row['KodGredSSM']}";?>"><?php echo "{$row['KodGredSSM']}";?> </option> <? } ?> </select> <input type="text" name="NamaJawatan"> <--textfield i want KodGredSSM that i had choose in drop down menu appear in textfield named NamaJawatan.. Quote Link to comment https://forums.phpfreaks.com/topic/130293-auto-drop-down-from-database/#findComment-675777 Share on other sites More sharing options...
revraz Posted October 27, 2008 Share Posted October 27, 2008 You can't do it on the same page with just PHP unless you use a submit button, you need to use Ajax or Javascript if you want it dynamic. Quote Link to comment https://forums.phpfreaks.com/topic/130293-auto-drop-down-from-database/#findComment-675788 Share on other sites More sharing options...
pinky_rose Posted October 28, 2008 Author Share Posted October 28, 2008 thank you revraz...can someone show me how to use javascrpit?...i need to use mysql query to get data from database...but i have no idea to use mysql query in javascript... i found the codes but still not get it.. here is the example: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title></title> <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/ intellisense/ie5"> <script id="clientEventHandlersJS" language="javascript"> <!-- functionButton1_onclick() { var locator = new ActiveXObject ("WbemScripting.SWbemLocator"); var service = locator.ConnectServer(".","rootMicrosoftSQLServer"); var properties = service.ExecQuery("SELECT Name FROM MSSQL_Database"); var e = new Enumerator (properties); document.write("<table border=1>"); dispHeading(); for (;!e.atEnd();e.moveNext ()) { var p = e.item (); document.write("<tr>"); document.write("<td>" + p.Name + "</td>"); document.write("<td>" + p.SQLServerName + "</td>"); document.write("</tr>"); } document.write("</table>"); } functiondispHeading() { document.write("<thead>"); document.write("<td>Name</td>"); document.write("<td>SQLServerName</td>"); document.write("</thead>"); } //--> </script> </head> <body> <INPUT id="Button1" type="button" value="Button" name="Button1" language="javascript" onclick="return Button1_onclick()"> </body> </html> can i use the sql command like above? Quote Link to comment https://forums.phpfreaks.com/topic/130293-auto-drop-down-from-database/#findComment-676783 Share on other sites More sharing options...
aseaofflames Posted October 29, 2008 Share Posted October 29, 2008 i would use ajax for this. function getval(KodGredSSM) { var ajaxRequest; // The variable that makes Ajax possible! try{ // Opera 8.0+, Firefox, Safari ajaxRequest = new XMLHttpRequest(); } catch (e){ // Internet Explorer Browsers try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ // Something went wrong alert("Your browser broke!"); return false; } } } // Create a function that will receive data sent from the server ajaxRequest.onreadystatechange = function(){ if(ajaxRequest.readyState == 4){ document.getElementById("NamaJawatan").value = ajaxRequest.responseText; } } //this var php = "getval.php?KodGredSSM="+KodGredSSM; ajaxRequest.open("GET", php, true); ajaxRequest.send(null); } then change <select name="KodGredSSM" id="KodGredSSM" > to <select name="KodGredSSM" id="KodGredSSM" onChange="getval(this.value)"> and add id="NamaJawatan" to the textfield then create a file name getval.php (you need to add the code to connect to the database): <?php $KodGredSSM = $_GET['KodGredSSM']; $query = mysql_query("select NamaJawatan from Jawatan where KodGredSSM='$KodGredSSM' LIMIT 1"); $get = mysql_fetch_row($query); echo $get[0]; ?> every time the value in the select is changed, the value of the textfield should change.[/code] Quote Link to comment https://forums.phpfreaks.com/topic/130293-auto-drop-down-from-database/#findComment-677055 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.