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 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)