weixin_33708432 2018-07-04 18:17 采纳率: 0%
浏览 19

Ajax到PHP $变量

I try to pass this value to my php code, but I do not know how to do it. post method does not work. (I do not know why).

<script>
    var val = localStorage.getItem('sumalist');
    $.ajax({
        type: "POST",
        url: "index.php",
        data: {value: val},
        success: function () {
            console.log(val);
        }
    });
</script>

and in my php code, value is not set.

if (isset($_POST["value"])) {
    echo "Yes, value is set";
    $value = $_POST["value"];
}else{
    echo "N0, value is not set";

}

PS: My php code is in the same file in js code.

  • 写回答

4条回答 默认 最新

  • weixin_33743880 2018-07-04 18:26
    关注

    If you want to use the response in callback success function, use this:

    success: function (ret) {
        console.log(ret); //Prints 'Yes, value is set' in browser console
    }
    
    评论

报告相同问题?