0

This JavaScript code was in a PHP file, and I need to put this JS code into a .js file.

<script>
  $j(".fresh").click(function(){
    $j(this).html('<span><?php echo $this->lang_arr['my_new_func']; ?></span>');

    $j.post(  "<?= site_url('website/func_resh') ?>",
              { id          : "<?= $webfun_one_arr[id] ?>",
                website_id  : "<?= $web_one_arr[id] ?>"
              }
    );
  })
</script>

I know this code <?php echo $this->lang_arr['my_new_func']; ?> in the JavaScript format is {$this->lang_arr['my_new_func'];}

But I don't know how I change <?= site_url('website/func_resh') ?> and <?= $webfun_one_arr[id] ?> into JS format.

Thanks for any answer.

6
  • I'm a bit confused. Are you trying to generate a .js file with a PHP script, or are you trying to port a PHP script into JavaScript? Commented Sep 30, 2011 at 2:59
  • well that depends what he php function site_url() does. Commented Sep 30, 2011 at 2:59
  • @Justin the js code is in the php file, I'm trying to put them into a seperate .js file Commented Sep 30, 2011 at 3:03
  • Is this js file that has php in it included via a <script src= tag? Commented Sep 30, 2011 at 3:04
  • @ctcherry yes, so I need to remove the php label from the js file to make the js work Commented Sep 30, 2011 at 3:08

2 Answers 2

2

Interpolating PHP and JavaScript is a big can of worms that you should avoid opening. IMO the best way to deal with this is to use PHP to output a JSON object, which it has built-in support for, and then use the object in your JavaScript. This way you only have to put one tiny snippet of PHP in a <script> tag and you don't have to try to make the server parse .js files as PHP. Something like this:

In your PHP:

<?php $my_vals = array(
        'myNewFunc'     => $this->lang_arr['my_new_func'],
        'postUrl'       => site_url('website/func_resh'),
        'postId'        => $webfun_one_arr['id'],
        'postWebsiteId' => $web_one_arr['id']
      );
?>
<script>
  $j.getScript('/path/to/your_script.js', function() {
    myFunc(<%= json_encode($my_vals); %>); // turns your PHP array into a
  });                                      // JavaScript object automatically
</script>

Then, in your_script.js:

// Look, Ma, no PHP!
function myFunc(someJson) {
  $j(".fresh").click(function(){
    $j(this).html('<span>' + someJson.myNewFunc + '</span>');

    $j.post( someJson.postUrl,
      { id          : someJson.postId,
        website_id  : someJson.postWebsiteId
      }
    );
  });
}
Sign up to request clarification or add additional context in comments.

Comments

2

Don't. Make it a .js.php file that returns a content type of text/javascript, and includes the other relevant PHP scripts.

2 Comments

How do I "include" the php script? What is the code? I just don't know how to quote it in js=_=
You include with include() and the like. You don't do it in JavaScript, you do it in PHP and emit JavaScript.

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.