1
votes

First and foremost, this is my 1st time writing PHP code, so please forgive my newbie'isms.

I have a login form with 3 submit buttons (post method) named login, register and forgot. If I select login button, the login function in the PHP code gets called, however, the same is not true for the register and forgot buttons. Its almost like the only submit button that is working is the login buttons. My best guess at this point is there is id/name that not correct. For the sake of brevity I've removed the CSS portion. Any points in the right direction will be most appreciated.

<?php
function login(){
//do stuff
echo "Login";
}

function register(){
// do stuff
echo "Register";
}

function forgot(){
//do stuff
echo "Forgot";

}

 if($_SERVER["REQUEST_METHOD"] == "POST") {
   if(isset($_POST['login'])) {
    login();
   }
    if(isset($_POST['register'])) {
    register();
   }
    if(isset($_POST['forgot'])) {
    forgot();
   }
}
?>

<html>
<head>
<meta charset="utf-8">
<title>Login Page</title>
</head>
<body>
<div id="login">
<h1><strong>Welcome!</strong> Please login.</h1>
<form action="" method="post">
<fieldset>
<p><input type="text" name="username" required value="Username" onBlur="if(this.value=='')this.value='Username'" onFocus="if(this.value=='Username')this.value=''"></p>
<p><input type="password" name="password"></p>
<p><input type="submit" name= "login" value="Login"></p>
<p><input type="submit" name= "register" value="Register"></p>
<p><input type="submit" name= "forgot" value="Forgot Password"></p>
</fieldset>
</form>
</div> <!-- end login -->
</body>
</html>
2
I just tested and for me all buttons are working. Maybe I didn't understand your question? I get those words in functions depending on which button I pressed.Gytis TG
Your code is working fine for me too.RanjanaLK
I copied this into another file and it worked too, but not in my original code. So I'm not sure what I did to make that not work. Sorry for the bother and Thanks for the assistance.user3254596
I'm glad this was solved. :)Gytis TG
Your code looks just fine. Maybe the problem is somewhere else. print_r() the $_POST array to see the data you get there.Eisa Adil

2 Answers

1
votes

HTML forms can not contain more than one input of type "submit" (which means others would not be functioning)

register and forgot are links rather than buttons. Instead you could use:

<a href="/register.php">Register</a>

Another alternative is to use javascript to make these buttons act as links.

0
votes

After copying and pasting my code into another file. It worked without any issues. The multiple submit buttons work without any issues.