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.

3
  • 1
    It's natural. You have to put php in a php conditonal not a JavaScript conditional. Commented Mar 26, 2017 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. Commented Mar 26, 2017 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". Commented Mar 26, 2017 at 22:10

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, you've been the first one, and i did get some knowledge right there :)
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>

11 Comments

Thanks, is there any security approaches like that?
Sorry what do you mean by that? Are you asking if this is a secure way or not?
Yes that's what i meant, sorry.
Well security is another big topic. But this is just fine if you are not working on a very large scale application. This is how we normally pass data from our html forms to php.
Congratulations! Please mark it as an answer if it helped you.
|
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

5 Comments

Thanks, i know what you said and that's right, but here it seems to work because if i checked the value of the session variable, it will be equals to "Yes", so the session is starting.
I still don't understand what you want to achieve with PHP's session inside a JavaScript callback?
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".
You should either use Cookies instead, or have an additional AJAX callback after a user changes the checkbox.
Thanks, i was thing of using Cookies too.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.