首页 > 解决方案 > 如何将 JS 值传递给 PHP 参数

问题描述

在此处添加的代码中,我将值从 PHP 参数传递给 JS 参数:两个参数都称为“stv”。

我想知道我该如何做相反的事情?

谢谢!

<script>
var stv="<?php echo $stv; ?>";
</script>

标签: javascriptphp

解决方案


如果文档中已经使用 jquery,则使用 jquery 发送带有 ajax 的请求。如果您没有添加 Jquery,您可以使用脚本标签将其添加到 html 正文的末尾并从 google 获取下载地址:

<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</body>


  function ajax(value) {
        var x = {
            'info': value //data to send can be json or something else
        };
        $.ajax({
            url: 'index.php', //page where to send
            type: 'POST', //type of request
            data: x, // data to send
            success: function(data) { //what happends when request is success 
                try {
                    getAjPromise(JSON.parse(data));
                } catch (e) {
                    console.log("error");
                }
            }
        });

    }

在 PHP 中检查是否有 $_POST['info'],获取值并执行某些操作


推荐阅读