473,386 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

javascript refresh a select box without refreshing page

178 100+
Hi, I am trying to improve the usabilty of an app I have written and it would be great if I could refresh the content of my select boxes without refreshing the whole page.

I am sure this could be done. perhaps using iFrames. Can anyone shed some light?
Nov 21 '08 #1
9 24116
RamananKalirajan
608 512MB
Refreshing the Select Box withut refreshing the page means in what way you want to refresh the select box. Wether are u adding the Database values into that select box or user defined values in that page should be added to the select box.

Regards
Ramanan Kalirajan
Nov 21 '08 #2
cleary1981
178 100+
the select box is populated from my database. when I add a new item to my database i want to see the values in the select box without refreshing.
Nov 21 '08 #3
gits
5,390 Expert Mod 4TB
so you want to push an update to a webapp when your database changes without user interaction? you just need to use an ajax-call and poll the db with it ... you have to decide how often you need that and you may use setInterval() for this purpose ... you just cannot really push things to the client ... the client has to request the update ...

kind regards
Nov 21 '08 #4
cleary1981
178 100+
Yeah i have the function of adding to my database on the same page so when I add a new item to my database I want the selectbox which is a list of records to now contain the new one. Can you show me how this is done with ajax?
Nov 21 '08 #5
gits
5,390 Expert Mod 4TB
that is not nessecary then ... you could add the option to the DB with the call and besides that you just need to add it to the node with dom-methods to avoid requests/traffic ... what have you done so far?
Nov 21 '08 #6
cleary1981
178 100+
Its is neccessary because I have three select boxes linked together. I need to turn my requests into ajax requests. Thanks for your help anyway. Below are the unctions I am currently using to populate my select boxes

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. require "config.php"; // database connection details 
  4. echo "
  5.  
  6. function fillType(){
  7.     //this function is used to fill the companies in main.php on load
  8. ";    
  9.  
  10. $q11=mysql_query("SELECT type FROM module_type");
  11. echo mysql_error();
  12. while($nt11=mysql_fetch_array($q11)){
  13. echo "addOption(document.form1.newtype, '$nt11[type]', '$nt11[type]');";}// end of while
  14. ?>
  15. }
  16.  
  17. function fillType2() {
  18.  
  19. removeAllOptions(document.form1.selecttype);
  20. addOption(document.form1.selecttype, "", "Select", "");
  21. <?php    
  22. $q77=mysql_query("SELECT type FROM module_type");
  23. echo mysql_error();
  24. while($nt77=mysql_fetch_array($q77)){
  25. echo "addOption(document.form1.selecttype, '$nt77[type]', '$nt77[type]');";}// end of while
  26. ?>
  27. }
  28.  
  29. function fillNewSub() {
  30.     removeAllOptions(document.form1.newsub);
  31.     addOption(document.form1.newsub, "", "Select", "");
  32.  
  33.     <?php
  34.         $q22=mysql_query("select distinct(type) from subtype"); 
  35.         while($nt22=mysql_fetch_array($q22)){
  36.             echo "if(document.form1.newtype.value == '$nt22[type]'){";
  37.             $q32=mysql_query("select subtype from subtype where type='$nt22[type]' order by subtype asc");
  38.             while($nt32=mysql_fetch_array($q32)){
  39.             echo "addOption(document.form1.newsub,'$nt32[subtype]', '$nt32[subtype]');";
  40.  
  41.             } // end of while loop
  42.             echo "}"; // end of JS if condition
  43.         }
  44. ?>
  45. }
  46.  
  47. function fillselectsub(){
  48.     removeAllOptions(document.form1.selectsub);
  49.     addOption(document.form1.selectsub, "", "Select", "");
  50.  
  51.     <?php
  52.         $q55=mysql_query("select distinct(type) from subtype"); 
  53.         while($nt55=mysql_fetch_array($q55)){
  54.             echo "if(document.form1.selecttype.value == '$nt55[type]'){";
  55.             $q56=mysql_query("select subtype from subtype where type='$nt55[type]' order by subtype asc");
  56.             while($nt56=mysql_fetch_array($q56)){
  57.             echo "addOption(document.form1.selectsub,'$nt56[subtype]', '$nt56[subtype]');";
  58.  
  59.             } // end of while loop
  60.             echo "}"; // end of JS if condition
  61.         }
  62. ?>
  63. }
  64.  
  65. function fillselectmod(){
  66.     removeAllOptions(document.form1.selectmod);
  67.     addOption(document.form1.selectmod, "", "Select", "");
  68.  
  69.     <?php
  70.         $q44=mysql_query("select distinct(subtype) from module"); 
  71.         while($nt44=mysql_fetch_array($q44)){
  72.             echo "if(document.form1.selectsub.value == '$nt44[subtype]'){";
  73.             $q45=mysql_query("select module_name from module where subtype='$nt44[subtype]' order by module_name asc");
  74.             while($nt45=mysql_fetch_array($q45)){
  75.             echo "addOption(document.form1.selectmod,'$nt45[module_name]', '$nt45[module_name]');";
  76.  
  77.             } // end of while loop
  78.             echo "}"; // end of JS if condition
  79.         }
  80. ?>
  81. }
  82.  
  83.  
  84. function removeAllOptions(selectbox)
  85. {
  86.     var i;
  87.     for(i=selectbox.options.length-1;i>=0;i--)
  88.     {
  89.         //selectbox.options.remove(i);
  90.         selectbox.remove(i);
  91.     }
  92. }
  93.  
  94. function addOption(selectbox, value, text )
  95. {
  96.     var optn = document.createElement("OPTION");
  97.     optn.text = text;
  98.     optn.value = value;
  99.  
  100.     selectbox.options.add(optn);
  101. }
  102.  
Nov 21 '08 #7
gits
5,390 Expert Mod 4TB
so you mean you have to update all three select-nodes when adding a new option to one of them? as i said ... then you need something that does the following steps:

1. user adds an option
2. start your ajax-call and insert the option to the db
3. when inserted retrieve the data from the db
4. send back the result
5. the onreadystatechange-handler could now update the list-nodes
Nov 21 '08 #8
cleary1981
178 100+
ok heres what I have for the first select. I can get the values into my program but how do I put the values into the select?

Expand|Select|Wrap|Line Numbers
  1. function ajax_filltype(){
  2.     var x=0;
  3.     var url = "filltype.php?x=" + escape(x);
  4.     url = url + "&dummy=" + new Date().getTime();
  5.     request.open("GET", url, true);
  6.     request.onreadystatechange = populateselecttype;
  7.     request.send(null); 
  8. }
  9. function populateselecttype() {
  10.     if (request.readyState == 4) {
  11.         var response = request.responseText;
  12.         var splitResult = response.split("%%");
  13.         for (i=0; i<splitResult.length-1; i++){
  14.             alert(splitResult[i]);
  15.             //add each option i to select box
  16.         }
  17.     }
  18.  
  19. }
php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. require "config.php";
  4.  
  5. $q1=mysql_query("SELECT type FROM module_type");
  6. while($r = mysql_fetch_array($q1)){
  7.         echo $r['type'] . '%%';
  8.  
  9.   }
  10. ?>
Nov 21 '08 #9
cleary1981
178 100+
never mind it was just

Expand|Select|Wrap|Line Numbers
  1. function populateselecttype() {
  2.     if (request.readyState == 4) {
  3.         var response = request.responseText;
  4.         var splitResult = response.split("%%");
  5.         for (i=0; i<splitResult.length-1; i++){
  6.             document.form1.selecttype.options[document.form1.selecttype.length] = new Option(splitResult[i], splitResult[i]);
  7.         }
  8.     }
  9.  
  10. }
Nov 21 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Steve | last post by:
Hi, I have a private website for 20 people that is similar to a web email client like hotmail. There are two frames, one on the left with links for "New", "History", "Todays" and a frame on the...
3
by: Troy | last post by:
Hello- I have a website that uses a custom built webserver to serve the pages. (Please don't ask me why my boss had his own web server written). I am displaying a log of information that is an...
9
by: paul | last post by:
Hi All, We have a small dilemma. We have the following page: http://giggsey.com/m00Cow.php (don't ask about the content) that we want to turn into an interactive application for some new intake...
7
by: Assimalyst | last post by:
Hi, I want to create a popup to notify a user that the server side process has completed successfully. I intended to use a javascript alert to do this, although ideally the icon should be...
10
by: paulie | last post by:
Hi, I have been experiencing an issue when trying to use AJAX to reload a DIV area using a timer of 2000ms, which contains a html page with another DIV and javascript. Scenario -------------...
2
by: Jimbo1 | last post by:
Hi there, I'm new to this group, so first of all I'd like to say a big "Hello"! Moving on, I'm a database developer who has found himself in a grey zone between the database and the web front...
4
by: Peter | last post by:
ASP.NET I have an application which use ASP.NET Autocomplete extender which works great. But I have a question how to update all the fields on the screen using Ajax. Users starts typing in a...
23
by: WebFoot | last post by:
Is there a reliable way for a webpage to either break out or refuse to display when a hostile website puts it in a frame? I know about the JavaScript solution, but not all visitors have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.