0

I have a array in php file say

<?php $authcode = array("auth"=>"jshkjashdkj");?>

Now I want to access this array in a JS file, then how can I do it?

As in .js file <?php echo json_encode($authcode); ?> wont work.

UDPATED

My custom.js file contains below code,where I have to use authcode in url:

$('#removeimage').click(function(){


              $.ajax({
                        url:'deletefile.php?filename='+$('#filename').val()+'&make='+$('#make').val()+'&model='+$('#model').val(),
                        cache: false,
                        async: false,
            dataType: "json",
              success: function(data) {
              window.location="/upload/?authcode=97A434B1-E250-490D-8CF1-4B664AB42EED&make="+data.make+"&model="+data.model+"&imagename="+data.filename;


                        },
            error: function(data) {
                alert("Service is down");
            }
                 });
});

Please notice the authcode usage, thats the place I need to use my authcode array

Please guide me.

OM The Eternity
  • 15,694
  • 44
  • 120
  • 182
  • possible duplicate of [Parse a JavaScript file through PHP](http://stackoverflow.com/questions/3943198/parse-a-javascript-file-through-php). Additionally, you can reconfigure your PHP interpreter to parse required `*.js` files as PHP code. – Álvaro González Jun 25 '12 at 11:19

4 Answers4

3

You'll need to tell php that it should process also the .js file. There are two ways for doing this:

  1. rename from file.js to file.php
  2. mess with the apache configuration to make it treat .js files as php

The first one is easier. The other change you must do is to substitute in your html the call to your js file:

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

edit: Make the file.php script output a header("Content-Type: text/javascript"); at the top of the file

Carlos Campderrós
  • 22,354
  • 11
  • 51
  • 57
  • I wil be writing my js code under ' – OM The Eternity Jun 25 '12 at 12:06
  • Using the first option, you'll rename the `custom.js` to `custom.php` and add the php code you need inside it using the normal php tags. And in the html, you'll update the call to the custom.js file, as it has been renamed, so instead of `` you'll have `` – Carlos Campderrós Jun 25 '12 at 14:34
1
<?php $authcode = array("auth"=>"jshkjashdkj");?>
<script>
  // declare a global var, then you could access it in file.js
  var authcode = <?php echo json_encode($authcode); ?>;
</script>
<script src="file.js"></script>
xdazz
  • 158,678
  • 38
  • 247
  • 274
0

You cannot access PHP variables in a JS file without using AJAX.

That said, you can make your script a PHP file that returns an application/javascript response.

Peter Olson
  • 139,199
  • 49
  • 202
  • 242
  • Exactly thats what i want to do I am creating a url in ajax success response, where on success the page needs to be direct to a page whose query string will contain authcode as parameter, guide m ehow can i do that, please see the updated question – OM The Eternity Jun 25 '12 at 11:54
0

You have several possibilities depending on your use case.

  1. You may wan't generate the .js file from PHP

    file_put_contents('somefile.js', $content);

  2. Allow PHP in .js file

    AddHandler application/x-httpd-php5 .js

  3. Put it in your layout and store your data in a global variable to let it accessible from every other js scripts.

Which methods fits your need is really up to you. But I would generally avoid the second method.

Boris Guéry
  • 47,316
  • 8
  • 52
  • 87