问题
Trying to display errors on the same page. How do I do that? As this stands right now, every time there are errors, it generates a blank page with all the errors listed.
I don't want that.
Here's a basic form that I ripped off from a tutorial somewhere test.php
:
<?php session_start(); ?>
<html>
<head>
<title></title>
</head>
<body>
<form name="form1" method="post" action="test2.php">
Name: <br/>
<input type="text" name="name" value="<?php echo $_POST['name']; ?>" size="50" /><br/><br/>
Email Address: <br/>
<input type="text" name="email" value="<?php echo $_POST['email']; ?>" size="50"/> <br/><br/>
Home Page: <br/>
<input type="text" name="homepage" value="<?php echo $_POST['homepage']; ?>" size="50" /> <br/><br/>
Message: <br/>
<textarea name="message" rows="5" cols="50"><?php echo $_POST['message']; ?></textarea>
<br/>
<input type="submit" name="Submit" />
</form>
</body>
</html>
Here's the php code for it that was also ripped off from the tutorial, ie, test2.php
:
<?php
session_start();
if (isset($_POST['Submit'])) {
if ($_POST['name'] != "") {
$_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
if ($_POST['name'] == "") {
$errors .= 'Please enter a valid name.<br/><br/>';
}
} else {
$errors .= 'Please enter your name.<br/>';
}
if ($_POST['email'] != "") {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
}
} else {
$errors .= 'Please enter your email address.<br/>';
}
if ($_POST['homepage'] != "") {
$homepage = filter_var($_POST['homepage'], FILTER_SANITIZE_URL);
if (!filter_var($homepage, FILTER_VALIDATE_URL)) {
$errors .= "$homepage is <strong>NOT</strong> a valid URL.<br/><br/>";
}
} else {
$errors .= 'Please enter your home page.<br/>';
}
if ($_POST['message'] != "") {
$_POST['message'] = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
if ($_POST['message'] == "") {
$errors .= 'Please enter a message to send.<br/>';
}
} else {
$errors .= 'Please enter a message to send.<br/>';
}
if (!$errors) {
$mail_to = 'me@somewhere.com';
$subject = 'New Mail from Form Submission';
$message = 'From: ' . $_POST['name'] . "\n";
$message .= 'Email: ' . $_POST['email'] . "\n";
$message .= 'Homepage: ' . $_POST['homepage'] . "\n";
$message .= "Message:\n" . $_POST['message'] . "\n\n";
mail($to, $subject, $message);
echo "Thank you for your email!<br/><br/>";
} else {
echo '<div style="color: red">' . $errors . '<br/></div>';
}
}
?>
Note, I added the session_start
to both pages because I'd like the errors done via that instead of GET
.
How do I have the errors show up on test.php
form?
回答1:
I would use sessions to represent errors on the same page.
start the session on test2.php
session_start();
then create session errors in the if statements you need
$_SESSION['error'] = 'error message here';
header('location:test1.php');
exit;
then on test1.php, something like this
<?php
session_start();
if (!empty($_SESSION['error'])){
?>
<div class="alert"> //you could then style this div to present error message better
<?php
echo $_SESSION['error'];
unset($_SESSION['error']);
}
?>
</div>
or have it without the divs in one php statement to just simply echo the error.
Hope I helped.
来源:https://stackoverflow.com/questions/36878170/how-to-display-errors-on-the-same-page-php-form