Javascript变量值到php变量[重复]

Javascript变量值到php变量[重复]

问题描述:

This question already has an answer here:

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

</div>

此问题已经存在 这里有一个答案: p>

  • 客户端编程和服务器端编程有什么区别? 5 answers span> li> ul> div>

    我迷惑了 当我将JavaScript变量传递给PHP变量时。然后我有php会话名称example_servey我有3个按钮和jq attr。当点击按钮时它会触发JQ click事件并将attr值传递给condition i需要将这个条件值输入到session 价值与价值 更新会话 p>

     &lt;?php 
    session_start(); 
    if(!isset($ _ SESSION ['loop_survey_2016'])){
     $ _SESSION [“example_servey”]  =“0”;  
    }?&gt; 
     
     
     
    &lt; button class =“click_btn”data-status =“0”&gt; btn 01&lt; / button&gt; 
    &lt; button class =“click_btn”data-status =  “1”&gt; btn 02&lt; / button&gt; 
    &lt; button class =“click_btn”data-status =“2”&gt; btn 03&lt; / button&gt; 
     
     
    &lt; script type =“text /  javascript“&gt; 
     $(document).ready(function(e){
     $(”。clickStn“)。on(”click“,function(){
     var status = $(this).attr(  “data-status”); 
     var session_val =''; 
     if(status =='0'){
     session_val ='empty'; 
     
    }否则if(status =='1')  {
     session_val ='pending'; 
     
    }否则if(status =='2'){
     session_val ='complete'; 
    } 
    &lt;?php $ _SESSION ['example_servey']?  &gt; = session_val; 
    }); 
    }); 
    &lt; / script&gt; 
      code>  pre> 
     
     

    有任何纯JS方法 p> DIV>

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

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
?>