我有一个注册表单,它是没有刷新页面的帖子数据!我的问题表格重置数据。它适用于用户名并保持用户的输入。我也尝试为“性别”做同样的事情,但是如果我感到恐惧,“Genderselection也会被重置。我无法管理它。
有人可以帮我吗?
我的代码如下:
/* Current Page */
public function currentPage() {
/* Current Page */
$currentPage = 'http';
if(isset($_SERVER["HTTPS"]) == "on") {$currentPage .= "s";}
$currentPage .= "://";
$currentPage .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
return $currentPage;
}
/* Register */
public function register() {
global $database;
/* Set Message Array */
$message = array();
/* Check if Login is set */
if(isset($_POST['register'])) {
/* Check Username */
if(!empty($_POST['username'])) {
$check_username = strtolower($_POST['username']);
/* Check the username length */
$length = strlen($check_username);
if($length >= 5 && $length <= 25) {
/* Is the username Alphanumeric? */
if(preg_match('/[^a-zA-Z0-9_]/', $check_username)) {
$error[] = "Please enter a valid alphanumeric username";
$username = null;
} else {
$database->query('SELECT id FROM users WHERE username = :username', array(':username' => $check_username));
/* Check if user exist in database */
if($database->count() == 0) {
/* Require use to validate account */
if($this->email_verification == true) {
/* Check if user exist in inactive database */
$database->query('SELECT date FROM users_inactive WHERE username = :username', array(':username' => $check_username));
/* If user incative is older than 24 hours */
$user = $database->statement->fetch(PDO::FETCH_OBJ);
if($database->count() == 0 or time() >= strtotime($user->date) + 86400) {
/* If user incative is older than 24 hours */
$username = $_POST['username'];
} else {
$error[] = "Username already in use";
$username = $check_username;
}
} else {
$username = $_POST['username'];
}
} else {
$error[] = "Username already in use";
$username = $check_username;
}
}
} else {
$error[] = "Please enter a username between 5 to 25 characters";
$username = $check_username;
}
} else {
$error[] = "Please enter a username";
$username = null;
}
/* Check Password */
if(!empty($_POST['password'])) {
/* Do passwords match? */
if(isset($_POST['password_again']) && $_POST['password_again'] == $_POST['password']) {
/* Is the password long enough? */
$length = strlen($_POST['password']);
if($length >= 8) {
$password = $_POST['password'];
} else {
$error[] = "Passwords must be atleast than 8 characters";
}
} else {
$error[] = "Passwords must match";
}
} else {
$error[] = "Please enter a password";
}
/* Check Gender */
if(empty($_POST['gender'])) {
$error[] = "Please choose your gender";
}else{
$gender = $_POST['gender'];
}
$form = '
<form name="register" action="' . $this->currentPage() . '" method="post">
<label><span>Username</span>
<input type="text" name="username" value="' . $username . '" />
</label>
<label><span>Password</span>
<input type="password" name="password" />
</label>
<label><span>Password Again</span>
<input type="password" name="password_again" />
</label>
<label><span>Gender:</span>
<select name="gender" value="' . $gender . '">
<option value="">Choose</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
<input name="register" type="submit" value="Register" />
</form>
';
/* Combine Data */
$data = "";
/* Report any Info */
if(isset($info)) {
foreach($info as $message) {
$data .= '<div class="notice info">' . $message . '</div>';
}
}
/* Report any Errors */
if(isset($error)) {
foreach($error as $message) {
$data .= '<div class="notice error">' . $message . '</div>';
}
}
/* Report any Success */
if(isset($success)) {
foreach($success as $message) {
$data .= '<div class="notice success">' . $message . '</div>';
}
}
/* Return data */
return $data;
}
答案 0 :(得分:1)
关于“性别”价值问题 - 您需要更改此代码
<select name="gender" value="' . $gender . '">
<option value="">Choose</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
要
<select name="gender">
<option value="" ' .($gender == '' ? 'selected="selected"' : '') . '>Choose</option>
<option value="Male" ' .($gender == 'male' ? 'selected="selected"' : '') . '>Male</option>
<option value="Female" ' .($gender == 'female' ? 'selected="selected"' : '') . '>Female</option>
您正尝试将value
设置为select
无效的标记。