Js file communication

Hello all,

I would like to ask your opinion about this:

On a given .js file I have something like this:

animatedcollapse.addDiv('news0', 'fade=1');
animatedcollapse.addDiv('news1', 'fade=1');
animatedcollapse.addDiv('news2', 'fade=1');
animatedcollapse.addDiv('news3', 'fade=1');

However I would like to spice this up with PHP, hence, those divs that now are statically called to this js script, should now be called dynamically.

So, instead of having:

animatedcollapse.addDiv('news0', 'fade=1');
animatedcollapse.addDiv('news1', 'fade=1');
animatedcollapse.addDiv('news2', 'fade=1');
animatedcollapse.addDiv('news3', 'fade=1');

I should have something like:

foreach fieldOnMyDatabaseObject as news
   animatedcollapse.addDiv($news, 'fade=1');

But I’m not sure about the proper way to relate this client and server side scripts.

Is the json something that I should look at on this case?
What ways to we have in order to put some php variables into js files?


Dummy question it was ? :nono:

We sure can have a php to generate a href with some javascript call in it.
It will be the user agent, the responsible for dealing with it.

Thanks again Rajug,

So, one last question remains, once we generate those javascripts scripts, we need to present them to the user.

So we will construct a php page that will have html dynamically generated by php, and that html generated needs to have some javascript inside.

Something like:

foreach($news as $new){
echo "<article>";
echo "<header>".$new->title."</header>    
echo " <a href="javascript:animatedcollapse.show('new')"> ....";



My question is, will the browser understand this line:



Or should we change the header information to text/javascript ?

Thanks a lot for your help,

JSON is nothing to do with ServerSide though server side scripting can generate json format variables/strings like PHP does. So json itself is not for server side.

Thank you all,

So apparently, and without been able to deal with design patterns at the moment, the way we have to grab some server side variables and put them on script is to, actually, generate all the js script using a server side language.


I’ve never used before but, json isn’t something in between the server side and the client side language?
Does anyone know something about it ? Probably is another way for doing it?

Thanks in advance,

As far as I understood from OP’s first post, he is retrieving something from database in an array as he has given a variable ‘fieldOnMyDatabaseObject’ in foreach. So if it is from the database then I think he must use PHP to construct the JS lines in anyway.

Don’t get PHP to write this! JavaScript can handle it just fine.

for(i = 0; i <= 3; i++)
    animatedcollapse.addDiv('news' + i, 'fade=1');

Whether you write this code in another file and include that in your main file or you write it on the main file itself but it should be .php file. It is up to you how you want to manage your code.

Thanks. So, no external .js will be used ?

Instead a external .php file containing javascript code with some server side variables will be used instead?

Thanks in advance,

Couldn’t use be using a jQuery selector to find all the elements you want to do this to, rather than having PHP write JavaScript?

You must echo the JS lines as they are within the <script> tag:

echo '<script type="text/javascript">' . "\
foreach($news as $new){
    echo "animatedcollapse.addDiv('" . $new . "', 'fade=1');\
echo '</script>';