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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘