0

i have confuse with when pass JavaScript variables to PHP variable .then i have php session name example_servey i have 3 button with jq attr .when click button it fire with JQ click event and pass attr value in to condition i need to get this condition value in to session value & update session

<?php
session_start();
if(!isset($_SESSION['loop_survey_2016'])){
  $_SESSION["example_servey"] = "0"; 
}?>



<button class=" click_btn" data-status="0">btn 01 </button>
<button class=" click_btn" data-status="1">btn 02 </button>
<button class=" click_btn" data-status="2">btn 03 </button>


<script type="text/javascript">
$( document ).ready(function(e) {
   $( ".click_btn " ).on( "click", function() {
      var status =  $(this).attr("data-status");
      var session_val = '' ;
    if (status == '0') {
        session_val = 'empty';

    } else if(status == '1') {
        session_val = 'pending';

    }else if(status == '2'){
        session_val = 'complete';
    }
     <?php $_SESSION['example_servey'] ?> = session_val;
}); 
});
</script>

have any method with pure JS

3
  • 2
    You would need Ajax to update the PHP variable via Javascript. You can't run PHP code after the page is loaded without requests. Commented Mar 16, 2016 at 12:38
  • Please read this first. programmers.stackexchange.com/questions/171203/… Commented Mar 16, 2016 at 12:41
  • FYI: You have two status=== 1 checks Commented Mar 16, 2016 at 12:51

2 Answers 2

1

As PHP is server side and JavaScript is client side, you cannot do that. If you want to do so, you should use jQuery and Ajax function, to "silently" send your value to the server.

Also you can see an example here : How to insert javascript value into the php session variable or Set Session variable using javascript

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

Comments

0

PHP is a server side language, while Javascript is a client side language, so to achieve what you are looking for, you need to send the information to the server via a request, the simplest and fastest way to do it is by submitting a form and taking the form parameters with php on page reload.

so javascript would be:

<form action="#" method="post">
<input type="hidden" value="<script>document.write(variable)</script>" name="my_var" />
<input type="submit">
</form>


<script type="text/javascript">
$( document ).ready(function(e) {
   $( ".click_btn " ).on( "click", function() {
      var status =  $(this).attr("data-status");
      var session_val = '' ;
    if (status == '0') {
        session_val = 'empty';

    } else if(status == '1') {
        session_val = 'pending';

    }else if(status == '1'){
        session_val = 'complete';
    }
     //Remove this line 
    <?php $_SESSION['example_servey'] ?> = session_val;

        $.post( "YOUR_PHP_FILE.php", { my_var : session_val } );
      }); 
    });
    </script>

And php would be:

<?php 
$javascript_var = $_POST["my_var"];
// Now you can use $javascript_var as a PHP variable
?>

4 Comments

document.write? How will document.write help??
Document.write will output the variable into the form, then, the value would be passed as a POST variable from the form. This is one way to achieve this. If you dont want to print html, you can always use AJAX calls.
And that will run when the page has loaded so how will that put the updated variable in the form when clicked?
Please see the updated anserwer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.