user2109706
user2109706

Reputation: 115

How to use PHP to display information from a submit button?

I am trying to debug my php/html code for a faux login project. The basic idea is you can log in with one of two different submit buttons (so no validation) and then be directed to a page that recognizes who is logged in, using sessions. Right now I am just trying to find out why my code isn't working, before I start styling it in anyway.

Here is my first page:

< ?php
session_start();

$_SESSION["username1"] = "Bob";
$_SESSION["username2"] = "Jo";

? >

<! DOCTYPE html>
<html>
<head>

   <title>Login </title>

</head>

<body>

   <h1>Login</h1><br>
   <br>
   <form action="fauxLogin.php" method="post">
      <input type="submit" name="username1" id="Bob" value="Login as Bob"><br>
      <input type="submit" name="username2" id="Jo" value="Login as Jo"><br>
   </form>

</body>
</html>

And here is the code for my second page:

<?php

session_start();

if(isset($_POST['username1'])){
    "Welcome " . $_SESSION['username1'] . "! You are now logged in.";
}
 elseif(isset($_POST['username2'])){
   "Welcome " . $_SESSION['username2'] . "! You are now logged in.";
 }


? >

When I click to login, I am directed to just a blank page. Am I not correctly using the isset function? Or session? Or accessing the forms? Do I need to close the session before it will work?

Thanks!

Upvotes: 0

Views: 807

Answers (3)

Kannan Prasad
Kannan Prasad

Reputation: 1806

Your are not printing anything in the page that's why the page is empty . Use echo to print your message.

Upvotes: 0

Matthew Johnson
Matthew Johnson

Reputation: 5165

Echo out the content. For example:

echo "Welcome " . $_SESSION['username1'] . "! You are now logged in.";

Upvotes: 3

dave
dave

Reputation: 64725

Try echoing your output:

"Welcome " . $_SESSION['username1'] . "! You are now logged in.";

Should be

echo "Welcome " . $_SESSION['username1'] . "! You are now logged in.";

Otherwise, PHP doesn't do anything, it just concatenates your string, and then it never outputs it.

Upvotes: 4

Related Questions