//index.php
$username=$password=$confirm_pass=$admin_type=$email="";
$pass_exists=$con_pass_exists=$errview="";
$errarr=array();
......//Verification and error push//........
//assigning variable and error push
if(isset($_POST["username"]) && !empty($_POST["username"])){
$username=$_POST["username"];
}else{
array_push($errarr,"Username Field is Empty!!!");
}
if(count(errarr)>=0){
$errview="<div class='errors'>";
$errview.="<ul>";
foreach ($errarr as $error){
$errview.="<li>".$error."</li>";
}
$errview.="</ul>";
$errview.="</div>";
echo $errview;
add_form();
}
//html
<form action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']) ?>"method="post" id="add_form">
<span class="title">Username</span><input value="<?php if(isset($username))echo $username; ?>" tabindex="1" type="text" class="username" name="username">
......other elemenets.........
</form>
如果在提交后发现错误,我想在某些输入字段中保留值。我甚至尝试使用$ _GLOBAL []来创建变量全局范围。我可以实现吗?
答案 0 :(得分:0)
由于你使用method = post,你可以这样做:
if (isset($_POST['username']) {
// Form is submitted and we have a value.
$username = $_POST['username'];
}
else {
// Not a form submission, so we don't have a value.
$username = '';
}
然后您可以显示错误消息,并将值设置回表单:
<input value="<?php echo $username; ?>">
答案 1 :(得分:0)
使用直接$ _POST而不是变量
来解决它<input value="<?php if(isset($_POST["username"]))echo $_POST["username"]; ?>" tabindex="1" type="text" class="username" name="username">
但我想知道为什么变量赋值不起作用?