javascript refresh a select box without refreshing page | Familiar Sight | | Join Date: Jun 2008
Posts: 164
| | |
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?
|  | Needs Regular Fix | | Join Date: Mar 2008 Location: Chennai - India
Posts: 350
| | | re: javascript refresh a select box without refreshing page
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
| | Familiar Sight | | Join Date: Jun 2008
Posts: 164
| | | re: javascript refresh a select box without refreshing page
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.
|  | Moderator | | Join Date: May 2007 Location: Munich, Germany
Posts: 4,136
| | | re: javascript refresh a select box without refreshing page
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
| | Familiar Sight | | Join Date: Jun 2008
Posts: 164
| | | re: javascript refresh a select box without refreshing page
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?
|  | Moderator | | Join Date: May 2007 Location: Munich, Germany
Posts: 4,136
| | | re: javascript refresh a select box without refreshing page
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?
| | Familiar Sight | | Join Date: Jun 2008
Posts: 164
| | | re: javascript refresh a select box without refreshing page
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 - <?php
-
-
require "config.php"; // database connection details
-
echo "
-
-
function fillType(){
-
//this function is used to fill the companies in main.php on load
-
";
-
-
$q11=mysql_query("SELECT type FROM module_type");
-
echo mysql_error();
-
while($nt11=mysql_fetch_array($q11)){
-
echo "addOption(document.form1.newtype, '$nt11[type]', '$nt11[type]');";}// end of while
-
?>
-
}
-
-
function fillType2() {
-
-
removeAllOptions(document.form1.selecttype);
-
addOption(document.form1.selecttype, "", "Select", "");
-
<?php
-
$q77=mysql_query("SELECT type FROM module_type");
-
echo mysql_error();
-
while($nt77=mysql_fetch_array($q77)){
-
echo "addOption(document.form1.selecttype, '$nt77[type]', '$nt77[type]');";}// end of while
-
?>
-
}
-
-
function fillNewSub() {
-
removeAllOptions(document.form1.newsub);
-
addOption(document.form1.newsub, "", "Select", "");
-
-
<?php
-
$q22=mysql_query("select distinct(type) from subtype");
-
while($nt22=mysql_fetch_array($q22)){
-
echo "if(document.form1.newtype.value == '$nt22[type]'){";
-
$q32=mysql_query("select subtype from subtype where type='$nt22[type]' order by subtype asc");
-
while($nt32=mysql_fetch_array($q32)){
-
echo "addOption(document.form1.newsub,'$nt32[subtype]', '$nt32[subtype]');";
-
-
} // end of while loop
-
echo "}"; // end of JS if condition
-
}
-
?>
-
}
-
-
function fillselectsub(){
-
removeAllOptions(document.form1.selectsub);
-
addOption(document.form1.selectsub, "", "Select", "");
-
-
<?php
-
$q55=mysql_query("select distinct(type) from subtype");
-
while($nt55=mysql_fetch_array($q55)){
-
echo "if(document.form1.selecttype.value == '$nt55[type]'){";
-
$q56=mysql_query("select subtype from subtype where type='$nt55[type]' order by subtype asc");
-
while($nt56=mysql_fetch_array($q56)){
-
echo "addOption(document.form1.selectsub,'$nt56[subtype]', '$nt56[subtype]');";
-
-
} // end of while loop
-
echo "}"; // end of JS if condition
-
}
-
?>
-
}
-
-
function fillselectmod(){
-
removeAllOptions(document.form1.selectmod);
-
addOption(document.form1.selectmod, "", "Select", "");
-
-
<?php
-
$q44=mysql_query("select distinct(subtype) from module");
-
while($nt44=mysql_fetch_array($q44)){
-
echo "if(document.form1.selectsub.value == '$nt44[subtype]'){";
-
$q45=mysql_query("select module_name from module where subtype='$nt44[subtype]' order by module_name asc");
-
while($nt45=mysql_fetch_array($q45)){
-
echo "addOption(document.form1.selectmod,'$nt45[module_name]', '$nt45[module_name]');";
-
-
} // end of while loop
-
echo "}"; // end of JS if condition
-
}
-
?>
-
}
-
-
-
function removeAllOptions(selectbox)
-
{
-
var i;
-
for(i=selectbox.options.length-1;i>=0;i--)
-
{
-
//selectbox.options.remove(i);
-
selectbox.remove(i);
-
}
-
}
-
-
function addOption(selectbox, value, text )
-
{
-
var optn = document.createElement("OPTION");
-
optn.text = text;
-
optn.value = value;
-
-
selectbox.options.add(optn);
-
}
-
|  | Moderator | | Join Date: May 2007 Location: Munich, Germany
Posts: 4,136
| | | re: javascript refresh a select box without refreshing page
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
| | Familiar Sight | | Join Date: Jun 2008
Posts: 164
| | | re: javascript refresh a select box without refreshing page
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? - function ajax_filltype(){
-
var x=0;
-
var url = "filltype.php?x=" + escape(x);
-
url = url + "&dummy=" + new Date().getTime();
-
request.open("GET", url, true);
-
request.onreadystatechange = populateselecttype;
-
request.send(null);
-
}
-
function populateselecttype() {
-
if (request.readyState == 4) {
-
var response = request.responseText;
-
var splitResult = response.split("%%");
-
for (i=0; i<splitResult.length-1; i++){
-
alert(splitResult[i]);
-
//add each option i to select box
-
}
-
}
-
-
}
php - <?php
-
-
require "config.php";
-
-
$q1=mysql_query("SELECT type FROM module_type");
-
while($r = mysql_fetch_array($q1)){
-
echo $r['type'] . '%%';
-
-
}
-
?>
| | Familiar Sight | | Join Date: Jun 2008
Posts: 164
| | | re: javascript refresh a select box without refreshing page
never mind it was just - function populateselecttype() {
-
if (request.readyState == 4) {
-
var response = request.responseText;
-
var splitResult = response.split("%%");
-
for (i=0; i<splitResult.length-1; i++){
-
document.form1.selecttype.options[document.form1.selecttype.length] = new Option(splitResult[i], splitResult[i]);
-
}
-
}
-
-
}
|  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,501 network members.
|