Jump to content

How to include a .php file inside of a .js file?


swampone

Recommended Posts

I have a php file that generates a string that I need to use in a .js (javascript)file. Being that php developers sometimes using javascript with php, Im hoping someone can help me with this, cause i dont know any javascript.

//This is the varible inside the .js file
var suggestionText = "I need to be able to include my string generated by the php file here...";

 

 

 

 

 

 

Link to comment
Share on other sites

This isn't actually a php question, it's a javascript one.

 

Basically in order to do this you need to use some mechanism in javascript to query this page in the background. There are plenty of javascript frameworks (jQuery, MooTools etc.) that provide something called an "AJAX" implementation.

 

Basically, you can provide a URL to these javascript methods and it will essentially "visit" the page in the background. It will then return whatever string you want.

 

For example, in jQuery, you would do something like this:

$.ajax({
  url: 'path/to/your/file.php',
  dataTye: 'text', 
  success: function(data) {
    // data is the string from file.php
  }
});

 

Now you would just need to modify your .php file to echo whatever you want to display

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.