希望這不會像我一直認爲它應該一樣簡單。點擊提交時,一個簡單的HTML論壇會調用一個Javascript進行驗證,以便由PHP處理將已驗證的信息放入MySql表中。我遇到的問題是一旦我得到的JavaScript返回true,我該如何拉出來在PHP中使用?通過JS返回php值的HTML論壇驗證
論壇:
<form action="" method="post" name="register">
<div class="field">
<label for "username">Username</label>
<input type="text" name="username" id="username" value="" autocomplete="off">
</div>
<div class="field">
<label for "password">Choose a password</label>
<input type="password" name="password" id="password">
</div>
<div class="field">
<label for "password_again">Reenter Password</label>
<input type="password" name="password_again" id="password_again">
</div>
<div class="field">
<label for "name">Name</label>
<input type="text" name="name" id="name" value="" autocomplete="off">
</div>
<input type="submit" value="Register" id="submit" onclick="validate()" />
外部JS文件:
function validate() {
var password = document.register.password.value;
var passwordagain = document.register.password_again.value;
var name = document.register.username.value;
var test = new RegExp("[^a-zA-Z0-9]");
var space = String.fromCharCode(32);
if (name.match(test)) {
alert("Your name may only contain letters and numbers.");
return false;
} else if (name.match(space)) {
alert("Spaces are not allowed.");
return false;
} else if ((name.length <= 2) || (name.length >= 11)) {
alert("Name must be between 3 and 10 characters.");
return false;
} else if (!password.match(passwordagain)) {
alert("Passwords do not match");
return false;
} else if ((password.length <= 6) || (password.length >= 32)) {
alert("Password must be between 6 and 32 characters long.");
return false;
}
return true;
}
無論你的表單動作點會在數據被提交。如果空白,它將被提交到瀏覽器所在的頁面。您可以添加PHP代碼來處理當前頁面中的POST,也可以創建專門的PHP頁面來處理POST數據並更改表單操作。 – gcochard