Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
php is run on the server, so the page needs be reloaded before having the Session set – Damien Pirsy Sep 30 '11 at 9:24
The session is getting set using AJAX. So no need to reload the page for setting session. – ssdesign Sep 30 '11 at 9:27
You should add those details, then – Damien Pirsy Sep 30 '11 at 9:28

5 Answers

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

<? 
if(isset($_SESSION['current'])) {header("location: step2.php"); exit;}
?>
share|improve this answer
In that case, how do I execute the "else" statement? --edit-- Ok I think I can try... – ssdesign Sep 30 '11 at 9:46
@ssdesign: You don't need to. – hakre Sep 30 '11 at 10:14

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.

share|improve this answer
The jQuery Ajax call is working. I dont know what you mean by "that is not how it works". Because session is getting set through an Ajax call. – ssdesign Sep 30 '11 at 9:37
I don't see any jQuery AJAX call in your code. When you have a javascript section of code - it cannot contain PHP code (your code snippet mixes them and is therefore wrong). They don't mix. The only way you get to mix them is by making requests to the server (via AJAX requests) that can then run PHP code. – Paul Sep 30 '11 at 9:56
I didnt put the Ajax part of the code here because it is working fine. I have a seperate function that does the session setting. This particular function is used by another button which when pressed should check if the session is set and should do something accordingly. The way I set my session can be found here: pastie.org/2616055 if you also want to see the PHP code of setSession.php, let me know. – ssdesign Sep 30 '11 at 10:01
Here is the PHP code of setSession.php : pastie.org/2616061 – ssdesign Sep 30 '11 at 10:03
@ssdesign: Paul is right, you're making your life more complicated than it needs to be. Obviously you have already problems to understand what's going on and not the slightest clue how the different layers exactly interact with each other. Keep things apart to gain more knowledge an power. – hakre Sep 30 '11 at 10:15

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.

share|improve this answer
I am already using jQuery AJAX solution to set the session variables. They are getting set properly. Only thing is, when I am calling the same javascript after the session is set, its still not going to the right loop. – ssdesign Sep 30 '11 at 9:28
You still don't get it. The $_SESSION['current'] you're using, is written once and only once. The Javascript isn't updated. After that, even if you update the session variable, the piece of JS you've got still contains the old value, since the $_SESSION part is not being evaluated by the PHP interpreter anymore. It's already been through that stage, and now resides statically in your browser. – CodeCaster Sep 30 '11 at 10:00
oh, so then how can I retreive the session once it has been set? Do I have to make another AJAX call to retrieve it? – ssdesign Sep 30 '11 at 10:07
This discussion might lead me somewhere.... – ssdesign Sep 30 '11 at 10:08
You could create a variable in javascript which you initially set with myVar = <?=$_SESSION['current']?>;, and if you would like to re-evaluate it, you set it like myVar = doAjaxCall('GetCurrentSession') or so. :) – CodeCaster Sep 30 '11 at 11:08

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').

share|improve this answer

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

share|improve this answer
session_start is there. As I mentioned the session is getting set properly. – ssdesign Sep 30 '11 at 9:33
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – oezi Nov 19 '12 at 1:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.