Jump to content

javascript arrays problem


mona
 Share

Recommended Posts

hi can u help me to pass php values to javascript array,actually I have many drop down boxesI need onchange of one of them to change the values of the others according to the value of the first oneand since I dont need to submit the form to take the values and use them in php , I tried at the first of my program to put all the data in javascript arrays and then onchange I use javascript function to search for my values. this is a logical solution but it is not working with me, can u find the problemthis is my program:search.php<?php$db = mysql_connect('localhost','root', 'memo1982');mysql_select_db ("collab1",$db);$sql=mysql_query("select * from student",$db);?><script language='javascript'>function lolo(){p=0;var id=new array();var fname=new array();var lname=new array();var gender=new array();var level=new array();var type=new array();<?$sql1=mysql_query("select * from sanction s, student st, officer o where s.Id=st.Id and s.Oid=o.Oid",$db);while($row = mysql_fetch_array($sql1, MYSQL_NUM)){?>id[p]=<?=$row["Id"]?>;p=p+1;<?}?>document.write(p); //i made this one only to see if the function is working but it is not working}</script>it is not even understand the arrays, can u help me work with arrays and solve my problem?

Link to comment
Share on other sites

<?$sql1=mysql_query("select * from sanction s, student st, officer o where s.Id=st.Id and s.Oid=o.Oid",$db);while($row = mysql_fetch_array($sql1, MYSQL_NUM)){?>id[p]=<?=$row["Id"]?>;p=p+1;<?}?>

try changing the above to this

<?$sql1=mysql_query("select * from sanction s, student st, officer o where s.Id=st.Id and s.Oid=o.Oid",$db);$phpCounter = 0;while($row = mysql_fetch_array($sql1, MYSQL_NUM)){?>p = <?=phpCounter?>;id[p]=<?=$row["Id"]?>;<?phpCounter = phpCounter + 1;}?>

also try decalering the p variablep=0; ==> var p = 0;

Link to comment
Share on other sites

Ah, I think I see the mistake. In the JavaScript code, where you declared the arrays, you need to add id = [], because otherwise JavaScript doens't understand what "id[p]" is. Also, instead of using p = p+1, just type p++.I recommend re-writing the code, because it's generally not a good idea to have the PHP and HTML mixed up. In other words, try to keep them separate, and instead have funtions to echo the JavaScript.

Link to comment
Share on other sites

you need to add id = [],
No you don't. FatalError (which you just made) do you actually know JavaScript?I notice soething that may be the problem. you need to change new array() to new Array()Believe it or not hte uppercase A makes a difference...I should have noticed that sooner.
Link to comment
Share on other sites

Yes, because new Array() is a function. That's uber_ important. But because there's so much php interacting with javascript and html, there's many problems possible. :S

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...