1

Can we use the<?php ?> tag in javascript? If yes then my 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   
        }
     }  ?>
}
5
  • 4
    if the page is .php you can otherwise no Commented Apr 8, 2011 at 12:38
  • 1
    it doesnt have to be .php, the extension has nothing to do.. Commented Apr 8, 2011 at 12:42
  • @pleasedontbelong By .php I was mean file must be executed as PHP script Commented Apr 8, 2011 at 12:45
  • its always good to specify that =D people might get confused Commented Apr 8, 2011 at 12:55
  • @pleasedontbelong: True, Sometimes it is good to understand emotions behind the words rather then means of words itself. Cheers! Commented Apr 8, 2011 at 12:58

8 Answers 8

9

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\";";   
       }
     }  ?>
}
Sign up to request clarification or add additional context in comments.

Comments

1

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 } ?>

Comments

1

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.

Comments

1

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.

Comments

1

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.

Comments

0

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 }?>
}

1 Comment

data is Javascript variable here not a PHP variable. And php variables are used as $var.
0

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.

Comments

0

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>

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.