Search code examples
phphtmlformsisset

PHP/HTML - Need form_1 to take user's name, then after submitting it, go to form_2 which will remember the name and greet him


Now I guess this sounds confusing, so I'll try to explain it a little better. What I'm looking for is a form that'll ask a user to input text. After the text is input and submitted, the page will go to the second form (in case of which nothing was input, the site will ask the user to input it again), which will greet the user with the name/text he used, like "Greetings, (name)!". The second form already has the log-in code working, but I need the first form that'll go to the second without crashing.

This is a very rough sketch of what I expect of form one, and this is a very rough sketch of what form 2 already does, except the name thing

I'll provide the login code here, so it works with the first form, (if that even makes sense..)

<?php
session_start();

$name = $_GET['name'] ?? "";
$username = $_POST['username'] ?? "";
$pass = $_POST['pass'] ?? "";

$isLogged = false;
if(!empty($username) && !empty($pass)) {
    if($username === 'x' && $pass === 'y') {
        $_SESSION['isLogged'] = true;
        $_SESSION['username'] = $username;
        $_SESSION['pass'] = $pass;
    }
    else {
        $errLog= true;
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Forms</title>
</head>
<body>
    <h1>Greetings <?= $name ?></h1>

    <?php if(isSet($errLog) && $errLog=== true): ?>
        <h2>Wrong login!</h2>
    <?php endif; ?>

    <?php if(!isSet($_SESSION['isLogged']) || $_SESSION['isLogged'] === false): ?>
    <form method="post">
        <input type="text" name="username">
        <input type="password" name="pass">
        <button type="submit">Send</button>
    </form>

    <?php else: ?>
    <p>Given login:</p>
    <ul>
        <li>Login: <?= $_SESSION['username']  ?> </li>
        <li>Password: <?= $_SESSION['pass'] ?> </li>
        <li><a href="http://localhost/logout.php">Logout</a></li>
    </ul>

    <?php endif; ?>
</body>
</html>

Again, this is just a test site that's not supposed to be safe or anything. I'm a beginner and that's the project we had to do.

Now for the first form, I've tried !empty and !isSet commands, but it didn't work at all. I can't provide any code because nothing I've tried so far have given the expected results.

There's also the "logout.php" file, which is a code to redirect you back to the login site (form 2), so it's not that important.

And to sum it up, sorry for such a long post, I'm just stuck and everything I've tried so far hasn't worked. I'm completely new here as well, so I hope I can get some understanding. Cheers.


Solution

  • If this is what you want then by my understanding add a another page which will post the name to your login page like bellow code/page:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Forms 1</title>
    </head>
    <body>
        <h1>What is your name </h1>
    
        <form method="post" action="test4.php">
            <input type="text" name="name" required="true">
            
            <button type="submit">Send</button>
        </form>
    
       
    </body>
    </html>
    

    here in form action insted of test4.php add form_2.php