duanhan9479 2014-04-19 19:33
浏览 127
已采纳

在表单的同一页面上显示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?

  • 写回答

5条回答 默认 最新

  • drdu53813 2014-04-19 19:40
    关注

    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.
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探