2

I input using the form then $_POST, the code look like this

       <form class="form-signin" method="POST" action="">
          <div class="form-label-group">
            <label for="username">User Name</label>
            <input type="text" name="username" class="form-control" placeholder="username" required autofocus>
          </div>

          <div class="form-label-group">
            <label for="password">Password</label>
            <input type="password" name="password" class="form-control" placeholder="Password" required>
          </div>

          <button class="btn btn-primary mt-3" type="submit" name="SubmitLogin">Submit</button>
        </form>

it's just a simple username and password, then I check the username and password using this code

if(isset($_POST["SubmitLogin"])){
$data = $_POST['SubmitLogin'];
var_dump($data);
$username = $data['username'];
$password = $data['password'];
$result = mysqli_query($mysqli, "SELECT COUNT(*) AS Exist FROM user WHERE username = '$username' AND pwd = '$password' ");
$login = mysqli_fetch_array($result);

if($login['Exist'] == 1){
    $_SESSION["Login"] = true;
    header('Location:index.php');
}else{
    echo '<script language="javascript">';
    echo 'alert("Username/Password wrong")';
    echo '</script>';
  } 
}

and unfortunately as written in the code I want to check what is inside $_POST["SubmitLogin"] by containing it in $data variable and it returns "" instead....what could possibly fix this?

Jatin Parmar
  • 2,272
  • 5
  • 16
  • 27
radren
  • 130
  • 1
  • 9

3 Answers3

4
if(isset($_POST["SubmitLogin"])){
    $data = $_POST;
    $username = $data['username'];
    $password = $data['password'];
    $result = mysqli_query($mysqli, "SELECT COUNT(*) AS Exist FROM user WHERE username = '$username' AND pwd = '$password' ");
    $login = mysqli_fetch_array($result);

    if($login['Exist'] == 1){
     $_SESSION["Login"] = true;
        header('Location:index.php');
    }else{
        echo '<script language="javascript">';
        echo 'alert("Username/Password wrong")';
        echo '</script>';
    } 
}
Ramki
  • 910
  • 3
  • 17
4

Try this:

if(isset($_POST["SubmitLogin"])){

    $username = $_POST['username'] ?? ''; // php7
    $password = $_POST['password'] ?? '';// php7
    var_dump($_POST);
}
Dmitry
  • 3,457
  • 3
  • 19
  • 33
3

You made mistake here $data = $_POST['SubmitLogin'];

So you can use

$data = $_POST;
$username = $data['username'];
$password = $data['password'];

OR

$username = $_POST['username'];
$password = $_POST['password'];
Devsi Odedra
  • 5,071
  • 1
  • 21
  • 33