doumeng3188 2012-05-16 19:41
浏览 30
已采纳

PHP - 如果输入错误,请不要转到下一页

I'm working with a .html and a .php. In the default.html the user must enter some info and when the button is clicked the html does a post to the default2.php. In default2.php the data is checked and if it's correct it redirects the user to another page. The problem I'm having is when the data entered is wrong. I'm having two issues here:

  1. When the data is wrong I'm redirecting the user to the default.html, because if I don't do that, it will stay in default2.php and default2.php has nothing important for the user to see. I don't know if this is the best way to do this.
  2. When the data entered is wrong, I want an echo message to the user in default.html. But I don't know how to trigger this from default2.php.

How can I solve these two issues? Thanks...


default.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">

</script>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP4</title>
<style type="text/css">
body {
    background-color: #CCC;
}
</style>
</head>

<body>


<p>&nbsp;</p>

<form id="form1" name="form1" method="post" action="default2.php">
  <p>
    <label for="textfield1">User Name</label>
    <input type="text" name="username" id="username" />
  </p>
  <p>
    <label for="textfield2">Password</label>
    <input type="password" name="password" id="password" />
  </p>
  <p>
    <input type="submit" name="button1" id="button1" value="Submit"  />
    <br />
    <br />
  <label id="label1">
  </label></p>
  <p>&nbsp;</p>  
</form>
<p>&nbsp;</p>


</body>
</html>

default2.php:

<?php
require 'connection.php';

  if (isset($_POST['button1'])) {

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

    // Then you can prepare a statement and execute it.    
    $stmt = $dbh->prepare("CALL login(?, ?)");
    $stmt->bindParam(1, $username_v, PDO::PARAM_STR); 
    $stmt->bindParam(2, $password_v, PDO::PARAM_STR); 

    // call the stored procedure
    $stmt->execute();

    if ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) 
    {
          header("Location: main.php");
    }
    else
    {
        header("Location: default.html");
    }

  }
?>
  • 写回答

3条回答 默认 最新

  • douniwan_0025 2012-05-16 19:54
    关注

    Just add some parameter to

    header("Location: default.html?test=failed");
    

    And in html use Javascript to display something sensible when variable test is set to failed. You can find a tutorial how to get value of url parameter with javascript here.

    Hope that helps.

    Other than that you can use PHP in your default.html and perhaps even AJAX request to do validation without leaving the page and highlighting validation errors.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?