javascript - 根据 PHP 输出执行 Javascript

标签 javascript php html ajax json

如果我的 PHP 代码 conditional.php 看到输入了某些内容然后提交了,我正在尝试使我的代码 test.php 执行一些 Javascript 代码。相反,我的代码所做的是输出“Notempty”而不是“Do Something in Javascript”。我注意到的一件奇怪的事情是,test.php 中的第 26-32 行(在 doSomething() 函数内部)被忽略,但它打印出 "Empty"尽管如此,文本框中仍显示 “Notempty”

我这样做的原因是因为我的实际网站中的代码需要使用 Javascript API 或仅使用 PHP,具体取决于输入来生成输出。

test.php

<!DOCTYPE html>
<html>
  <body>
    <!-- Input -->  
    <div class="form">
      <form action="test.php" method="post">
        <input type="text" id="inputText" name="inputText">
        <input type="submit">
      </form>
    </div>

    <br>

    <!-- Output -->
    <div class="txtBox">
      <textarea id="txtBox">
<?php require_once "conditional.php";?>
      </textarea>
    </div>

    <script>
        function makeRequest() {
            httpRequest = new XMLHttpRequest();

            httpRequest.onreadystatechange = doSomething() {
                // get echoed json from conditional.php
                document.getElementById("txtBox").innerHTML = httpRequest.responseText;

               /if (httpRequest.responseText == "Not Empty") {
                    // do my Javascript code
                    document.getElementById("txtBox").innerHTML = "Do something in Javascript";
                }
            };

            httpRequest.open("GET", conditional.php);
            httpRequest.send();
        }
    </script>
  </body>
</html>

条件.php

<?php
$input = $_POST["inputText"];

if ($input != "") {
    echo json_encode("Not empty");
} else {
    echo json_encode("Empty");
}
?>

最佳答案

您的代码中存在一些问题,导致您无法获得所需的行为。

首先,您尝试通过 GET 发送数据但可以通过 POST 访问它。其次,您的表单数据实际上并未通过 AJAX 调用发送。最后,你的函数 makeRequest()实际上并没有在任何地方被调用。您将在 <textarea> 中获取文本因为你的conditional.php在其内部输出。

在您的情况下,您需要删除表单的提交功能(因为您是通过 AJAX 执行的),调用 makeRequest()函数,发送数据,获取响应并编辑<textarea> .

另一件事需要考虑,为什么不使用 HTTP 响应代码而不是比较文本返回值?它非常适合这些情况,您可以使用 PHP 设置它。

这是您的两个文件,经过一些修改以实现您想要的效果。

test.php

<!DOCTYPE html>
<html>
  <body>

    <!-- Input -->  
    <div class="form">
      <form onsubmit="makeRequest(); return false;">
        <input type="text" id="inputText" name="inputText">
        <input type="submit">
      </form>
    </div>

    <br>

    <!-- Output -->
    <div class="txtBox">
      <textarea id="txtBox">
      </textarea>
    </div>

    <script>
        function makeRequest() {
            httpRequest = new XMLHttpRequest();            
            httpRequest.onreadystatechange = function() {
                if(httpRequest.readyState == 4) {
                    document.getElementById("txtBox").value = httpRequest.responseText;
                    if (httpRequest.status == 200) {
                        // do my Javascript code
                        document.getElementById("txtBox").value = "Do something in Javascript";
                    } else {
                        document.getElementById("txtBox").value = "Empty";
                    }
                }
            };
            httpRequest.open("POST", "conditional.php", true);
            httpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            httpRequest.send("inputText=" + document.getElementById("inputText").value);
        }
    </script>

  </body>
</html>

条件.php

<?php

if (isset($_POST["inputText"]) && $_POST["inputText"] != "") {
    http_response_code(200);
} else {
    http_response_code(400);
}

?>

请注意http_response_code()适用于 PHP >= 5.4 - 在此之前的任何版本,您必须使用 header()函数代替。

关于javascript - 根据 PHP 输出执行 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28575137/

相关文章:

HTML 视频标签在 IE 中只显示一半的视频

html - 将 box-shadow 放在 site-wrapper 上

Javascript根据另一个下拉框值更改下拉框选项

javascript - 表分类器和日期 mm/yyyyy

javascript - 如果复选框被选中/未选中,启用/禁用提交按钮?

php - 错误的 json 数组 Python post 请求到 PHP

php - 如何检查链接(a)提交按钮是否被点击

javascript - 为什么 Mocha 无法识别我的错误?

javascript - 如何在javascript中缩放(调整大小)图像保持纵横比

php - 在字符串中的美元符号后使用花括号是否错误