簡體   English   中英

ajax請求不發布表單數據

[英]ajax request not posting form data

我有一個問題,即我的登錄表單中輸入的值(電子郵件和密碼)未被解析為我的服務器php腳本。

我的ajax請求:

function signin(){
var loginEmail = gebi("loginEmail").value;
var loginPass = gebi("loginPass").value;

if(loginEmail == "" || loginPass == ""){
    gebi("loginEmail").style.borderColor = "red";
    gebi("loginPass").style.borderColor = "red";
} else {
    gebi("signinBtn").style.display = "none";

            //Declare ajax request variables
    hr = new XMLHttpRequest();
    url = "main.php";
    vars = "email="+loginEmail+"&pass="+loginPass;

    //Open the PHP file that is receiving the request
    hr.open("POST", url, true);

    //Set content type header info for sending url ecncoded variable in the request
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    //Trim string data before sending it
    if (!String.prototype.trim) {
        String.prototype.trim = function () {
            return this.replace(/^\s+|\s+$/g, '');
        };
    }

    //Access the onreadystatechange event for the XMLHttpRequest
    hr.onreadystatechange = function() {
        if (hr.readyState == 4 && hr.status == 200) {
            var responseText = hr.responseText.trim();
            if (responseText != "signin_failed") { 
                console.log(responseText);
            } else {
                console.log(responseText);
                gebi("signinBtn").style.display = "block";
                gebi("loginEmail").style.borderColor = "red";
                gebi("loginPass").style.borderColor = "red";
            }
        }
    }
    //Send the data to the PHP file for processing and wait for responseText
    hr.send(vars);
}
}

當php腳本使用以下代碼返回值時:

if (isset($_POST["email"])) {
    echo 'email = '+$_POST["email"];
}

即使表單字段中存在數據,記錄到控制台的返回值也為“0”。

出了什么問題?

問題出在您的PHP腳本中。 如果你試圖在PHP中連接字符串使用點. +用於在javascript等語言中連接字符串。

echo 'email = ' . $_POST["email"];

暫無
暫無

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

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