...

View Full Version : Dynamic SELECT from array



html_amnesiac
03-28-2009, 11:53 AM
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

bdl
03-28-2009, 10:33 PM
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 (http://www.google.com/search?hl=en&q=xmlhttprequest&btnG=Search) 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.

html_amnesiac
03-29-2009, 02:58 PM
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:


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 :

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];
}}


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?

rnd me
03-29-2009, 08:22 PM
print ( "<script type='text/javascript'>var drire=" . json_encode('$drire') . ";</script>" );

html_amnesiac
03-30-2009, 02:03 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum