Hi all....

Ok, so I've got 3 components, all working independently:

1) Simple HTML page
2) PHP Script (which produces 3 arrays)
3) Javascript (which processes 3 arrays)

Now, I need to hook them all together through the HTML. The PHP should be called on the loading of the page, and the results pushed into the Javascript.

Would anyone be able to show me how to hook this all together?


Recommended Answers

All 7 Replies

Hi Todd,

Need a bit more info - why don't you post the code?

Hi... thanks for the offer to assist!!!

Basically, I want to do a few seemingly simple things.

(1) User loads the HTML
(2) HTML triggers PHP which does some funky SQL, and basically produces 3 arrays.
(3) Take these Arrays, and feed them into the client Javascript.

What it actually does is the PHP multi-dimensional arrays, that the Javascript then uses to drive a little 'auto-filling' series of drop down boxes.

I have the PHP extracting the correct Arrays. And I have the Javascript working with the same Array architecture (just hard-coded in).

Essentially - I just need to understand how to:

(1) Call the PHP (is this from the HTML or Javascript)
(2) How to OUTPUT the array results from the PHP
(3) How to feed these into the Javascript.

I DO NOT need this to be dynamic, the SQL can run on load, and then never need to be called again, as the Javascript can dynamically navigate around the Arrays.

Appreciate assistance, and hope the above is clear. Have not posted the PHP/Javascript code, as that's all app specific. It's the "glue" that I can't decipher....


You have at least two options, as follows:

Include the html in the PHP file, such that the user loads "index.php" instead of "something.html". It would look something like:

function dosomething(arrayData) {

// Get stuff from database and place in array
echo "the stuff from the database in such a way so that it is passed to the Javascript function dosomething()";
// Something like:
echo "<script>dosomething($Data)</script>";

The other option is to use AJAX, which may suit you better. There are many articles on the subject with examples all over the web. Do a Google on somethng like "PHP AJAX" and you will get plenty of stuff.

A good one seems to be

Ok, so took this opportunity to learn a new skill... AJAX. Got it all up and running pretty quickly, very cool! I can pass standard text back to the HTML, but am struggling with the arrays.

Can you point me at a tutorial for PHP/AJAX Array Handling?

Ideally, I would want to send back the 3 arrays, but these get into 3 level nesting, and I'm figuring that the code will get VERY messy to decipher these. So, seeing as I can now do live queries on my Db, I can split up the application to do live calls to PHP (will break up the script to seperate SQL PHP files) and only ever have to pass back one normal array.

I guess I will need a Tut of how to pass variables INTO the PHP via AJAX as well, if I go with this solution.


So.... am I correct when I read somewhere that AJAX will basically return HTML string. So... there would be no such thing as an Array Structure... correct? All I can ever get is a string... correct? So to "fake" an Array I would need to create a delimited list from the PHP, echo that as a string, and then use the HTML/Javascript to iterate over the string to build an array?

Thats the only solution I can think of.... seems overly complex, and surely there must be some built in commands in PHP/Javascript to handle such data passing in a standardised fashion.


Member Avatar for diafol

If I stick my nose in, don't break it off!

How about using json? Get your php file to return json values to the js script, which then parses this into meaningful html for replacement?

BTW: libraries/frameworks can handle the ajax object for you with a minimum of fuss (try prototype [prototypejs.org] or jQuery).

The prototype library (I don't use much jQuery, but I assume it is equally adept), can get handle the response and update a containing tag (div, textarea etc) with about 3 lines of js. The downside is that you have to load the whole library, although they're quite small.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.