Home > database >  How to print error of php information to span in HTML?
How to print error of php information to span in HTML?

Time:04-22

I use PHP to check the password and re-password if it is correct I will enter the data if it is wrong I will print a message in the span tag. How to print errors in span tag in HTML? Thank for help

<?php
    if(isset($_POST["username"]) && isset($_POST["password"]) && isset($_POST["name"]) && isset($_POST["e-mail"])) {
        $accPhone = $_POST["username"];
        $accPasswd = $_POST["password"];
        $accPasswd=md5($_POST['password']);
        $accName = $_POST["name"];
        $accMail = $_POST["e-mail"];
        
        if('password' === 're-password') {
            $query = "INSERT INTO accounts(accPhone,accPasswd,accName,accMail,accType) VALUES ('$accPhone','$accPasswd','$accName','$accMail','1')";
            $result = $conn->query($query) or die("Query failed: " . $conn->error);
        } else {
            // print error to span tag in html
        }
    }  

CodePudding user response:

Create one variable called $error at the top of the code and keep it null or 0 while will be easy to check in html later, then just assign your error to one variable and in html part check if that variable is not null or 0 and print it in span

<?php
    $error = '';
    if(isset($_POST["username"]) && isset($_POST["password"]) & isset($_POST["name"]) && isset($_POST["e-mail"])) {
        $accPhone = $_POST["username"];
        $accPasswd = $_POST["password"];
        $accPasswd=md5($_POST['password']);
        $accName = $_POST["name"];
        $accMail = $_POST["e-mail"];

        if('password' === 're-password') {
            $query = "INSERT INTO   accounts(accPhone,accPasswd,accName,accMail,accType) VALUES ('$accPhone','$accPasswd','$accName','$accMail','1')";
            $result = $conn->query($query)
            $error = "Query failed: " . $conn->error;
        }
    }
?>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <?= $error != '' ? '<span >'.$error.'</span>' : '' ?>

</body>
</html>

CodePudding user response:

You can do this simply the following code:

<?
if(isset($_POST["username"]) && isset($_POST["password"])
&& isset($_POST["name"]) && isset($_POST["e-mail"])) {
$accPhone = $_POST["username"];
$accPasswd = $_POST["password"];
$accPasswd=md5($_POST['password']);
$accName = $_POST["name"];
$accMail = $_POST["e-mail"];
if('password' === 're-password') {
$query = "INSERT INTO    accounts(accPhone,accPasswd,accName,accMail,accType)
VALUES ('$accPhone','$accPasswd','$accName','$accMail','1')";
$result = $conn->query($query)
or die("Query failed: " . $conn->error);
} else {
echo "<span class='alert alert-danger'>". $error ."</span>";
}
}  
?>
  •  Tags:  
  • php
  • Related