Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    passing php to javascript arrays

    hi can u help me to pass php values to javascript array,
    actually I have many drop down boxes
    I need onchange of one of them to change the values of the others according to the value of the first one
    and 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 problem
    this 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?

  • #2
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A few possible problems:

    Firstly (probably the main problem), in your PHP while-loop, you are retrieving $row using mysql_fetch_array() with the MYSQL_NUM bit-setting, yet later you seem to address a particular column in the row using a field name:
    Code:
    id[p]=<?=$row["Id"]?>;
    MYSQL_NUM means that each column is address by a number whereas MYSQL_ASSOC allows you to address a column by the field name. You may also want to consider using mysql_fetch_assoc() instead of mysql_fetch_array()...

    Secondly, if $row["Id"] holds a string value in the DB, then you will need to include quotes around it in the js script:
    Code:
    id[p]='<?=$row["Id"]?>';
    Thirdly, I assume you already realize that the PHP will be run before this function is ever called... You will only be searching the db when the page is first loaded (it doesn't look like you were trying to plug in some js value into your query, which is why I assumed you already realized this possible issue).

    Finally, you may consider using id[id.length] instead of 'p' to create the next item in your array... The length always gives 1+ the highest idex value in the current array, and it automatically updates when the array gains a new member.
    If you want answers, write a smart question.

    Yes, someone probably does know how...

    Oh, and if you want to learn, STFW!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •