简体   繁体   English

表单提交后如何用php显示错误消息

[英]How to display error message with php after form submit

Hello everyone i am building a project with php where do i want to display error message on form submit with php but the problem is i am unable to do that my page gets reloads and the message doesnot shows.大家好,我正在用 php 构建一个项目,我想在使用 php 提交的表单上显示错误消息,但问题是我无法做到我的页面重新加载并且消息不显示。 Below i am pasting the codes please help me how to do that.下面我粘贴代码,请帮助我如何做到这一点。

Form part表格部分

<form method="post" action="" id="contact_form">
                      <div class="form-row mt-3">
                        <div class="col">
                          <div class="form-group input-group">
                            <input type="text" class="form-control" name="con_first_name" placeholder="Enter your first name">
                            <span><?php echo $err_fname; ?></span>
                          </div>
                        </div>
                        
                      </div>
                      
                      <div class="form-group"> 
                      
                        <button type="submit" name="con_submit" class="btn show_more con_submit">Submit</button>
                      </div> 
                    </form>


php codes php代码


if(isset($_POST['con_submit'])){
    
    
    
    // Posted form data 
    
    if(!empty($_POST['con_first_name'])){
        $err_fname = "First name cannot be empty";
    }else{
        $fname = htmlentities(strip_tags(mysqli_real_escape_string($con, $_POST['con_first_name'])), ENT_QUOTES, 'UTF-8');
    }
    
    
    
}

I would do it with session!我会用会话来做!

session_start();
if(isset($_POST['con_submit'])){
    
    // Posted form data 
    if(!empty($_POST['con_first_name'])){
        $err_fname = "First name cannot be empty";
        $_SESSION['form_error'] = $err_fname;
    }else{
        $fname = htmlentities(strip_tags(mysqli_real_escape_string($con, $_POST['con_first_name'])), ENT_QUOTES, 'UTF-8');
    }
     
}

And then where you want to show it do:然后你想展示它的地方:

if(isset($_SESSION['form_error'])){
    echo $_SESSION['form_error'];
    unset($_SESSION['form_error']);
}

use this code使用此代码

if(isset($_POST['con_submit'])){
    if(empty($_POST['con_first_name'])){
        $err_fname = "First name cannot be empty";
    }else{
        $fname = htmlentities(strip_tags(mysqli_real_escape_string($con, $_POST['con_first_name'])), ENT_QUOTES, 'UTF-8');
    }
}

you can use with session like this你可以像这样使用会话


session_start();
if(isset($_POST['con_submit'])){
    
    // Posted form data 
    if(empty($_POST['con_first_name'])){
        $err_fname = "First name cannot be empty";
        $_SESSION['form_error_name'] = $err_fname;
    }else{
        $fname = htmlentities(strip_tags(mysqli_real_escape_string($con, $_POST['con_first_name'])), ENT_QUOTES, 'UTF-8');
    }
     
}

if(isset($_SESSION['form_error_name'])){
    echo $_SESSION['form_error_name'];
    unset($_SESSION['form_error_name']);
}

session is secure and every time you post it will set session and then it will remove the value set is session with unset the session. session 是安全的,每次发布它都会设置 session,然后它将删除设置为 session 的值并取消设置 session。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM