简体   繁体   English

如何使用PHP在视图页面中显示错误消息

[英]How to display error message in view page using PHP

I need to display the error message using php.I am explaining my code below. 我需要使用php显示错误消息。我在下面解释我的代码。

addcomplain.php: addcomplain.php:

<div style="color:#F00; text-align:center;"><?php echo $error?></div>
<form name="billdata" id="billdata"  enctype="multipart/form-data" method="POST" onSubmit="javascript:return checkForm();" action="complain.php">
</form>

Suppose i have a form like above and when i submit the below file is called. 假设我有一个像上面的表格,当我提交下面的文件时被调用。

complain.php: 抱怨.php:

$sql="SELECT * FROM medilink_complain WHERE email='".$email."'";
$selres = mysqli_query($con,$sql);
 if(mysqli_num_rows($selres ) > 0)
   {
    $error = "Email Id already exists!";

    }

when the above error will come,i need it should display just above the form.In my case i am not getting anything.Please help me to resolve this issue. 当出现上述错误时,我需要将其显示在表格上方。就我而言,我什么也没收到。请帮助我解决此问题。

Try this 尝试这个

addcomplain.php: addcomplain.php:

<?php 
if(isset($_SESSION["error"])){
    echo '<div style="color:#F00; text-align:center;">'. $_SESSION["error"] .'</div>';;
    unset($_SESSION["error"]);
} ?>

complain.php: 抱怨.php:

session_start();
$sql="SELECT * FROM medilink_complain WHERE email='".$email."'";
$selres = mysqli_query($con,$sql);
 if(mysqli_num_rows($selres ) > 0)
   {
        $_SESSION["error"] = "Email Id already exists!";
   }

You don't need a session. 您不需要会话。

Merge your code into 1 file. 将您的代码合并为1个文件。 Show only $error if he is not null , like so: 如果他不为null ,则仅显示$error ,如下所示:

<?php
   $error = null;

   if ($_SERVER['REQUEST_METHOD'] == 'POST') {
       $sql="SELECT * FROM medilink_complain WHERE email='".$email."'";
       $selres = mysqli_query($con,$sql);

       if(mysqli_num_rows($selres ) > 0) {
           $error = "Email Id already exists!";
       }
    }

?>

<div style="color:#F00; text-align:center;"><?= (!is_null($error) ? $error : ''); ?></div>
     <form name="billdata" id="billdata"  enctype="multipart/form-data" method="POST" onSubmit="javascript:return checkForm();" action="complain.php">
     </form>
</div>

Try like this : 尝试这样:

complain.php: 抱怨.php:

$sql="SELECT * FROM medilink_complain WHERE email='".$email."'";
$selres = mysqli_query($con,$sql);
 if(mysqli_num_rows($selres ) > 0)
   {
    $_SESSION["error"] = "Email Id already exists!";

    }

In addcomplain.php : 在addcomplain.php中:

$error = $_SESSION["error"];
unset($_SESSION["error"]);
    <div style="color:#F00; text-align:center;"><?php echo $error?></div>
    <form name="billdata" id="billdata"  enctype="multipart/form-data" method="POST" onSubmit="javascript:return checkForm();" action="complain.php">
    </form>

You can write couple of functions to make it easier: 您可以编写一些函数来简化操作:

function setMessage($message){
    $_SESSION['msg'] = $message;
}

function showMessage(){
    echo $_SESSION['msg'];
    unset($_SESSION['msg']);
}

complain.php: 抱怨.php:

if(mysqli_num_rows($selres ) > 0)
{
    $error = "Email Id already exists!";
    setMessage($error);
}

addcomplain.php: addcomplain.php:

<div style="color:#F00; text-align:center;"><?php echo showMessage(); ?></div>

Note: make sure you have started the session, using session_start() in the top of the page. 注意:请使用页面顶部的session_start()确保已启动会话。

Use Session object for this. 为此使用Session对象。

$sql="SELECT * FROM medilink_complain WHERE email='".$email."'";
$selres = mysqli_query($con,$sql);

if(mysqli_num_rows($selres ) > 0)
{
    $_SESSION['error'] = "Email Id already exists!";

}

The display like this. 这样的显示。

<?php 
    $error = "";
    if(isset($_SESSION['error'])) 
    { 
        $error = $_SESSION['error']; 
        unset($_SESSION['error']); 
    } 
  ?>
  <div style="color:#F00; text-align:center;">
      <?php echo $error; ?>
  </div>

  <form name="billdata" id="billdata"  enctype="multipart/form-data" method="POST" onSubmit="javascript:return checkForm();" action="complain.php">
  </form>

complain.php 抱怨.php

    <?php
    $sql="SELECT * FROM medilink_complain WHERE email='".$email."'";
    $selres = mysqli_query($con,$sql);
    if(mysqli_num_rows($selres ) > 0)
    {
       $error = "Email Id already exists!";
    } else {
       $error = '';
    }
    include 'addcomplain.php'; // <----
    ?>

addcomplain.php addcomplain.php

    <html>
    <body>    
    <div style="color:#F00; text-align:center;"><?php echo $error?></div>
    <form name="billdata" id="billdata"  enctype="multipart/form-data" method="POST" onSubmit="javascript:return checkForm();" action="complain.php">
    <input value="test" />
    </form>    
    </body>
    </html>

...works for me! ...为我工作! No session variable needed. 无需会话变量。

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

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