从js脚本将数据发送到当前的php文件

I have a php file, content.php, that is currently being loaded in the browser:

<html>
    <head>
        <title>Content</title>

        <script src="content.js"></script>
    </head>

    <body>
        <?php
            if ( isset($_POST["status"]) && ($_POST["status"] === 1) ) {
        ?>

            Content for users with status 1

        <?php
            } else {
        ?>

            Content for users with different status

        <?php
            }
        ?>
    </body>
</html>

What I want to do, is set the

$_POST["status"]

variable from within

content.js

I have thought about using a hidden html form and clicking the submit button through javascript, but that doesn't really seem like an elegant solution.

I have also thought about using an XMLHttpRequest, the problem being that I haven't found a way to send the data to the currently viewed/loading page through an XMLHttpRequest.

I am using no extra libraries, only javascript and php.

Is there a more elegant solution to my problem the a hidden html form?

查看全部
dos49618
dos49618
2017/12/19 04:51
  • php
  • html
  • ajax
  • javascript
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复