1

Inside a PHP/HTML page i'm trying to start a session in php and then set a value to a session variable, and i'm trying that inside a Javascript function, and in this function there is a if statement, but the php code inside the if statement runs automatically when i go to the page immediately, when i want the code to run only when the user accept the terms...

Here is my code :

HTML :

<label>                                        
    <input type="checkbox" name="terms" id="terms-checkbox">             
    <font color="#090909" size="+1">I agree to the Terms and Conditions.</font>
</label>

Javascript :

if(document.getElementById("terms-checkbox").checked){
    <?php session_start(); $_SESSION["ITA"] = "Yes"; ?>
    window.open('DL.php?App=STOPit');
}

So when i open the php page that contains that previous Javascript code, the $_SESSION["ITA"] variable will be equals to "Yes" even that the condition is not met yet.

Hope someone can help me with that.

Mousa Alfhaily
  • 1,260
  • 3
  • 20
  • 38
  • 1
    It's natural. You have to put php in a php conditonal not a JavaScript conditional. – Robert Mar 26 '17 at 22:01
  • 1
    You mean you are trying to run server code in the client? Never gonna happen. PHP produces your document, then you need to fire a requestb to interact with the server. – Ruby Racer Mar 26 '17 at 22:02
  • Sounds right, but how i'm gonna do it, inside `DL.php` i'm checking the session variable and download a file when it's only equals to "Yes" and then reset its value to "No". – Mousa Alfhaily Mar 26 '17 at 22:10

3 Answers3

4

That's not how PHP and Javascript work.

PHP runs on the server before anything else. JS runs on the client side.

Without further information:

I suppose your JS code is not a file, but a inline script on the same HTML/PHP page. When your user requests the HTML page, PHP parses it, including the tags inside your JS code.

If you want PHP to do something based on a user action on the browser (document.getElementById) you have to take other routes. Maybe using Ajax, for example -- so your PHP code can do something.

noderman
  • 1,934
  • 1
  • 20
  • 36
3

For your better understanding php runs on the server side and javascript runs on the client side.

What you are trying to do is possible if you submit the data using a form or you have to make an ajax call using javascript.

Below is one way of doing something similar.

<?php
    session_start();

    if(isset($_POST['terms'])) {
        $_SESSION["ITA"] = "Yes";
        header('Location: DL.php?App=STOPit');
    }
?>

<form action="<?php $_SERVER["PHP_SELF"] ?>" method="post">
    <label>
        <input type="checkbox" name="terms" id="terms-checkbox">
        <font color="#090909" size="+1">I agree to the Terms and Conditions.</font>
        <input type="submit" value="Submit">
    </label>
</form>
AZee
  • 520
  • 1
  • 6
  • 22
1

Always start your PHP session_start() on the top of your PHP script file.

<?php

session_start();

?>

Here you can continue with your HTML/CSS/JavaScript codes
but the above is mandatory in order to start a session within
your PHP project.

Otherwise you'll be sending header information after the output buffering happend, and you'll end up with an error like this:

Warning: Cannot modify header information - headers already sent by (output started at /some/file.php:12) in /some/file.php on line 23

When you have properly initialized a session handler as I described, you can use or do:

$_SESSION['item-name'] = 'value'; // to set a value of a specific session item

echo $_SESSION['item-name'];      // retrieve it's value and output it

unset($_SESSION['item-name']);    // to delete it from the $_SESSION container
Zlatan Omerović
  • 3,863
  • 4
  • 39
  • 67