【发布时间】:2019-10-09 06:28:30
【问题描述】:
基本上我想保留没有错误且不是密码或重复密码的值。为此,我关注了这个问题:
PHP Keep entered values after validation error。
不过,我尝试过的方式只保留了姓氏。我目前尝试对表单中的所有字段执行此操作的方式是:"<?php echo isset($_GET["email"]) ? $_GET["email"] : ''; ?>"。我也有错误消息,它使用 $_GET 从 URL 读取并相应地发送错误消息,这工作正常。这是我的实际代码。
<form method="post" action="includes/signup.inc.php" id="create_customer" accept-charset="UTF-8"><input type="hidden" name="form_type" value="create_customer" /><input type="hidden" name="utf8" value="✓" />
<div id="first_name" class="clearfix large_form"> <label for="fname" class="login">Nome</label>
<input type="text" value="<?php if(isset($_GET["fname"])){echo($_GET["fname"]);}?>" name="fname" id="fname" class="large" size="30" />
</div>
<div id="last_name" class="clearfix large_form"> <label for="lname" class="login">Sobrenome</label>
<input type="text" value="<?php if(isset($_GET["fname"])){echo($_GET["fname"]);}?>" name="lname" id="lname" class="large" size="30" /></div>
<div id="email" class="clearfix large_form"> <label for="email" class="login">E-mail</label> <input type="email" value="<?php echo isset($_POST["email"]) ? $POST["email"] : ''; ?>" name="email" id="email" class="large" size="30" /></div>
<div id="password" class="clearfix large_form"> <label for="password" class="login">Senha</label> <input type="password" value="" name="pwd" id="password" class="large password" size="30" />
<div id="password" class="clearfix large_form"> <label for="password" class="login">Repetir Senha</label> <input type="password" value="" name="pwd-repeat" id="password" class="large password" size="30" />
</div>
<div class="acceptsMarketing"> <input type="checkbox" id="customer[accepts_marketing]" name="customer[accepts_marketing]"> <label for="customer[accepts_marketing]">Assine a nossa
newsletter?</label></div>
<div class="action_bottom"> <input class="btn action_button" name="signup-submit" type="submit" value="Inscrever-se" />
<p class="right" style="padding-top: 8px;">
<input class="btn action_button" type="submit" value="Recuperar Senha" />
<p class="right" style="padding-top: 8px;">
Já é cliente? <a href="login.php" id="customer_login_link">Entrar →</a></p>
</div>
</form>
</div><!-- /#create-customer -->
</div>
</div>
</div>
</div>
</div>
我最初不知道,这里是错误显示的验证代码:
if (isset($_POST['signup-submit'])) {
require 'dbh.inc.php';
$email = $_POST['email'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$pwd = $_POST['pwd'];
$pwdrepeat = $_POST['pwd-repeat'];
if (empty($fname) || empty($lname) || empty($email) || empty($pwd) || empty($pwdrepeat)) {
header("Location: ../register.php?error=emptyfields&fname=" . $fname . "&lname" . $lname . "&email" . $email);
exit();
} else if (!filter_var($fname, FILTER_VALIDATE_REGEXP) && !preg_match("/^[a-zA-Z -]+$/", $fname)) {
header("Location: ../register.php?error=nomeinvalido&fname=" . $fname);
exit();
} else if (!filter_var($lname, FILTER_VALIDATE_REGEXP) && !preg_match("/^[a-zA-Z -]+$/", $lname)) {
header("Location: ../register.php?error=sobrenomeinvalido&lname=" . $lname. "&lname");
exit();
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL) && !preg_match('/^[a-zA-Z0-9]*$/', $email)) {
header("Location: ../register.php?error=invalidmail&email=" . $email);
exit();
} else if ($pwd !== $pwdrepeat) {
header("Location: ../register.php?error=passwordcheck&fname=");
exit();
【问题讨论】:
-
昨天在你之前的问题中出现了同样的表单,然后我提到你的 HTML 不正确 - 它保持不变......为什么?
-
更新代码格式
-
@RamRaider 我确实澄清了 div 是正确的。还更新了一些我尝试做的测试 "" 它保存了名字字段,但是如果我为 $_GET["lname"] 它不保存,如果我对所有字段重复 $_GET["fname",它会保存...
-
上述表格格式不正确。存在不平衡的元素数量和重复 ID
-
是的。尽管如此,格式化它并不能解决我的问题......