开发者

<?PHP ?> tag can be used in javascript

Can we use the<?php ?> tag in javascript? If yes then m开发者_JAVA百科y next question is; can we use session in this tag? Example:

success:function(data)
{                               
    if(data)
    {
     <?php 
      if(isset($_SESSION['UserId']))
       { ?>
            window.location.href="coll_delivery_det.php";
       }
      else
       { 
    ?>                                                     window.location.href="courier.php";  <?php   
        }
     }  ?>
}


If I understand what your looking to do you would want to use php to echo out your javascript commands.

success:function(data)
{                               
    if(data)
    {
     <?php 
      if(isset($_SESSION['UserId']))
       { 
            echo "window.location.href=\"coll_delivery_det.php\";";
       }
      else
       { 
            echo "window.location.href=\"courier.php\";";   
       }
     }  ?>
}


Yes. But only if the page is executed as actual PHP page.

If you use PHP code through your javascript or HTML I suggest using templatish statements, like so:

<?php if ($someVariable) : ?>
var i = 0;
<?php else : ?>
var i = 2;
<?php endif; ?>

It'll be much more clear what statements are closed. Instead of the following:

<?php if ($someVariable) { ?>
var i = 0;
<?php } else { ?>
var i = 2;
<?php } ?>


In fact, you can't use PHP tags in JavaScript.

You can only generate either whole JS code or only some data for it using PHP.


The specific solutions posted here address your current situation, I'd just like to touch on the reasoning behind them.

Javascript logic is executed in your browser. PHP logic is executed on the server.

Embedding conditional PHP statements directly in javascript won't do what you want, but you can use PHP to generate the javascript your browser needs to execute.


Yes as long as you are doing this within a file that will be executed as PHP but your code is syntactically incorrect from what I can see. Try this instead:

success:function(data) {                               
    if(data) {
     <?php if(isset($_SESSION['UserId'])) { ?>
          window.location.href="coll_delivery_det.php";
      <?php } else { ?>
          window.location.href="courier.php";
      <?php } ?>
     }
}

It is worth noting that you cannot go the other way. Javascript variables cannot be used in PHP code as by the time the users browser executes the Javascript the PHP execution cycle is terminated. The only way to pass it back this way would be to make an Ajax request.

Also the PHP will only be run once each page load so using your code if $_SESSION['UserId'] is set then the users browser would just see:

success:function(data) {                               
    if(data) {
          window.location.href="coll_delivery_det.php";
     }
}

Otherwise if it is not set it will just be rendered from PHP as:

success:function(data) {                               
    if(data) {
          window.location.href="courier.php";
     }
}

In this way javascript is generated from your PHP code.


Yes, php can generate anything: html, css and JavaScript as well. So you can do something like that on your .php page:

function() {
  <?php if($data) { ?>
    window.location.href="coll_delivery_det.php"; 
  <?php } else { ?> 
    window.location.href="courier.php"; 
  <?php } ?>
}

However you need to remember that PHP is generating JavaScript as any other text, so you can't use Javascript variables in PHP script. eg. something like that will not work:

function test(){
  var r=1;
  <?php if ($r==1){ ?>
     alert('r = 1');
  <?php }?>
}


If you're using apache you can create a .htaccess file in your javascript directory with the following contents:

AddHandler php-cgi .js

This will make your .js files run as php, but retain its original extension.


the easiest way is to create a page that generates a dynamic javascript and includes the header for the javascript.

mysite.com/js.php

<?php header("Content-type: application/x-javascript");?>
success:function(data) {                               
    if(data) {
     <?php if(isset($_SESSION['UserId'])) { ?>
          window.location.href="coll_delivery_det.php";
      <?php } else { ?>
          window.location.href="courier.php";
      <?php } ?>
     }
}

but you probably dont want to do that.. browsers save the included javascript files on cache, and you could have some problems with that..

the best way to proceed is to have your js files separated and and pass some parameter to the functions or classes using a <script> tag inside your <header>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜