繁体   English   中英

如何使用ajax(一个文件)从php获取响应数据?

[英]How do I get response data from php with ajax (one file)?

我正在尝试使用 ajax 获取 php 响应数据。 我想从我的输入中检查testing.txt是否有特定的字符串,如果找到该字符串,php 应该echo "1"但无论我尝试什么 AJAX 总是说the output isn't 1

这是我的代码:

<?php
if (isset($_POST['table'])) {
    $file = file("testing.txt");
    if (in_array($_POST['table'], $file)) { 
        echo "1"; 
    } else { 
        echo "0"; 
    } 
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>test</title>
</head>
<body>
<input type="text" name="text" id="text">
<button id="button">NEXT</button>
<script type="text/javascript" src="jquery.js"></script>
<script>
    var text;
    document.getElementById('button').onclick = function () {
        text = document.getElementById('text').value;
        post(text);
    };
    function post(vally) {
        var table = vally;
        $.post('test.php', {table:table}, function(data) {

        })
        .done(function (data) {
            if (data == 1) {
                console.log("the output is 1")
            } else {
                console.log("the output isn't 1")
            }
        });
        console.log('posted');
    }
</script>
</body>
</html>

测试.txt:

abc
def
ghi

如果我console.log(data)得到的响应:

0<!DOCTYPE html>
<html>
<head>
    <title>test</title>
</head>
<body>
<input type="text" name="text" id="text">
<button id="button">NEXT</button>
<script type="text/javascript" src="jquery.js"></script>
<script>
    var text;
    document.getElementById('button').onclick = function () {
        text = document.getElementById('text').value;
        post(text);
    };
    function post(vally) {
        var table = vally;
        $.post('test.php', {table:table}, function(data) {

        })
        .done(function (data) {
            if (data == 1) {
                console.log("the output is 1")
            } else {
                console.log(data)
            }
        });
        console.log('posted');
    }
</script>
</body>
</html>

我曾尝试使用 .done()、.fail() 和 .always() 但我总是得到the output isn't 1 (我使用的是 JQuery 3.2.1)。

有人能告诉我我做错了什么吗?

编辑:我想指出一些我以前没有的东西。 我正在寻找一页解决方案。 我知道用两页可以轻松完成,但我想知道是否有一页解决方案。

问题是 Ajax 请求被发送到主页,因此它接收“0”或“1”之后的所有内容。 拆分那个。

  1. 将您的 PHP 代码移动到另一个文件中,例如“ajax.php”
  2. 并更改您的$.post()设置以调用ajax.php而不是test.php

因此 Ajax 请求将只接收 '0' 或 '1' 字符串。

请注意您的 AJAX 响应是如何在整个页面前加上您要查找的单个数字的。 您不需要将整个页面发送到浏览器两次。 将您的 PHP 逻辑移动到它自己的文件中,只包含该逻辑。 为了演示起见,我们将其checkTable.php

<?php
if (isset($_POST['table'])) {
    $file = file("testing.txt");
    if (in_array($_POST['table'], $file)) { 
        echo "1"; 
    } else { 
        echo "0"; 
    } 
}
?>

然后对那个页面进行 AJAX 调用:

$.post('checkTable.php', {table:table})

然后响应将包含 PHP 代码返回的内容,而不是整个页面。 (值得注意的是,如果table不在 POST 数据中,此 PHP 代码将返回一个空响应。)

除此之外,您的代码当前为您提供的任何输入返回0 ,因此“输出不是 1”仍然是正确的。 为此,您需要仔细检查您的输入和数据以确认您的假设。

因为我想将所有内容都放在一个文件中,所以我决定使用data.slice(0, 1); 修剪除第一个字符之外的所有内容01 ,并感谢大卫提醒我可能存在空格问题。 现在我添加了text.trim()以从输入和array_filter(array_map('trim', $file));删除所有空格array_filter(array_map('trim', $file)); 从文件中写入的字符串中删除所有空格。

这是完成的代码:

<?php
if (isset($_POST['table'])) {
    $file = file("testing.txt");
    $file = array_filter(array_map('trim', $file));
    if (in_array($_POST['table'], $file) == true) { 
        echo "1"; 
    } else { 
        echo "0"; 
    } 
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>test</title>
</head>
<body>
<input type="text" name="text" id="text">
<button id="button">NEXT</button>
<script type="text/javascript" src="jquery.js"></script>
<script>
    var text;
    document.getElementById('button').onclick = function () {
        text = document.getElementById('text').value;
        post(text.trim());
    };
    function post(vally) {
        var table = vally;
        console.log(vally);
        $.post('test.php', {table:table}, function(data) {
            var cut = data.slice(0, 1);
            if (cut == 1) {
                console.log("the output is 1")
            } else {
                console.log(cut);
            }
        });
        console.log('posted');
    }
</script>
</body>
</html>

我要感谢所有帮助我解决我的问题的人,这些问题在过去 2 天一直困扰着我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM