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

channasmcs
  • 1,104
  • 12
  • 27
  • 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. – Phiter Mar 16 '16 at 12:38
  • Please read this first. http://programmers.stackexchange.com/questions/171203/what-are-the-differences-between-server-side-and-client-side-programming – apokryfos Mar 16 '16 at 12:41
  • FYI: You have two status=== 1 checks – epascarello Mar 16 '16 at 12:51

2 Answers2

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

Community
  • 1
  • 1
r4phG
  • 470
  • 8
  • 16
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
?>
patito
  • 530
  • 2
  • 13