0

so $user->turnon is always the second value which is "1" when loading page

<script>
    $(document).ready(function(){
        $(".turnof").click(function(e){
            var editcase = "<?php $USER->turnon = 0 ;?>";
        });
        $(".turnon").click(function(e){
            var editcase = "<?php $USER->turnon = 1 ;?>";
        });
    });
</script>
4
  • Mh, you do know the PHP source will be parsed before the browser receives it? PHP will set $USER->turnon 1 because it is written there. And in your js variable is literally empty. Commented Aug 10, 2014 at 6:50
  • If you want to set the variable depending on click events, you have to do an AJAX request to a script exactly doing that. PHP would never notice what your browser is doing, if you're not telling your server with an AJAX request. Commented Aug 10, 2014 at 6:52
  • @CharlotteDunois So i want to edit session variable wich is $USER->turnon, could i do it with javascript?? Commented Aug 10, 2014 at 6:52
  • Do an AJAX request to a PHP script exactly doing that. Commented Aug 10, 2014 at 6:53

2 Answers 2

2

You can't call PHP from a JavaScript function directly like that. PHP code executes on your server, so it will evaluate before the page loads. Your code is the same as...

<?php
    $USER->turnon = 0;
    $USER->turnon = 1;
?>

<script>
    $(document).ready(function(){

        $(".turnof").click(function(e){
            var editcase = "";
        });

        $(".turnon").click(function(e){
            var editcase = "";
        });

    });
</script>

Since you're not using the echo function (or another printing function) in your PHP code, nothing is output to the page, so your click functions will just set your JavaScript variable editcase to be the empty string.

On the PHP side of things, you're just setting $USER->turnon to be 0, and then immediately changing its value to 1. This happens before the page loads.

You may be looking to perform an AJAX request, which allows you initiate requests to your server from a JavaScript function. The request to your server can be used to execute PHP code.

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

Comments

0

You should do it via Ajax, as stated in the comments.

<script>
    $(document).ready(function(){
        $(".turnof").click(function(e){
            $.ajax({
                url: "/ajax.php?turnon=0",
                context: document.body
            }).done(function() {
                alert('success');
            });
        });
        $(".turnon").click(function(e){
            $.ajax({
                url: "/ajax.php?turnon=1",
                context: document.body
            }).done(function() {
                alert('success');
            });
        });
    });
</script>

This is the ajax.php

//session start etc..
if (isset($_GET['turnon'])) {
    $USER->turnon = $_GET['turnon'] === '1' ? 1 : 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.