PHP:如果提交失败,用输入值重新显示表单

PHP:如果提交失败,用输入值重新显示表单,php,Php,我的用户名是主键。当用户提交表单并发现存在重复的用户名时,sql将失败。如果提交失败,我想用用户输入的值重新显示表单。我该怎么做 我尝试将user if(isset)方法和windows.location.href返回到同一页面,它会清除插入的值 我还尝试在没有window.location.href的情况下回显警报脚本,它会清除所有内容 如果提交失败,如何在文本框中重新显示带有插入值的表单 这是我的密码 include('sql_connect.php'); if (isset($_POS

我的用户名是主键。当用户提交表单并发现存在重复的用户名时,sql将失败。如果提交失败,我想用用户输入的值重新显示表单。我该怎么做

我尝试将user if(isset)方法和windows.location.href返回到同一页面,它会清除插入的值

我还尝试在没有window.location.href的情况下回显警报脚本,它会清除所有内容

如果提交失败,如何在文本框中重新显示带有插入值的表单

这是我的密码


include('sql_connect.php');

if (isset($_POST['submit'])) {
  // code...

  $username = $_POST['username'];
  $password = $_POST['password'];

  $sql = "INSERT INTO `test`(`Username`, `Password`) VALUES ('$username' , '$password') ";
  if (!mysqli_query($connection , $sql)){

      echo"<script>alert('Failed');</script>";

  }
  else {
    echo"<script>alert('$type INSERTED');window.location.href='test3.php';</script>";
  }
}
else {
  // code...


 ?>

<form class="" action="test.php" method="post">

Username : <input type="text" name="username" value=""> <br>
Password : <input type="text" name="password" value="">

<button type="submit" name="submit">Submit</button>
</form>

<?php

} ?>

包括('sql_connect.php');
如果(isset($_POST['submit'])){
//代码。。。
$username=$_POST['username'];
$password=$_POST['password'];
$sql=“插入'test'('Username','Password')值('$Username','$Password')”;
if(!mysqli_query($connection,$sql)){
回显“警报(‘失败’);”;
}
否则{
echo“警报('$type INSERTED');window.location.href='test3.php';”;
}
}
否则{
//代码。。。
?>
用户名:
密码: 提交
您可以使用会话来设置会话和html字段中的其他字段

    if (isset($_POST['submit'])) {
session_start();
$_SESSION['email']=$_POST['email'];
 }
<input type="email" name="email" value="<?php if(isset($_SESSION['email'])){echo 
$_SESSION['email'];}?>">
if(isset($\u POST['submit'])){
会话_start();
$_SESSION['email']=$_POST['email'];
}

您可以在前端使用验证,以避免冗长的过程。不要重定向到其他地方,只需再次输出表单-这次用您刚收到的值预先填充文本输入字段的
属性。这是否回答了您的问题?@04FS是的!谢谢。我真的不知道要用谷歌搜索什么这就是我问的原因。谢谢!“你可以用session来做这件事”-或者你可以用
$\u POST['email']
来做这件事,在这种情况下你现在已经有了…