swampone Posted February 24, 2011 Share Posted February 24, 2011 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..."; Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted February 24, 2011 Share Posted February 24, 2011 You probably need to rename the .js file to .php and then do: var suggestionText = "<?php echo $your_var; ?>"; Quote Link to comment Share on other sites More sharing options...
xangelo Posted February 24, 2011 Share Posted February 24, 2011 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.