input dynamic drop down into db

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
jkatcher
Forum Newbie
Posts: 5
Joined: Tue May 11, 2004 8:54 pm

input dynamic drop down into db

Post by jkatcher »

(i searched and couldn't find these answers)

someone was nice enough to post this code (which made a few changes to the variables) -- thank you.

my questions:

1) how do you input the results into a database with javascript? i have the database, and tables set already...and know how to write php code to do it.

2) when the applet is initialized, the drop down on the left displays options, but there are no options on the right drop down. it's not until you go through each option that they all show up on the right...how can you change that?

3) is there a way to add more than 2 dynamic drop downs in this code? i am not too familiar w/ javascript.

THANK YOU SO MUCH IN ADVANCE!!!!



code:--------------------------------------------------------------------------------
<script type="text/javascript">
<!--
function select1(selected)
{
newBox = document.forms.myform.box2
if (selected=="ford")
{
x=new Array("Econoline","Taurus","Explorer")
}
else if (selected=="chevrolet")
{
x=new Array("Tahoe","Corsica","Trailblazer")
}
for (i=0; i<x.length; i++)
{
newBox.options = new Option(x)
}

}
//-->
</script>

<form name="myform">
Choose a Make & Model:
<select name="feeling" onchange="select1(this.options[selectedIndex].value)">
<option value="ford">Ford</option>
<option value="chevrolet">Chevrolet</option>
</select>
<select name="box2">
</select>
</form>
--------------------------------------------------------------------------------
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post by xisle »

have you tried duplicating the JS function with a name like select2 and using onchange="select2(this.options[selectedIndex].value)"
I used to just duplicate stuff before learning to write JS from scratch..
jkatcher
Forum Newbie
Posts: 5
Joined: Tue May 11, 2004 8:54 pm

Post by jkatcher »

definitely, that sounds like a good idea. how then, do i input the data into the database? i guess i'm just not sure how to incorporate that into javascript...
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post by xisle »

The JScript is building the options dynamically and doesn't need to be part of the DB insert, just use php/Mysql to do that...

Code: Select all

if($_POST['buttonname'] != ""){

	$link=MYSQL_CONNECT($host,$username,$password) OR DIE("Unable to connect to database");
	$db=mysql_select_db($DBname) or die( "Unable to select database");

	$query = "INSERT into tablename (field1,field2) VALUES('".$_POST['formfield1']."','".$_POST['formfield2']."')";
	$result=mysql_query($query) or die(mysql_error());
}
the names of the form fields need to match the insert code ..

Code: Select all

&lt;form name="myform" method="POST"&gt;  
Choose a Make &amp; Model:  
&lt;select name="formfield1" onchange="select1(this.options&#1111;selectedIndex].value)"&gt;  
&lt;option value="ford"&gt;Ford&lt;/option&gt;  
&lt;option value="chevrolet"&gt;Chevrolet&lt;/option&gt;  
&lt;/select&gt;  
&lt;select name="formfield2"&gt; 
&lt;option value="foo"&gt;bar&lt;/option&gt;   
&lt;/select&gt; 
&lt;input type="submit" name="buttonname" value="do it"&gt; 
&lt;/form&gt;
jkatcher
Forum Newbie
Posts: 5
Joined: Tue May 11, 2004 8:54 pm

Post by jkatcher »

what purpose does the 'foo' drop down serve?
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post by xisle »

that will be the second dynamic pulldown,
the one where you will copy the JS function to reuse it...
jkatcher
Forum Newbie
Posts: 5
Joined: Tue May 11, 2004 8:54 pm

Post by jkatcher »

this is the code i tried...but it didn't work out. two drop downs were created, but neither were dynamic..

Code: Select all

<script type="text/javascript"> 
<!-- 
function select1(selected) 
&#123; 
newBox = document.forms.myform.box2 
if (selected=="ford") 
&#123; 
x=new Array("Econoline","Taurus","Explorer") 
&#125; 
else if (selected=="chevrolet") 
&#123; 
x=new Array("Tahoe","Corsica","Trailblazer") 
&#125; 
for (i=0; i<x.length; i++) 
&#123; 
newBox.options&#1111;i] = new Option(x&#1111;i]) 
&#125; 

&#125; 
//--> 
</script> 

<script type="text/javascript"> 
<!-- 
function select2(selected) 
&#123; 
newBox = document.forms.myform.box2 
if (selected=="1999") 
&#123; 
x=new Array("a","b","c") 
&#125; 
else if (selected=="2000") 
&#123; 
x=new Array("d","e","f") 
&#125; 
for (i=0; i<x.length; i++) 
&#123; 
newBox.options&#1111;i] = new Option(x&#1111;i]) 
&#125; 

&#125; 
//--> 
</script>

Code: Select all

<form name="myform" method="POST">  
Choose a Make & Model:  
<select name="formfield1" onchange="select1(this.options&#1111;selectedIndex].value)">  
<option value="ford">Ford</option>  
<option value="chevrolet">Chevrolet</option>  
</select>  
<select name="formfield2" onchange="select2(this.options&#1111;selectedIndex].value)">  
<option value="1999">1999</option>  
<option value="2000">2000</option>  
</select> 
<input type="submit" name="buttonname" value="do it"> 
</form>
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post by xisle »

you have to call the pulldown by name like this...

Code: Select all

// in the first JS function
newBox = document.forms.myform.formfield1

// in the second JS function
newBox = document.forms.myform.formfield2
instead of this...

Code: Select all

newBox = document.forms.myform.box2
Post Reply