开发者

Javascript function and PHP session variable

开发者 https://www.devze.com 2023-04-09 06:45 出处:网络
I have the following script in my Body Tag inside a PHP page: <script type=\"text/javascript\">开发者_运维技巧

I have the following script in my Body Tag inside a PHP page:

<script type="text/javascript">开发者_运维技巧
function goNext()
{
    <?php
       if(isset($_SESSION['current']))
       {
    ?>
    window.location="step2.php";
    <?php
      }
       else
      {
    ?>
      alert('Please select one item to continue.');
    <?php
      }
    ?>
}
</script>

I am setting the session vaible in PHP. So when I click a link on the page, the session is set. After that, when I call this function, it still goes to the Alert.

If I refresh the page, then it skips the alert and forwards to the URL mentioned in the function.

Any idea why while I have not refreshed the page, it does not go to the URL?


Update


I am already setting the Session_start at the begining of the page. The session is getting set properly, because when I refresh, I can see it working. Thirdly, I am already using AJAX to set sessions without doing page refresh. There are other elements on the screen that respond to session change and they are working. Its only this function where the "issset" function is not detecting the session.


why don't you just simplify it with a header?

<? 
if(isset($_SESSION['current'])) {header("location: step2.php"); exit;}
?>


That is not how AJAX works. You have to keep your PHP and Javascript separate. Use PHP to write your page from the server. The javascript will make AJAX calls back to the server where you will answer them with the PHP. You'll need a tutorial to find out how to make the AJAX request - which will depend on whether you are using plain old javascript or a libarary such as jQuery or Mootools.

Edit: I am not sure exactly what you are trying to do, but I'm going to try to put what I think you want to do. (Note: I have never used jQuery so this is a big guess and is only pseudo-code - hopefully the idea is useful).

<script type="text/javascript">
function goNext()
{
    // Get the session from the server (via an AJAX call).
    $.get("getSession.php", function(sessionFromServer)
    {
       // Do stuff depending on the value of sessionFromServer

       // However, it seems odd that the php isn't doing this with 
       // header("location: step2.php");
       window.location = "step2.php";
    });
}
</script>

getSession.php - something like this:

<?php
session_start();
echo json_encode($_SESSION);
?>

This gives you an idea of what i mean by separating them.


Because the difference between serverside and clientside. Javascript executes in the browser, after the PHP script is executed on the server.

I don't exactly know what you're trying to do, but AJAX might be the answer.


You'll need to use an AJAX call to set and store the session value. You could then redirect within the PHP code using header('Location: step2.php').


dont forget session_start() at the top (or be sure that you have session auto start)

0

精彩评论

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

关注公众号