PHP Javascript variable help

前端 未结 3 1234
傲寒
傲寒 2021-01-27 10:47

Is there any way to pass a javascript variable to a php function or simply assign a js variable to a php variable....???

test(\"asdas\"); I need to update \"asdas\" to a

3条回答
  •  太阳男子
    2021-01-27 11:19

    You can pass value from js to PHP using ajax request or add js value to URL and then reload page.

    Variant #1 (using ajax):

    JS side (jquery)

    var js_var = 'hello';
    $.ajax({
       type: "POST",
       url: "some.php",
       data: "js_var="+js_var,
       success: function(msg){
         alert( "Data Saved: " + msg );
       }
     });
    

    PHP side

    $js_var = isset($_POST['js_var']) ? $_POST['js_var'] : '';

    Variant #2 (with page reload):

    JS side

    
    

    PHP side

    $js_var = isset($_GET['js_var']) ? $_GET['js_var'] : '';

提交回复
热议问题