1

How do I enter a <? echo "hello"; ?> in a .js file. This is a jquery app, therefore the js file.

Thanks Jean

1

4 Answers 4

16

You would only be able to do this if the PHP interpreter is configured to run on *.js files, which by default it won't be. Quite honestly, I wouldn't recommend this behavior.

What I'd do instead is something like this (This method can be used for CSS files, too.):

<script type="text/javascript" src="js.php"></script>

js.php

<?php
//GZIP the file and set the JavaScript header
ob_start("ob_gzhandler");
header("Content-type: text/javascript");

//Set a JavaScript variable based on PHP work
echo 'var logged_in_user = "'.$_SESSION['username'].'";';

//Require an external script
require_once($_SERVER['DOCUMENT_ROOT']."/path/to/jquery.js");
?>

//More Javascript functions and code here
$(document).ready(function() {
  $('#mydiv').tipsy();
});

<?php
//Flush the output buffer
ob_end_flush();
?>

I personally do this for many reason.

  1. I have many jQuery files I want to include, but I don't want my browser doing 5+ HTTP requests. Including them all in one file means less HTTP requests.

  2. GZIP! I'm significantly reducing the size of the file be transferred and that speeds things up for the visitor.

  3. It's a central location to add, remove, or modify my JavaScript for the whole site. I can even use $_GET checks to make certain scripts conditional based on how I wrote the <script> tag.

    For example, <script type="text/javascript" src="js.php?var=1"></script>. I can then check $_GET['var'] within the js.php file.

Sign up to request clarification or add additional context in comments.

4 Comments

This is the best answer of those given. The URL you request doesn't have to have a .js file at the end of it, as long as it returns js with the correct headers (as shown above).
Fantastic solution, kills 3 birds with one stone (consolidates js, compresses js, and allows PHP tags in js).
@endophage Though I upvoted it, how. It still is not a .js but a .php file. Why do I do this, I can simply do a include of a php file.
This doesn't seem to work. I tried it with alert and no luck.
1

You regularly don't use PHP within your JavaScript files. Javascript is a client-side language which is interpreterred in your web browser. PHP is run on the web server.

However, if you need to pass data from your PHP-code to your javascript document, you can do something like:

$js = "<script> myObject = " . json_encode($your_data) . " </script>";
print $js;

If you do this in your <head>-part of your HTML-document, you will have access to myObject in other JS files you load after that.

$your_data can be an array or any kind of object, string or integer. Look for PHP JSON around the interwebs.

Comments

0

I think is not possible to enter a php in the js file, but: try to create an element div for example or an input ...

and then use this functions to get the value of the div tag.

    function AddHiddenValue(oForm) {
       var strValue = document.getElementById("city").value;
       alert("value: " + strValue);
       var oHidden = document.createElement("input");
       oHidden.name = "printthisinput";
       oHidden.value = strValue;
       oForm.appendChild(oHidden);
    }

It come from another object form (select .. )

document.getElementById("city").value;

Comments

-1

Ok guys here is the answer

The Q: I want to input a value for a variable into a .js file, php tags are not permitted and the js would throw an error.

The A: write a

<script> <? var value_pass = echo "hello"; ?> </script> before the said .js file

In the said .js file var value=value_pass;

So there is no need to have any of the ob_end_flush.

If this is not viable please let me know.

Thanks Jean

6 Comments

You did not have your code between two back ticks (`) so it was not displaying.
This really isn't an answer to the question you posed though. You asked how to print PHP in a JavaScript file, not how to pass information (that doesn't have to come from PHP) into an external JavaScript file.
Please check the question again How do I enter a <? echo "hello"; ?> in a .js file
@Jean This works? Surely it would need to be <script> var value_pass = "<? echo "hello"; ?>" </script>
If this is the correct answer, you should select it as being so.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.