Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing PHP code inside a .js file

I'm trying to get a bit of PHP to execute inside of a .js file, but obviously don't know how to do it properly.

Basically the code is adding some HTML tags to my page, which I am using for a slideout contact form. However the contact form itself is done in Wordpress by a shortcode. So I'm trying to get the shortcode to work inside the code that makes the form slide out.

var phpTest = '<?php do_shortcode("[contact-form-7 id=\'1088\' title=\'Live Chat Form\']"); ?>';

// add feedback box
$this.html('<div id="fpi_feedback"><div id="fpi_title" class="rotate"><h2>'
    + thisSettings.title
    + '</h2></div><div id="fpi_content">'
    + phpTest
    + '</div></div>');

If I change the variable "phpTest" to a regular string with text, it shows up fine, I'm just not sure how to execute the php in this instance.

like image 217
KVDD Avatar asked May 09 '14 21:05

KVDD


3 Answers

EDIT: The script type should always be text/javascript as application/javascript may break compatibility with older browsers (I think IE8 and earlier will ignore the script tag with that type). The Content-type, however, should still be application/javascript.


You can't mix in php into a .js file, the way you would with a normal .php file, since the .js file is not preprocessed by the .php engine the way your .php files are.

The most typical way to do this--including php in a js file--is to just have inline JS (stuff between <script> tags) in your .php file. Then it will work as expected.

Another way to go is to make a js file within php, so start your .php file with

<?php
header("Content-type: application/javascript");
?>
var jsvar='something';
var othervar='<?php echo $phpVar; ?>';
//<?php //some php ;?>

and then include it on your page like this

<script src='myjs.php' type='text/javascript'></script>
like image 138
chiliNUT Avatar answered Oct 28 '22 17:10

chiliNUT


I commented saying your question is not possible, however it is indeed possible. This is not recommended, but if you're not worried about the possible security holes, here is a way to accomplish your question:

Create a .htaccess file in the same directory as your .js files, with the following:

AddHandler application/x-httpd-php .js

<FilesMatch "\.js$">
    Header set Content-Type "text/javascript; charset=utf-8"
</FilesMatch>

This will execute your JavaScript files as PHP, then fix the Content-Type for .js files automatically (because they're seen as text when you add the php handler).

Note: While this is a quick and dirty solution, a better way would be to use Ajax (see chiliNUT's Answer)

like image 42
Nahydrin Avatar answered Oct 28 '22 15:10

Nahydrin


You can do a ajax post request to a .php file. It is a little dirty trick but it works

$.post('thePhpFile',{var1:var1,var2:var2},function(result){
    //Code to do with the resulted... like $("#div").html(result);
});

With that trick, you can basicaly do anything you want

like image 45
Jayo2k Avatar answered Oct 28 '22 16:10

Jayo2k