Click to See Complete Forum and Search --> : hlp with function call


katasova
12-12-2005, 11:22 AM
hi evreyone, i want to call a funtion to populate a combo box, but it is not working, this is my code



<form name="form1" action="" method="post">
<select name="state" id="estado" onchange="javascript:change();">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
</select>

<select name="city">
<option value="#">place holder</option>
</select>

<?php
include "Includes/ListCitys.php";
?>

<script language="javascript" type="text/javascript">

function change(){

var cacheobj=document.form1.city;
populate(combo1);
}

</script>



Evreything works fine if i dont try to use a function i.e.



<script language="javascript" type="text/javascript">

var cacheobj=document.form1.city;
populate(combo1);

</script>



but it is like the onChange property of my first combo box is not really calling the the function was never called...

anyone have any idea on why is this happening?
THX

TheBearMay
12-12-2005, 11:30 AM
The code you've posted should work assuming combo1 is defined somewhere (it's not defined in your code sample above), and the function populate is also defined.

katasova
12-12-2005, 11:41 AM
this is the other code, it is in the ListCitys.php file:



include "connect.php";

$query = "SELECT DISTINCT(City) FROM User WHERE State= 'anyState' ORDER BY City";

$res = mysql_query($query) or die ('Error');
$num = mysql_num_rows($res);

$x=0;
while($x<$num){

$array1[$x] = mysql_result($res, $x);
$x++;

}




<script language="javascript">

var combo1=new Array()
<?php

for($x=0;$x<$num;$x++)
{
?>
combo1["<?php echo $x; ?>"]=new Option("<?php echo $array1[$x]; ?>", "<?php echo $array1[$x]; ?>");
<?php
}
?>

</script>


<script language="javascript">
<!--

function populate(x){
for (m=cacheobj.options.length-1;m>0;m--)
cacheobj.options[m]=null
selectedarray=eval(x)
for (i=0;i<selectedarray.length;i++)
cacheobj.options[i]=new Option(selectedarray[i].text,selectedarray[i].value)
cacheobj.options[0].selected=false
}


//-->
</script>



it is not working :confused: as i said it works when i dont try to call populate() from a function, but i really need to call populate() from that function :o

katasova
12-12-2005, 11:59 AM
after several tests i found what is wrong.... in this section:



<script language="javascript" type="text/javascript">

function change(){

var cacheobj=document.form1.city;
populate(combo1);
}

</script>




i cant call the function populate() if i remove the call to function and change it for the code inside the function populate it works perfect...

its like i cant call functions inside other functions.... is this true? can i fix this in any way?



Again i made some tests and found that if i define the function populate() inside th function change() it works again....

So is this true, i cant call outsde functions from other functions?