2015-02-23 96 views
-3

我正在以POST請求的形式將數據發送到PHP頁面。在Chrome瀏覽器開發工具,我可以看到POST與有效載荷:POST負載未被PHP頁面讀取

「用戶名= T &密碼= T & cpassword = T &電子郵件= T & cemail = T」

我的PHP文件開始有以下情況:

<?php 
$username = $_POST['username']; 
$password = $_POST['password']; 
$cpassword = $_POST['cpassword']; 
$email = $_POST['email']; 
$cemail = $_POST['cemail']; 

echo("Username: $username <br>Password: $password <br>cPassword: $cpassword <br>Email: $email <br>cEmail: $cemail"); 
.... 

但是,頁面只顯示:

用戶名:
密碼:
cPassword:
電子郵件:
cEmail:

這是爲什麼,我怎樣才能讓這個$的用戶名,$密碼,$ cpassword,$ email和$ cemail設置?

編輯:方法用於產生後數據如下:

<script> 
function validateForm() { 
    window.alert("Form submitted"); 
    var username = encodeURIComponent(document.getElementById("username").value); 
    var password = encodeURIComponent(document.getElementById("password").value); 
    var cpassword = encodeURIComponent(document.getElementById("cpassword").value); 
    var email = encodeURIComponent(document.getElementById("email").value); 
    var cemail = encodeURIComponent(document.getElementById("cemail").value); 

    var postData = "username=" +username + "&password=" + password + "&cpassword=" + cpassword + "&email=" + email + "&cemail=" + cemail; 
    window.alert(postData); 

    if (email != cemail) { 
     window.alert("Emails do not match"); 
     document.getElementById("response").innerHTML = "Emails do not match"; 
     return false; 
    } 

    if (password != cpassword) { 
     window.alert("Passwords do not match"); 
     document.getElementById("response").innerHTML = "Passwords do not match"; 
     return false; 
    } 

    if (email == "" || email == null) { 
     window.alert("Email is blank"); 
     document.getElementById("response").innerHTML = "Email cannot be blank"; 
     return false; 
    } 

    if (password == "" || password == null) { 
     window.alert("Password is blank"); 
     document.getElementById("response").innerHTML = "Password cannot be blank"; 
     return false; 
    } 

    var xmlhttp; 
    if (window.XMLHttpRequest) { 
     xmlhttp=new XMLHttpRequest(); 
    } else { 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 

    xmlhttp.onreadystatechange=function() { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) { 
      var response=xmlhttp.responseText; 
      window.alert("Response recieved: " + response); 
      if (response == "New record created successfully") { 
       window.alert("Registration successful"); 
       document.getElementById("response").innerHTML = "Registration successful!"; 
      } else { 
       window.alert("Something went wrong... :("); 
       document.getElementById("response").innerHTML = "Something went wrong... :("; 
      } 
     } 
    } 

    xmlhttp.open("POST", "./handleRegistration.php", true); 
    window.alert("xmlhttp open"); 
    xmlhttp.send(postData); 
    window.alert("POSTED"); 
    return false; 
} 
</script> 

<form name="heliosRegister" onsubmit="return validateForm()" method="POST"> 
Username:<br> 
<input id="username" type="text" name="username" required> 
<br><br> 
Passsword:<br> 
<input id="password" type="password" name="password" required> 
<br><br> 
Confirm Passsword:<br> 
<input id="cpassword" type="password" name="cpassword" required> 
<br><br> 
Email:<br> 
<input id="email" type="text" name="email" required> 
<br><br> 
Confirm Email:<br> 
<input id="cemail" type="text" name="cemail" required> 
<br><br> 
<input type="submit" value="Register"> <p id="response"></p> 
</form> 

編輯2:

問題解決了,在發送POST時忘了加上頭信息。

+3

這就是你通過'$ _GET'訪問查詢字符串,不是'$ _POST' – 2015-02-23 19:38:29

+0

數據如何傳輸? – PeeHaa 2015-02-23 19:41:42

+2

@JohnConde - 你怎麼知道的?一個應用程序/ x-www-form-urlencoded post請求體被格式化爲查詢字符串。 – mata 2015-02-23 19:42:15

回答

0

更換

var username = encodeURIComponent(document.getElementById("username").value); 
var password = encodeURIComponent(document.getElementById("password").value); 
var cpassword = encodeURIComponent(document.getElementById("cpassword").value); 
var email = encodeURIComponent(document.getElementById("email").value); 
var cemail = encodeURIComponent(document.getElementById("cemail").value); 

var postData = "username=" +username + "&password=" + password + "&cpassword=" + cpassword + "&email=" + email + "&cemail=" + cemail; 

var fd=new FormData(); 
fd.append("username",document.getElementById("username").value); 
fd.append("password",document.getElementById("password").value); 
fd.append("cpassword",document.getElementById("cpassword").value); 
fd.append("email",document.getElementById("email").value); 
fd.append("cemail",document.getElementById("cemail").value); 

然後用

xmlhttp.send(fd); 

,我認爲它應該工作。 btw,$ cpassword = $ _POST ['cpassword']; 應該給你一個明顯的錯誤! (未定義索引), 開發中,使用此代碼!更容易在開發過程中發現幾種類型的錯誤:

error_reporting(E_ALL); 
function exception_error_handler($errno, $errstr, $errfile, $errline) { 
    if (!(error_reporting() & $errno)) { 
     // This error code is not included in error_reporting 
     return; 
    } 
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline); 
} 
set_error_handler("exception_error_handler"); 
1

問題是,我忘了,在發佈前添加頁眉信息:

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
xmlhttp.setRequestHeader("Content-length", postData.length); 
xmlhttp.setRequestHeader("Connection", "close"); 
+0

哦..好的FormData會自動爲您完成。此外,內容長度將被默默忽略和重新計算,XMLHttpRequest不會 - 標準要求它不允許 - 允許您指定內容長度標題。 – hanshenrik 2015-02-23 20:16:05