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

时间:2022-06-11 21:36:26

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

我有一个php文件,content.php,当前正在浏览器中加载:

<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.

我曾考虑使用隐藏的html表单并通过javascript单击提交按钮,但这似乎并不是一个优雅的解决方案。

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.

我还考虑过使用XMLHttpRequest,问题是我没有找到通过XMLHttpRequest将数据发送到当前查看/加载页面的方法。

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

我没有使用额外的库,只有javascript和php。

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

是否有一个更优雅的解决方案,我的问题隐藏的HTML表单?

2 个解决方案

#1


1  

Seems you need to work with AJAX because you need to execute server side script in hidden manner and update the html.

似乎你需要使用AJAX,因为你需要以隐藏的方式执行服务器端脚本并更新html。

  1. interface.php has content.js and a div
  2. interface.php有content.js和div
  3. content.js send ajax post request with status
  4. content.js发送带状态的ajax post请求
  5. content.php sends the content according to status
  6. content.php根据状态发送内容
  7. interface.php's div is updated.
  8. interface.php的div已更新。

Here are the content.js and interface.php

这是content.js和interface.php

let status = 1;
loadDoc(status);

function loadDoc(status) {
  let xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = () => {
    if (xhttp.readyState == 4 && this.status == 200) {
      document.getElementById("content").innerHTML =
      xhttp.responseText;
    }
  };
  xhttp.open("POST", "content.php", true);
  xhttp.send("status=" + status);
}
<html>
    <head>
        <title>Content</title>
        <script src="content.js"></script>
    </head>

    <body>
       <div id="content"></div>
    </body>
</html>

And here is your content.php

这是你的content.php

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

    Content for users with status 1

<?php
    } else {
?>

    Content for users with different status

<?php
    }
?>

Bingo! You set the $_POST["status"] via content.js

答对了!您通过content.js设置$ _POST [“status”]

#2


0  

POST variables can only be passed in a request header. This means that it has to be either a form submission, as you already know, or an AJAX request. There is no way to have a separate client-side resource supply new information to an existing request.

POST变量只能在请求标头中传递。这意味着它必须是您已经知道的表单提交或AJAX请求。没有办法让单独的客户端资源为现有请求提供新信息。

#1


1  

Seems you need to work with AJAX because you need to execute server side script in hidden manner and update the html.

似乎你需要使用AJAX,因为你需要以隐藏的方式执行服务器端脚本并更新html。

  1. interface.php has content.js and a div
  2. interface.php有content.js和div
  3. content.js send ajax post request with status
  4. content.js发送带状态的ajax post请求
  5. content.php sends the content according to status
  6. content.php根据状态发送内容
  7. interface.php's div is updated.
  8. interface.php的div已更新。

Here are the content.js and interface.php

这是content.js和interface.php

let status = 1;
loadDoc(status);

function loadDoc(status) {
  let xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = () => {
    if (xhttp.readyState == 4 && this.status == 200) {
      document.getElementById("content").innerHTML =
      xhttp.responseText;
    }
  };
  xhttp.open("POST", "content.php", true);
  xhttp.send("status=" + status);
}
<html>
    <head>
        <title>Content</title>
        <script src="content.js"></script>
    </head>

    <body>
       <div id="content"></div>
    </body>
</html>

And here is your content.php

这是你的content.php

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

    Content for users with status 1

<?php
    } else {
?>

    Content for users with different status

<?php
    }
?>

Bingo! You set the $_POST["status"] via content.js

答对了!您通过content.js设置$ _POST [“status”]

#2


0  

POST variables can only be passed in a request header. This means that it has to be either a form submission, as you already know, or an AJAX request. There is no way to have a separate client-side resource supply new information to an existing request.

POST变量只能在请求标头中传递。这意味着它必须是您已经知道的表单提交或AJAX请求。没有办法让单独的客户端资源为现有请求提供新信息。