click event on combo box

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
zia
Forum Newbie
Posts: 9
Joined: Fri Jan 03, 2003 3:51 am

click event on combo box

Post by zia »

Hello members

I am working with PHP(4.3), mysql(3.23.54) and Apache 1.3.27 on Windows 2000 server. I want the following thing to happen. If a user select an option from a combobox then some other elements on the form will be filled up by the values from database. If JavaScript can perform this please give me some tips about that.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

<select>-elements expose a onChange-event that can be used to trigger a function, e.g.

Code: Select all

<html>
<head>
	<script type="text/javascript">
		function changed()
		&#123;
			alert( document.getElementById("sel").value );
			return true;
		&#125;
	</script>
</head>
<body>
<select id="sel" onChange="changed()">
	<option>a</option>
	<option>b</option>
	<option>c</option>
</select>
</body></html>
But note that php and javascript have (virtually) nothing to do with each other. You cannot call a php-function that resides server-side from the client-side browser/script.
zia
Forum Newbie
Posts: 9
Joined: Fri Jan 03, 2003 3:51 am

click event on combo box

Post by zia »

Dear volka

Thank you very much for your kind reply. Would you please suggest me what shold I do to achieve my objective? Waiting for your response.
Thanks again.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if you really need to pull the data from a database when users change settings you might use frames. In the onChange-function you'd change the url of the next frame.
As a simple example:

Code: Select all

&lt;!-- file: test.html --&gt;
&lt;html&gt;
&lt;frameset cols="20%,*"&gt;
	&lt;frame name="first" src="test.php" /&gt;
	&lt;frame name="second" src="test.php?option=1" /&gt;
&lt;/frameset&gt;
&lt;/html&gt;

Code: Select all

<html><?php // file: test.php
if (isset($_GET['option']))
	echo '<body>', $_GET['option'];
else
{ ?>
	<head>
	<script type="text/javascript">
		function toggle(elem)
		{
			parent.second.location = "<?php echo $_SERVER['PHP_SELF']; ?>?option="+elem.value;
		}
	</script>
</head>
<body>
	<select onChange="toggle(this)>
		<option>1</option>
		<option>2</option>
		<option>3</option>
	</select>	
<?php
}
?></body></html>
User avatar
DaZZleD
Forum Commoner
Posts: 38
Joined: Tue Jan 07, 2003 5:39 am

Post by DaZZleD »

if the amount of data isn't very large you might consider loading it into an array and displaying it with the aid of javascript without reloading the frame (by changing the innerText of a layer for example.)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

That's of course an option, but I gave up to explain that since I always failed to do so :roll:

but an example can be found at the creative-website
http://uk.europe.creative.com/support/m ... elcome.asp
zia
Forum Newbie
Posts: 9
Joined: Fri Jan 03, 2003 3:51 am

Post by zia »

Hello,
Thank you every one. I have done it in the following way:-

<script language="JavaScript">
function getCourseID(c_id)
{

window.location="Course_Offer_Entry.php?id="+c_id

}
</script>
.................................
<select size="1" name="Course_No"
onChange="getCourseID (window.document.Course_Offer.Course_No.options[selectedIndex].text)

Have you any better suggestion to optimize the code?
Post Reply