Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/130293-auto-drop-down-from-database/
Share on other sites

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..

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?

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]

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.