Solved

forcing onchange event on selectbox

Posted on 2007-11-20
8
4,666 Views
Last Modified: 2008-04-21
hi, i'm using php to fill a select box, when the box is filled, i want to for an "onchange" event, is this possible?
0
Comment
Question by:markmchugh
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 20322073
Yes - something like this

<script>
function someFunction() {
    alert("Hello campers...");
}
</script>


<select name='abc' onchange='someFunction'>
   <option>....</option>
   ...
</select>
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 20322086
Hmmm..... on re-reading your question, I've just seen this "...to fill a select box..."

What do you mean by that? Populating a SELECT can be done by PHP, but onchange is a javascript function.

I think I've misunderstood your question.
0
 

Author Comment

by:markmchugh
ID: 20322101
this is a litte complex, here's the code


							<?php

//please populate the option dynamically
 

					$query = "Select * from countys;";

					$result1 = mysql_query($query);

				

					if (mysql_num_rows($result1) > 0)		

					{

						$option[] = '--';

						$option1[]= '--';

						while ($myrow1 = mysql_fetch_array($result1)) 

						{

						$option[]=$myrow1[1];

						$option1[]=$myrow1[0];

						}

	

					}
 
 
 
 

//$option = array('--','Yes','No');

$userinput = '--';

if (isset($_SESSION["var1"])) {

	$tmpvar = $_SESSION["var1"];

	$query = "Select * from countys where id = $tmpvar";

					$result1 = mysql_query($query);

				

					if (mysql_num_rows($result1) > 0)		

					{

						

						while ($myrow1 = mysql_fetch_array($result1)) 

						{

						$userinput = $myrow1[1];

					

						}

	

					}
 

}
 

?>

						

						

						<select id="County" name="County" style="width:180px" onChange="getCityList(this)">

                          

                          
 

				        <?

 

        for($i=0;$i<count($option);$i++)

        {

         $selectedText='';

         if($option[$i]==$userinput)

         {

                $selectedText = 'selected="selected"';

         }

        ?>

        <option value="<?php echo $option1[$i]; ?>" <?php echo $selectedText?>><?php echo $option[$i]; ?></option>

        <?php } ?>    

Open in new window

0
 

Author Comment

by:markmchugh
ID: 20322114
sorry, mistake there, the function onChange="getCityList(this)" fills another listbox, depending on whats in the first one, all works fine the first time the user loads the form, but when the user bits back to get to the form i want to keep their previous settings + still be able to select different options.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 20322133
Sorry - it looks more like a javascript problem than a PHP problem. Not really my area.
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20322727
>>user bits back to get to the form i want to keep their previous settings + still be able to select different options.

not sure what you mean here, do you mean when they click the back button on the browser?
0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 500 total points
ID: 20323956
markmchugh,

What you want to do is done with an effect called AJAX.  It will let you dynamically fill a listbox based on the selection in another listbox without refreshing the page.  I can help you with specifics on using it and working with it in this page but I first recommend taking a look at a page or two to see if this is what you want.

One of the best explanations and simplest examples I have found on the net is at http://www.skeymedia.com/programming/classic-asp-and-ajax-tutorial/.  There is also a good explanation on what AJAX is at http://en.wikipedia.org/wiki/Ajax_%28programming%29.  The reference site W3Schools has a good section on AJAX at http://www.w3schools.com/ajax/default.asp.

The server script in the "best" example is a different language but that is just a minor thing.  The key is the server is sending the contents for the page, the new, dynamic part.  In your case it could be the html for a select list.  The server response would be "inserted" in the page by Javascript using DOM, for example in a div called "select2."

Let me know what you think of it and if this sounds like what you need.  If that is the case then let me know what help you need to use it.  Much of the code in the "best" example could almost be used as is in your page with minor changes.  I could provide a version you can use if you want.  There are also prototypes and other tools, even in PHP, that provide AJAX features and abilities without all of the coding.  Let me know if you have any questions or need more information.

b0lsc0tt

p.s.  Have you received any recent emails from me?
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 21403947
Thanks for closing this (and your other open questions ;)).  I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol

p.s.  Still curious about that last question I asked.  Oh well. :(
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will learn how to count occurrences of each item in an array.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now