0

I've read about it but all the solutions doesn't help me. I've already added this to my .htaccess file:

<FilesMatch "\.(js)$">AddHandlerapplication/x-httpd-php .js</FilesMatch>.

That's my javascript code:

$('#calendar').fullCalendar({
      header: {
        left: 'prev,next',
        center: 'title',
        right: 'today'
      },
      defaultDate: "<?php echo $current ?>",
      editable: false,

But nothing appears then. I'd be grateful if anyone knows the solution.

11
  • @PatrickEvans that's not true when there's only one line of code Commented Oct 7, 2017 at 17:34
  • Why you wanna use PHP here? Just go defaultDate: new Date(),. Commented Oct 7, 2017 at 17:35
  • @PatrickEvans Thats not my whole actual code, its just a part of it. $current has just been defined before. Commented Oct 7, 2017 at 17:35
  • @connexo Later I want to print values from my database but i want first to learn how to print a php variable to do it then Commented Oct 7, 2017 at 17:36
  • 2
    Can't you use a PHP file to produce the JS code? and then include it using <script src="./myscript.php"></script> Commented Oct 7, 2017 at 17:42

3 Answers 3

1

I can suggest an alternative way to generate your JS code. By using a .php file, for example: myscript.php

// Set the header type to JS
header('Content-Type: text/javascript');

$value = 'Something here';

echo "alert('$value');";

and then just include it by using:

<script src="./myscript.php"></script>
Sign up to request clarification or add additional context in comments.

Comments

1

Set $current to javascript variable in php file and then include this js file. page.php

<script type="text/javascript">
     var currentDate = <?php echo $current; ?>;
</script>

<script src="script.js"></script>

In script.js

$('#calendar').fullCalendar({
      header: {
        left: 'prev,next',
        center: 'title',
        right: 'today'
      },
      defaultDate: currentDate,
      editable: false
});

1 Comment

how does this gonna help he is talking about php in .js file
1

The complete solution is :

1- creating file :

<?php
header("Content-type: application/javascript");
?>
var abc=0;
<?php echo "var babc='xyz'"?>;

2- saving file by this format :

filename.js.php

3- adding rewrite rules to .htaccess

RewriteRule "^(.*)\.php\.js$" "$1.js.php" [NC,NE,L,QSA]

4-including .js file as :

<script src="filename.php.js"></script>

the file name format need to be unique to be determined by rewrite rule, in this example i used :

FileName.php.js  for client side
FileName.js.php  for server side

Comments

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.