mona Posted December 20, 2005 Share Posted December 20, 2005 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 More sharing options...
aspnetguy Posted December 20, 2005 Share Posted December 20, 2005 <?$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 More sharing options...
mona Posted December 20, 2005 Author Share Posted December 20, 2005 I tried what u said nothing happened, as I told before may be the problem in the arrays , can u check up if there is an error in declairing those arrays Link to comment Share on other sites More sharing options...
aspnetguy Posted December 20, 2005 Share Posted December 20, 2005 The arrays are declared but I see that they are never given a length...I don't know if that makes a difference.I can't test your code on my machine because I am not running php.I can't be much more help, sorry. Link to comment Share on other sites More sharing options...
mona Posted December 20, 2005 Author Share Posted December 20, 2005 thanks anyway Link to comment Share on other sites More sharing options...
FatalError Posted December 20, 2005 Share Posted December 20, 2005 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 More sharing options...
aspnetguy Posted December 20, 2005 Share Posted December 20, 2005 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 More sharing options...
Chocolate570 Posted December 20, 2005 Share Posted December 20, 2005 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 More sharing options...
mona Posted December 21, 2005 Author Share Posted December 21, 2005 thank u actually may be u r right I'll try it coz javascript functions are case sensitive as I think. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now