Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4695
  • Last Modified:

forcing onchange event on selectbox

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
markmchugh
Asked:
markmchugh
  • 3
  • 2
  • 2
  • +1
1 Solution
 
Beverley PortlockCommented:
Yes - something like this

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


<select name='abc' onchange='someFunction'>
   <option>....</option>
   ...
</select>
0
 
Beverley PortlockCommented:
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
 
markmchughAuthor Commented:
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
markmchughAuthor Commented:
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
 
Beverley PortlockCommented:
Sorry - it looks more like a javascript problem than a PHP problem. Not really my area.
0
 
nizsmoDeveloperCommented:
>>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
 
b0lsc0ttIT ManagerCommented:
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
 
b0lsc0ttIT ManagerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now