how to assign javascript variable value to php variable

前端 未结 8 1559
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 15:12

I have declared a javascript variable ,

 var myJavascriptVar = 12345;

And unable to assign that value to php variable;

8条回答
  •  醉酒成梦
    2020-11-28 15:49

    Try using ajax with jQuery.post() if you want a more dynamic assignment of variables.

    The reason you can't assign a variable directly is because they are processed in different places.

    It's like trying to add eggs to an already baked cake, instead you should send the egg to the bakery to get a new cake with the new eggs. That's what jQuery's post is made for.

    Alert the results from requesting test.php with an additional payload of data (HTML or XML, depending on what was returned).

    $.post( "test.php", { name: "John", time: "2pm" })
      .done(function( data ) {
        alert( "Data Loaded: " + data );
      });
    

提交回复
热议问题