在表单的同一页面上显示PHP错误?
So I have two files my first one is the form itself
Here's the form index.php
<!DOCTYPE html>
<html>
<body>
<form method="post" action="http://localhost/s/r.php" >
Name: <input type="text" name="name"><br>
Username: <input type="text" name="username"><br>
Email: <input type="text" name="email"><br>
Password: <input type="text" name="pass1"><br>
Password, again: <input type="text" name="pass2"><br>
<input type="submit">
</form>
</body>
</html>
And then my r.php
<?php
include 'db.php';
$name = mysqli_real_escape_string($con, $_POST['name']);
$username = mysqli_real_escape_string($con, $_POST['username']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$pass1 = mysqli_real_escape_string($con, $_POST['pass1']);
$pass2 = mysqli_real_escape_string($con, $_POST['pass2']);
// Verification
if (empty($name) || empty($username) || empty($email) || empty($pass1) || empty($pass2))
{
echo "Complete all fields";
// you can stop it here instead of putting the curly brace ALL the way at the bottom :)
return;
}
// Password match
if ($pass1 <> $pass2)
{
echo $passmatch = "Passwords don't match";
}
// Email validation
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
echo $emailvalid = "Enter a valid email";
}
// Password length
if (strlen($pass1) <= 6)
{
echo $passlength = "Password must be at least 6 characters long";
}
// Password numbers
if (!preg_match("#[0-9]+#", $pass1))
{
echo $passnum = "Password must include at least one number!";
}
// Password letters
if (!preg_match("#[a-zA-Z]+#", $pass1))
{
echo $passletter = "Password must include at least one letter!";
}
?>
And I also have my db.php
which isn't relevant to the issue. So I'm trying to make the form not go to r.php
and display the errors if there is an error, but rather make it display next to the form, in index.php
. Is there a way to prevent it from going to r.php
or would I have to combine the two scripts?
simply put your r.php
code in index.php
and then change the form action, just put it as action=""
instead of action="http://localhost/s/r.php"
To prevent auto executing the php code, you could use isset
.
change your input button as follow
Name: <input type="text" name="name" value="<?php if(!empty($name)){echo $name;}?>"><br>
Username: <input type="text" name="username" value="<?php if(!empty($username){echo $username;}"><br>
Email: <input type="text" name="email" value="<?php if(!empty($email)){echo $email;}?>"><br>
Password: <input type="text" name="pass1" value="<?php if(!empty($pass1)){echo $pass1;}?>"><br>
Password, again: <input type="text" name="pass2" value="<?php if(!empty($pass2)){echo $pass2;}?>"><br>
<input type="submit" name="submit" value="submit">
then put your php code inside this.
<?php
if(isset($_POST['submit'])){
//put the php code here.
}
?>
You could you javascript validation or can make both html & php in the same page to show the error.
You'll need to pass your error message as an HTTP post or get variable to index.php
.
Everywhere you're currently echo
ing errors now, instead set an $error
variable,
then you can add something like
if ($error) {
header("index.php?error=" + $error); // passing error(s) as an HTTP get variable via the querystring
}
Then in index.php
you can process the error
query variable.
<!DOCTYPE html>
<html>
<body>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>" >
Name: <input type="text" name="name"><br>
Username: <input type="text" name="username"><br>
Email: <input type="text" name="email"><br>
Password: <input type="text" name="pass1"><br>
Password, again: <input type="text" name="pass2"><br>
<input type="submit">
</form>
</body>
</html>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST")
{
$name = mysqli_real_escape_string($con, $_POST['name']);
$username = mysqli_real_escape_string($con, $_POST['username']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$pass1 = mysqli_real_escape_string($con, $_POST['pass1']);
$pass2 = mysqli_real_escape_string($con, $_POST['pass2']);
// Verification
if (empty($name) || empty($username) || empty($email) || empty($pass1) || empty($pass2))
{
echo "Complete all fields";
// you can stop it here instead of putting the curly brace ALL the way at the bottom :)
return;
}
// Password match
if ($pass1 <> $pass2)
{
echo $passmatch = "Passwords don't match";
}
// Email validation
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
echo $emailvalid = "Enter a valid email";
}
// Password length
if (strlen($pass1) <= 6)
{
echo $passlength = "Password must be at least 6 characters long";
}
// Password numbers
if (!preg_match("#[0-9]+#", $pass1))
{
echo $passnum = "Password must include at least one number!";
}
// Password letters
if (!preg_match("#[a-zA-Z]+#", $pass1))
{
echo $passletter = "Password must include at least one letter!";
}
}
?>
you could use XHR (ajax) using javascript to send the form without leaving the page. you would use the onsubmit event of the form (returning false to prevent default behaviour) or onclick event of a button. you would update your form in the callback:
var x=(window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
x.onreadystatechange=function(){if(x.readyState==4&&x.status==200){
alert(x.responseText); //update your form
}}
x.open('GET','r.php',true);x.send();
you could send a javascript from your php as well, append a script element to the body of the document and set its innerHTML (or innerText)