Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

forcing onchange event on selectbox

Posted on 2007-11-20
8
Medium Priority
?
4,688 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

705 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