PDA

View Full Version : javascript help



chezang
Mar 6th, 2007, 05:47 AM
hi list,

I could not make javascript work the way I like it to: I like to update the value of other form field based on selected value of another form field.
I have 3 functions as described below:

this creates the necessary arrays:

jsinfo2.php
==============

<?php

$link = new mysqli('localhost', 'root', 'mylocalmysql');
$link->select_db('testdb');

$query1 = "select sid, sname from state";
$query2 = "select * from city where ";

function DependList($link, $query1, $query2, $filter)
{
$q = $link->query($query1);
$qq = $link->query($query1);

$r3 = $qq->fetch_row();

echo "<SCRIPT Language=\"JavaScript\">\n";

echo "\tvar region = new Array('');\n";
echo "\tvar category = new Array('');\n";


$j =0;

do{ $j2 = $j + 1; //loop to make the region array

echo "\tregion['$j'] = '$r3[1]'\n";
echo "\tcategory['$j2'] = new Array('');\n";

$query3 = $query2;

$query3 .= "sid = '$r3[0]'";

$q4 = $link->query($query3);
$r4 = $q4->fetch_row();
$i = 0;

//loop to make corresponding city array
do{ echo "\tcategory['$j2']['$i'] = '$r4[1]'\n"; $i++; } while($r4 = $q4->fetch_row());

$j++;

}while($r3 = $qq->fetch_row());
echo "\t</script>\n";
}//end function





//these functions fills and clears the field values:

echo "<SCRIPT Language=\"JavaScript\">\n";

echo "function fillDropDownRegion(thisform, loc1, loc2)\n";
echo "\t{\n";
echo "\tlocation1 = eval('document.'+thisform+'.'+loc1+' .selectedIndex');\n";
echo "\tlocation2 = eval('document.'+thisform+'.'+loc2);\n";
//echo "fillDropDown(document.dealer.Location2, category[document.dealer.Location1.selectedIndex],1);\n";
echo "fillDropDown(location2, category[location1],1);\n";

echo "\t}\n";

echo "function clearDropDownRegion()\n";
echo "\t{\n";
echo "\tstrnetwork = document.dealer.network.value;\n";
echo "\tif (strnetwork=='HSCP'){\n";
echo "\tdocument.dealer.Location2.options.length= 0 ;\n";
echo "\tdocument.dealer.Location2.options[0] = new Option('-- Location --', '');\n";
echo "\t}\n";
echo "\telse{\n";
echo "\tdocument.dealer.Location2.options[0] = new Option('Choose a Location', '');\n";
echo "\t}\n";

echo "\t}\n";

echo "\tfunction fillDropDown(dropdown, data ,nVal)\n";
echo "\t{\n";
echo "\tdropdown.length = 0;\n";

echo "\tfor(i=0; data.length >i;i++)\n";

echo "\t{\n";
echo "\tif ( data == '')\n";

echo "\t{ var x='No Area';}\n";
echo "\telse\n";
echo "\t{ var x = data[i];}\n";

echo "\tvar headingName = x.substring(0);\n";
echo "\tdropdown[i] = new Option(headingName,data[i]);\n";
echo "\t}\n";
echo "\t}\n";

echo "\t</script>\n";

?>

testjs2.php
=============


and this is my main page which calls the js functions:


<?php /* Created on: 1/10/2007 */ include "jsinfo2.php"; ?>
<html>
<head><script language="javascript">


</script>

</head>
<body>
<table>
<tr>
<form name="dealer" action="" method="post">
<td>
<select name="Location1" onChange="fillDropDownRegion(dealer, Location1, Location2);" style="WIDTH: 150px;">
<option>Choose a Region</option>


<script language="javascript">
for(i=0;i<region.length;i++)
{ document.write("<option value=\""+region[i]+"\">"+region[i]+"</option>");}
</script>

</select>
</td>

<td>
<SELECT name="Location2">
<OPTION selected value="">Choose a Location</OPTION>
</SELECT>
</td>
</tr> </form>



</table>

</body>
</html>



I wanted to update the option values of Location2 based on the selected value of Location1.
How do I pass the form name, and two select field names to javascript functions. I have done as shown above but could not make it work.

this works without passing variables to js functions but I wanted to make this usable on more than one pages......


so help me please.....

thanks in advance.

chezang.....

chump2877
Mar 6th, 2007, 05:54 AM
Hi, welcome to Coding Forums...

Could you please use [code][./code] and/or [php][./php] tags around your different blocks of code (make sure you delete the periods inside of the tags)...This makes your code much easier to read for everyone...

Also, which form field will be updated based on the selected value of what other form field? Could you please be more specific, so that we don;t have to guess what you are talking about?

Thanks.

chezang
Mar 6th, 2007, 06:42 AM
thanks,

I have edited my post as you indicated.......


chezang

chump2877
Mar 6th, 2007, 06:57 AM
You forgot the code tags, so that people can read your code easily :)

In the WYSIWYG editor that you are using to post messages in this thread, you'll see a button that looks like a pound sign....there is also a button that looks like a curled piece of paper, right next to the pound sign...

Select the different blocks of code in your original post and press one of these 2 buttons to surround your code with the necessary tags....After you submit your message, you will see that your code is now nicely formatted for others to view and help you better...