我正在以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時忘了加上頭信息。
這就是你通過'$ _GET'訪問查詢字符串,不是'$ _POST' – 2015-02-23 19:38:29
數據如何傳輸? – PeeHaa 2015-02-23 19:41:42
@JohnConde - 你怎麼知道的?一個應用程序/ x-www-form-urlencoded post請求體被格式化爲查詢字符串。 – mata 2015-02-23 19:42:15