2

I recently finished my login system but I have a question about PHP. How can I echo text or html to a certain part of html?

For example. Lets say the code below is all inside one PHP file. How could I echo the 2 echos in the php code to the html where the comment is?

So if I were to display this page and if their login failed it would echo "The username and password you entered did not match our records. Please double-check and try again." to that comment in the html.

I hope that makes sense, Thanks!

<?php
    session_start();

    if (isset($_POST['username']) && isset($_POST['password'])) {
        include_once("db.php");
        $username = mysqli_real_escape_string($sqlcon, $_POST['username']);
        $username = strtoupper($username);
        $password = mysqli_real_escape_string($sqlcon, $_POST['password']);

        for ($i = 0; $i < 1000; $i++) {
            $password = hash(sha512, $password . $username);
        }

        $userQuery = "SELECT * FROM users WHERE username = '" . $username . "' LIMIT 1";
        $user = mysqli_query($sqlcon, $userQuery);

        if (mysqli_num_rows($user) > 0) {
            $user = mysqli_fetch_assoc($user);
            $id = $user['id'];
            $databasepass = $user['password'];

            if ($password === $databasepass) {
                $_SESSION['username'] = $username;
                $_SESSION['id'] = $id;
                header("Location: admin.php");
            } else {
                echo "The username and password you entered did not match our records. Please double-check and try again.";
            }
        } else {
            echo "The username and password you entered did not match our records. Please double-check and try again.";
        }
    }
?>

<!DOCTYPE html>
<html>
    <head>
        <title>ADMIN</title>
    </head>
    <body>
        <div>
            <!--ECHO THOSE TWO ECHOS ABOVE HERE-->
        </div>
    </body>
</html>
1
  • I'd personally recommend looking into MVCs. It'll help keep code clean and separate your html from php Commented Apr 28, 2016 at 23:58

3 Answers 3

3

You can assign the text to a variable and use PHP down inside your HTML to echo that.

<?php
    session_start();

    if (isset($_POST['username']) && isset($_POST['password'])) {
        include_once("db.php");
        $username = mysqli_real_escape_string($sqlcon, $_POST['username']);
        $username = strtoupper($username);
        $password = mysqli_real_escape_string($sqlcon, $_POST['password']);

        for ($i = 0; $i < 1000; $i++) {
            $password = hash(sha512, $password . $username);
        }

        $userQuery = "SELECT * FROM users WHERE username = '" . $username . "' LIMIT 1";
        $user = mysqli_query($sqlcon, $userQuery);

        if (mysqli_num_rows($user) > 0) {
            $user = mysqli_fetch_assoc($user);
            $id = $user['id'];
            $databasepass = $user['password'];

            if ($password === $databasepass) {
                $_SESSION['username'] = $username;
                $_SESSION['id'] = $id;
                header("Location: admin.php");
            } else {
                $result = "The username and password you entered did not match our records. Please double-check and try again.";
            }
        } else {
            $result = "The username and password you entered did not match our records. Please double-check and try again.";
        }
    }
?>

<!DOCTYPE html>
<html>
    <head>
        <title>ADMIN</title>
    </head>
    <body>
        <div>
            <?php echo $result; ?>
        </div>
    </body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

3

Change echo in your code to variable, eg. $message = "......";

then inside your div

<?php

echo $message;

?>

Comments

0

if $message is not set it would throw an error, so why not either use:

<?=isset($message)?$message:"";?>

of you can initialise

$message = ""; 

in your code then display $message / $result as prescribed by Jacob See & rishal

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.