2

I have an html form with two buttons. One is to log in using a mysql query, and the other is to redirect to a registration page. The login button works as it should, but the register button is a little odd. It will run the function that it's supposed to, but it also returns an error regarding the login button portion of the if statement.

HTML:

<html><head><title> Sign-In </title></head>
<body>

<h1> Login Form </h1>
<div id="Sign-In">
<form method="POST" action="connectivity.php">
Username: <input type='text' name='user'> <br><br>
Password: <input type='password' name='password'> <br><br>
<input type="submit" class="button" name="login_button" value="Login"> <br>
<input type="submit" class="button" name="register_button" value="Register">

</form>
</div>
</body>
</html>

PHP:

<?php
session_start();   //starting the session for user profile page
define('DB_HOST', 'localhost');
define('DB_NAME', 'project2');
define('DB_USER','root');
define('DB_PASSWORD','password');

if($_POST['login_button']){
    signin();
}
else if($_POST['register_button']){
    register();
}

function signin()
{
    $con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
    $db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());

    $ID = $_POST["user"];
    $Password = $_POST["password"];

    if(!empty($_POST['user']))   //checking the 'user' name which is from Sign-In.html, is it empty or have some text
    {
        $query =("SELECT *  FROM Customers where userName = '" .$ID. "' AND pass = '" .$Password. "';");

        $result = mysql_query($query, $con);
        if (mysql_num_rows($result) == 0)
        {
            echo "Invalid username or password.";
        }
        else
        {
            echo "Successfully logged in!";
        }
    }
    else
    {
        echo "Username field is empty!";
    }
}

function register()
{
    echo "please register";
}



?>

The error I get is:

Notice: Undefined index: login_button in C:\xampp\htdocs\project2\connectivity.php on line 8
please register

As you can see, the echo for the register() function is working fine, but I guess it's also trying the signin() function. I'm needing only one of the functions to execute, depending on the button clicked. Thanks in advance!

Tangwheeler
  • 207
  • 1
  • 11

2 Answers2

1

When you do if($_POST['login_button']){, you're checking if the button existed in the previous form and was sent via post, which it was since it was inside the form.

If you want to check which button was clicked, you must do this instead:

if(isset($_POST['login_button'])){
    signin();
}
else if(isset($_POST['register_button'])){
    register();
}

The button won't be set if it wasn't clicked.

Phiter
  • 14,570
  • 14
  • 50
  • 84
  • Excellent! Thank you very much! This is my first experience with html/php, and it's been a little rough. You'd think an instructor would start with a simpler project... -.- Thanks again! – Tangwheeler Apr 01 '16 at 15:42
  • This is a simple C**R**UD, in this case, only a select. Quite easy actually. – Phiter Apr 01 '16 at 15:43
1

You can't have multiple submit buttons in a form. See this for an explanation: Multiple submit buttons in an HTML form

Community
  • 1
  • 1
tmarthal
  • 1,498
  • 19
  • 28