dsa122870 2016-04-01 15:27
浏览 191
已采纳

HTML if和else if语句错误

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!

  • 写回答

2条回答 默认 最新

  • douyousu9691 2016-04-01 15:40
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于下拉菜单选项关联的问题
  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R