#1
  1. No Profile Picture
    JRM
    Contributing User
    SEO Chat Discoverer (100 - 499 posts)

    Join Date
    Jul 2003
    Posts
    108
    Rep Power
    16

    Question Dynamic .js file using PHP


    Can I put php in a .js file? I understand that php is server-side and js is client-side. I want to call a php function and add data to the .js file, dynamically (all reesulting data would be servered to the user).

    Example:

    I have a .js function that prints specials. I want to be able to have the specials in a database for ease of updating the prices and adding more specials.

    Is this possible?

    Thank you,

    JRM
  2. #2
  3. No Profile Picture
    Posts: 20, 574 (I wish!)
    SEO Chat Adventurer (500 - 999 posts)

    Join Date
    Jun 2004
    Posts
    764
    Rep Power
    15
    Originally Posted by JRM
    Can I put php in a .js file? I understand that php is server-side and js is client-side. I want to call a php function and add data to the .js file, dynamically (all reesulting data would be servered to the user).

    Example:

    I have a .js function that prints specials. I want to be able to have the specials in a database for ease of updating the prices and adding more specials.

    Is this possible?

    Thank you,

    JRM
    its possible! Not sure how to do it, will test and post code here when or maybe if i get it to work

    Dan
  4. #3
  5. No Profile Picture
    JRM
    Contributing User
    SEO Chat Discoverer (100 - 499 posts)

    Join Date
    Jul 2003
    Posts
    108
    Rep Power
    16
    Dan,

    Great! I'm testing to figure it out too, not much yet. Whom ever finds it, post the solution, others may want to know too!

    JRM
  6. #4
  7. No Profile Picture
    JRM
    Contributing User
    SEO Chat Discoverer (100 - 499 posts)

    Join Date
    Jul 2003
    Posts
    108
    Rep Power
    16
    Here is how to run an external php script to do something within a .js file:

    in your .js file type in the following:
    document.write("<SCR" + "IPT SRC=\"/includes/test.php\" TYPE=\"text/javascript\"><SCR" + "IPT>");

    and yes keep the SCR" + "IPT just like that or wont work, then you can run php files on the load or the js file. I haven't tested it for the purposes I was looking into, but I bet it will work just fine!

    I found it here: http://forums.devshed.com/showthread.php?s=&threadid=16326&forumid=5

    Thank you,

    JRM
  8. #5
  9. No Profile Picture
    JRM
    Contributing User
    SEO Chat Discoverer (100 - 499 posts)

    Join Date
    Jul 2003
    Posts
    108
    Rep Power
    16
    Just tested it with connecting to a database, works like a charm.

    JRM
  10. #6
  11. No Profile Picture
    JRM
    Contributing User
    SEO Chat Discoverer (100 - 499 posts)

    Join Date
    Jul 2003
    Posts
    108
    Rep Power
    16
    Does anyone know how to pass a variable to the php script? I have tried test.php?var=test, and it doesn't seem to work.

    I'll try again though. I am trying to do sessions and cookies in the php file, which I guess I can't do, according to the fore-mentioned thread. So I may need to rethink that.

    Thank you,

    JRM
    Last edited by JRM; Sep 10th, 2004 at 03:27 PM.
  12. #7
  13. No Profile Picture
    JRM
    Contributing User
    SEO Chat Discoverer (100 - 499 posts)

    Join Date
    Jul 2003
    Posts
    108
    Rep Power
    16
    I figured what was going on. The referer is the page accessing the php.

    Thank you,

    JRM

Similar Threads

  1. Dynamic site map script PHP
    By dougedoug in forum Web Design, Coding and Programming
    Replies: 8
    Last Post: Sep 6th, 2004, 06:52 PM
  2. SSI: Why and How to Use Server Side Includes
    By amabaie in forum SEO Chat Articles
    Replies: 0
    Last Post: May 20th, 2004, 09:07 PM

IMN logo majestic logo threadwatch logo seochat tools logo