Jump to content

Autopopluate without refreshing


brooksh

Recommended Posts

I already have a javascript that will populate a drop down with a refresh. I would like to be able to do this, without refreshing the page. Any help would be appreciated.

 

<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.make.options[form.make.options.selectedIndex].value;
self.location='index.php?make=' + val ;
}

</script>

 

<?
@$make=$_GET['make']; // Use this line or below line if register_global is off
$quer2=mysql_query("SELECT DISTINCT make FROM car_database order by make"); 
$quer=mysql_query("SELECT DISTINCT model FROM car_database where make='$make' order by model"); 
?>
Make: <select name='make' onchange="reload(this.form)" ><option value="">Select one</option>
<?
while($noticia2 = mysql_fetch_array($quer2)) { 
echo "<option value='$noticia2[make]'";
if($noticia2['make']==@$make){echo "selected";}
echo ">$noticia2[make]</option>";
}
?>
</select>
<BR>Model: <select name="model" onchange="ShowTB(this,'model2');"><option value="">Select one</option>
<?
while($noticia = mysql_fetch_array($quer)) { ?>
<option value='<?=$noticia[model]?>' <? if($model == "$noticia[model]"){ echo "selected"; } ?>><?=$noticia[model]?></option>

Link to comment
Share on other sites

Well, this has been moved into the AJAX forum. But, depending on the amount of data it might be more efficient to just handle it in JavaScript.

 

If you are dealing with a "relatively" small list of data, you could simply use the data in the database to create javascript arrays on the page for all of the select list options and swap out the values only using JavaScipt. I say relatively because how big is too big is up to you. You could run some tests to see if there are any performance issues, but you could probably have hundreds of options before you see anything noticable.

 

If you do have a lot of options or just don't want all the options in the code, then you would want to use AJAX. however, you should include a way for users without JS to use the form too. Probably just keep the currentl functinoality you have, but instead of using javascript to change the page have a button to update the list that simple submits the form and checks the "make" field to redisplay the form with the appropriate values.

 

So, if you do want to use AJAX, here is a tutorial for you: http://www.satya-weblog.com/2007/04/dynamically-populate-select-list-by.html

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.