Jump to content

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


swampone
 Share

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

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • 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.