我想在單擊提交按鈕後記住特定輸入的值?目的是,如果在用戶註冊一個帳戶,並遇到像電子郵件存在的錯誤或密碼不匹配我想保留的值,如名稱,年齡等的時刻這裏是我的代碼,我該怎麼辦?任何幫助或建議?謝謝我想記住提交按鈕被點擊後的特定輸入的值?
<?php
session_start();
$errflag = false;
$errmsg = array();
if(isset($_SESSION['errmsg'])&&is_array($_SESSION['errmsg'])&&count($_SESSION['errmsg'])>0){
foreach($_SESSION['errmsg'] as $msg){
echo $msg;
}
unset($_SESSION['errmsg']);
}
?>
<form method="post">
<table>
<tr>
<td>Last Name:</td><td><input id="txtfield" type="text" name="lname" autofocus="autofocus" required="required"></td>
</tr>
<tr>
<td>First Name:</td><td><input id="txtfield" type="text" name="fname" required="required"></td>
</tr>
<tr>
<td>E-mail:</td><td><input id="txtfield" type="email" name="email" required="required"></td>
</tr>
<tr>
<td>Password:</td><td><input id="txtfield" type="password" name="pass" pattern=".{6,}" required="required"></td>
</tr>
<tr>
<td>Re-type:</td><td><input id="txtfield" type="password" name="rpass" pattern=".{6,}" required="required"></td>
</tr>
<tr>
<td></td><td><input id="btn" type="submit" name="register" value="Register"></td>
</tr>
</table>
</form>
<?php
include 'functions/functions.php';
if(isset($_POST['register'])){
$result=ValidateEmail($_POST['email']);
if($result){
$errmsg[] = '<p id="error"><img src="img/error.png" alt="error">This email address is already in use.</p>';
$errflag = true;
}
if($_POST['pass']!=$_POST['rpass']){
$errmsg[] = '<p id="error"><img src="img/error.png" alt="error">Passwords does not match.</p>';
$errflag = true;
}
if($errflag){
session_regenerate_id();
$_SESSION['errmsg'] = $errmsg;
session_write_close();
$errflag = false;
header('location: register.php');
exit();
}
else {
$user = array('lname'=>$_POST['lname'],'fname'=>$_POST['fname'],'email'=>$_POST['email'],'pass'=>$_POST['pass']);
RegisterUser($user);
session_regenerate_id();
$errmsg[] = '<p id="success"><img src="img/success.png" alt="success">Your account is now active. You may now login.</p>';
$_SESSION['errmsg'] = $errmsg;
session_write_close();
header('location: index.php');
exit();
}
}
?>
是,如下回答說,其最好送一個AJAX請求將返回某種驗證。不需要「記住」值,因爲您不會更改頁面,而是通過AJAX向另一個頁面發送請求,如果響應爲真(或任何您想驗證的值),則可以繼續。 –
任何樣本或鏈接,因爲我不熟悉阿賈克斯:( –
@SuiGo已在下面的答案中添加了一個例子,基本指南阿賈克斯。 – amitchhajer