Page 1 of 1

click event on combo box

Posted: Fri Feb 07, 2003 9:15 am
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.

Posted: Fri Feb 07, 2003 10:02 am
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.

click event on combo box

Posted: Fri Feb 07, 2003 12:38 pm
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.

Posted: Sun Feb 09, 2003 12:25 pm
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>

Posted: Mon Feb 10, 2003 4:50 am
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.)

Posted: Mon Feb 10, 2003 1:06 pm
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

Posted: Thu Feb 13, 2003 10:30 am
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?