Sending PHP varialbe to javascript?

Discussion in 'PHP' started by Panjabi, Feb 5, 2008.

  1. #1
    Hi, I have a javascript timer which runs from x seconds till 0, what I want to do is set the starting value set from an IF statement, how can this be done?
     
    Panjabi, Feb 5, 2008 IP
  2. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Something like this?

    
    <html>
    <body>
    <script lang="javascript">
    <?
    if($thing){
    $yourPhpVariable = 50;
    }else{
    $yourPhpVariable = 10;
    }
    echo "var PHPVar = " . $yourPhpVariable;
    ?>
    if(PHPVar){
    alert(1);
    }
    </script>
    </body>
    </html>
    
    Code (markup):
    Something along those lines?
     
    ToddMicheau, Feb 5, 2008 IP
  3. powerspike

    powerspike Peon

    Messages:
    312
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    if you want to interactively pass php data into a page (of any type) without it reloading, you'll need to use ajax style requests.
    If your passing it though just once, it seems fine.
     
    powerspike, Feb 5, 2008 IP
  4. Panjabi

    Panjabi Peon

    Messages:
    362
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The data just needs to be passed once, per page load.

    ToddMicheau, that is exactly what Im looking for, could you explain the part below, as to what it means?

    if(PHPVar){
    alert(1);
    Code (markup):
     
    Panjabi, Feb 5, 2008 IP
  5. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #5
    Yeah, either use AJAX or use the $_GET[] global variable.
     
    Kaizoku, Feb 5, 2008 IP
  6. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #6
    I just included that piece of code so you could tell if the variable has a value or not (If the variable is anything but 0 the IF statement will return true and run the block), you can erase that and put in your own code. =]
     
    ToddMicheau, Feb 5, 2008 IP