input dynamic drop down into db
Moderator: General Moderators
input dynamic drop down into db
(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>
--------------------------------------------------------------------------------
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>
--------------------------------------------------------------------------------
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...
the names of the form fields need to match the insert code ..
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());
}Code: Select all
<form name="myform" method="POST">
Choose a Make & Model:
<select name="formfield1" onchange="select1(this.optionsїselectedIndex].value)">
<option value="ford">Ford</option>
<option value="chevrolet">Chevrolet</option>
</select>
<select name="formfield2">
<option value="foo">bar</option>
</select>
<input type="submit" name="buttonname" value="do it">
</form>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)
{
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їi] = new Option(xїi])
}
}
//-->
</script>
<script type="text/javascript">
<!--
function select2(selected)
{
newBox = document.forms.myform.box2
if (selected=="1999")
{
x=new Array("a","b","c")
}
else if (selected=="2000")
{
x=new Array("d","e","f")
}
for (i=0; i<x.length; i++)
{
newBox.optionsїi] = new Option(xїi])
}
}
//-->
</script>Code: Select all
<form name="myform" method="POST">
Choose a Make & Model:
<select name="formfield1" onchange="select1(this.optionsїselectedIndex].value)">
<option value="ford">Ford</option>
<option value="chevrolet">Chevrolet</option>
</select>
<select name="formfield2" onchange="select2(this.optionsїselectedIndex].value)">
<option value="1999">1999</option>
<option value="2000">2000</option>
</select>
<input type="submit" name="buttonname" value="do it">
</form>you have to call the pulldown by name like this...
instead of this...
Code: Select all
// in the first JS function
newBox = document.forms.myform.formfield1
// in the second JS function
newBox = document.forms.myform.formfield2Code: Select all
newBox = document.forms.myform.box2