View Full Version : PHP output effects JAVASCRIPT code... HELP

10-11-2008, 11:34 AM
Hey there,
I am developing a website in PHP. I am not great at javascript, and have been playing around with javascript/AJAX.

I have a news page, and I am using a JS/Ajax pagination script off dynamic drive. It has an external .js file, and in that file the code that controls what is on each page is something like

"page1.php","page2.php","page3.php" etc...

Now that is all fine and dandy BUT I want the amount of pages to depend on the amount of news from a mysql database....

I can use php, mysql, and a while loop to get a variable as follows:

How can I then use this in the .js file to create 'x' pages..
When I simply put the php code in the js file nothing happens.

Hope that makes sense. Help greatly appreciated..

10-12-2008, 12:38 PM
are you talking about creating an array?


10-12-2008, 02:44 PM
Yes, I want to make an array.
Basically the links for each page in this pagination script are stored in an array in the .js file.

What I want to do is make it so depending on the number of rows in a mysql db table, the array changes. So If there are 50 rows, 5 pages. 40 rows, 4 pages..

I can do that in PHP using while loops BUT how do I put PHP code into a .js file...

10-13-2008, 07:45 AM
One trick is to use php to echo the javascript

echo "<script type=\"text/javascript\">";


echo "</script>"';


alternatively, you could use php to create a js file. I tried the "src=whatever.php" in a javascript tag, and it worked fine.

Hope this helps. Displaying some of the code would help me more. :)

11-25-2009, 12:55 AM
and if you are simply talking about echoing php in javascript it's simple.

<script type="text/javascript">
alert('<? echo "this works"; ?>');