Solved

forcing onchange event on selectbox

Posted on 2007-11-20
8
4,663 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Ajax control editor 4 37
update field on focusout 15 24
url rewrites not working in codeigniter 2 13
Adding through query php 9 12
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 demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

746 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

9 Experts available now in Live!

Get 1:1 Help Now