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 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2004
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Dynamic SELECT from array

    First thing is to say I don't know if this is JS or AJAX, I don't know enough about either to be sure.


    I have wriiten PHP code to extract records from a database and put them into an array.

    I have used a function to take the array into JS and have used an alert to test that the array.length matches the number of records in the database.

    At the moment each array line consists of the value of each field in the database comma separated, eg value1, value2, etc.

    I want to be able to use the first field in each array line to populate the OPTIONS and "value=" for a SELECT box.

    I want to then set other variables based on the OPTION selected.

    This is probably 2 functions.

    I do not know enough JS to do this on my own but I am reasonably good at picking things up, or making example code fit.

    Anybody out there able to give help or examples would be much appreciated

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    First thing is to say I don't know if this is JS or AJAX, I don't know enough about either to be sure.
    Well, if your JavaScript is calling a PHP script to retrieve data or compare data, then I would say that's "Ajax". Are you using XMLHttpRequest to perform this task?

    I have used a function to take the array into JS and have used an alert to test that the array.length matches the number of records in the database.
    Ok, so let me get this straight. PHP pulls records from the database, and stores them in an array. This array is handed off to JavaScript in some manner, which in turn calls the database again to compare the data? That doesn't make alot of sense.

    I want to be able to use the first field in each array line to populate the OPTIONS and "value=" for a SELECT box.
    Ok, so why not just have PHP output the markup straight from the database resultset, and not use JavaScript at all? I mean, JS would be used to trigger some form interaction, but not to handle the incoming data. You want your application to be unobtrusive, that is to say format the entire document as if the user has JavaScript disabled, and then add in hooks where JavaScript can take part in the user interface once the document has rendered.

  • #3
    New Coder
    Join Date
    Jul 2004
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply

    I am trying to take the array from PHP into JS so that I don't have to access the database again for the records.

    I would then like to use JS to set the SELECT OPTIONS based on the contents of this array.

    To get my PHP array into JS I use this code:
    PHP Code:
      print "<script type='text/javascript'>php2js('$drire','$num');</script>"
    $drire is the PHP array and $num is the record count.

    I know there are seven records in the PHP table.

    The JS function that I use is this :
    [SCRIPT]
    function php2js(drire,num) {
    alert (num);
    alert (drire.length);
    drire_array= new Array(drire.length);
    for (var i=0; i < drire.length;++i)
    {
    drire_array[i]=drire[i];
    }}
    [/SCRIPT]

    I am trying to set my PHP array, drire, into a global JS array, drire_array.

    The first problem that I have is that num=7 but drire.length=5, I appear to have lost 2 records.

    The second problem that I have is that when I try to display the array, ie echo(drire_array[i]); all that is displayed is "undefined".

    Anybody help on this would be appreciated, or am I way off course with this approach?

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,296
    Thanks
    10
    Thanked 584 Times in 565 Posts
    Code:
     print ( "<script type='text/javascript'>var drire=" . json_encode('$drire') . ";</script>" );
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #5
    New Coder
    Join Date
    Jul 2004
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the response

    I have tried the code modification you suggested but get an "undefined function" error.

    On examination I think this is because the version of PHP on my host is 5.0.51a and JSON requires version 5.2


  •  

    Posting Permissions

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