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");
    }

  }
?>

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.

If you made default.html a PHP file, you could pass a variable via the URL, this would then allow you check if this variable has been passed using $_GET[]and show the user a message.

So for example, if you forwarded the user to

default.php?error=1

On the default page, you could have a segment of code such as

if (isset($_GET['error'])) {
echo "Show user a message here";
}

Personally, I don't like to expose states like "error" and "invalid" to the user using a query string. In this situation, I would merge the two files in one single PHP file, with the PHP code at the top and the HTML code at the bottom.

The if statement in the PHP code would be:

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

And down in the HTML where you want to display the message:

<?php

if( isset( $error ) && $error )
    echo 'You have entered the wrong data!';

?>

Ofcourse, in the form element, you would have to remove action="default2.php".

If you prefer separating the logic and the markup, you could change default.html to for example template.php and include it at the end of your controller php file.

I just don't like the idea of an extra page without any content that only acts as a redirector.