簡體   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