簡體   English   中英

Ajax提交不起作用(無法在php中讀取$ _POST)

[英]Ajax submit doesn't work (can't read $_POST in php)

我正在嘗試使用Ajax提交表單。 單擊按鈕時,將調用hit()函數, 並將 文本框的內容傳遞test.php

$_POST似乎是空的,因為我收到了來自ajax的警報(已提交表單),但是卻看不到echo( echo $_POST['textbox']

test.php

<?php
    echo "test";
    if($_POST)
    {
        echo $_POST['textbox'];
    }
?>
<html>
    <body>
            <form action="index.php" method="post" align="center" id="form" name="form">

                <script type="text/javascript" src="test2.js"> </script>
                <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" type="text/javascript"></script>


                <input type="text" class="form-control" id="input" name="input" oninput="check();">
                <input type="button" class="form-control" id="send" name="send" value="Send" onclick="hit();">
            </form>
    </body>
    </div>
</html>

test2.js

function hit() {
        var inputText = $("#input").val();
        var inputTextString = "textbox=" + inputText;
        $.ajax({
            type: 'post',
            url: 'test.php',
            data: inputTextString,
            success: function () {
              alert('form was submitted');
            }
        });

}

您將在響應中獲得理想的結果。

function hit() {

        var inputText = $("#input").val();


        $.ajax({
            type: 'post',
            url: 'test.php',
            data: {textbox : inputText},
            success: function (res) {
              alert(res);
            }
        });

}

並且您需要更改test.php文件。

<?php
    echo "test";
    if($_POST)
    {
        echo $_POST['textbox'];exit;
    }
?>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="test2.js"> </script>
</head>

    <body>
            <form action="index.php" method="post" align="center" id="form" name="form">
                <input type="text" class="form-control" id="input" name="input" oninput="check();">
                <input type="button" class="form-control" id="send" name="send" value="Send" onclick="hit();">
            </form>
    </body>
    </div>
</html>

這里還有更多問題

test.php中

  • 您如何在包含jquery之前包含js文件..首先是第一個.. 經過測試 是,您可以在函數內使用jquery $()而不會出現$ undefined錯誤,而在包含jquery之后運行該函數..對不起壞
  • 腳本應位於<head></head></body>之前
  • 在使用“僅”按鈕時,單擊為什么使用<form>

您的代碼應該是這樣的

<?php
    echo "test";
    if($_POST)
    {
        echo $_POST['textbox'];
        return false;
    }
?>
<html>
    <head>
    </head>
    <body>
         <input type="text" class="form-control" id="input" name="input" oninput="check();">
          <input type="button" class="form-control" id="send" name="send" value="Send" onclick="hit();">

      <script type="text/javascript" src="test2.js"> </script>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" type="text/javascript"></script>
    </body>
    </div>
</html>

test2.js上

function hit() {
    var inputText = $("#input").val();
    var inputTextString = {textbox : inputText}; // use this
    $.ajax({
        type: 'post',
        url: 'test.php',
        data: inputTextString,
        success: function () {
           alert('form was submitted');
        }
    });
}

注意:對我來說,我更喜歡使用分離的php文件與ajax一起使用..這樣可以簡化輸出

如果您需要使用表單..您可以使用包括上面我的注釋的表單代碼,並使提交按鈕的類型為“ submit”,並從中刪除onclick="hit()" ,然后在您的js文件中使用

$(document).ready(function(){
     $('form').on('submit',function(e){
        e.preventDefault(); // to prevent form reload
        var inputText = $("#input").val();
        var inputTextString = {textbox : inputText}; // use this
        $.ajax({
           type: 'post',
           url: 'test.php',
           data: inputTextString,
           success: function () {
              alert('form was submitted');
           }
        });
     });
});

您永遠不會測試是否看到echo的響應-因此根本不會警告php的響應。

要查看您的php腳本返回的內容,您必須對成功回調中傳入的參數進行警報(或記錄或進行一些有用的操作):

....
success: function (response) { 
     alert(response);
     console.log(response); 
},
....

無論如何,您應該確保不要將其他數據(例如您的情況下不需要的html)發送回ajax,而僅發送回value / json。 因此,在您的情況下exit; echo后會有所幫助。

還請按照@ Mohammed-Yousef的說明處理其他問題!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM