0

when I press submit button I have error like: I did everything like a guy in a tutorial and I have still this error. Thanks for help in advance. I was trying to solve this with links which you've sent me, but it still doesnt work. I created table in database and I store there values, db_name = useraccounts and table: users.

Undefined index: username in jslogin.php

I have no idea what's wrong. Here is my code below.


<?php
require_once('config.php');

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




$sql = "SELECT  * from useraccounts.users where username = ? AND password = ? LIMIT=1 ";
$stmtselect = $db->prepare($sql);
$result = $stmtselect->execute([$username,$password]);
if($result){
    $user=$stmtselect->fetch(PDO::FETCH_ASSOC);
    if($stmtselect->rowCount() > 0){
        
    echo '1';
    }else{
        echo'Nie znaleziono uzytkownika';
    }
    }else{
        echo'Wystapily bledy przy laczeniu z baza danych';
    }
    ?>


<!DOCTYPE html>
<html>
<head>
<title> Strona o nalewkach </title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container h-100">
  <div class="d-flex justify-content-center h-100">
    <div class="user_card">
      <div class="d-flex justify-content-center">
         <div class="brand_logo_container">
             <img src= "img/logo.png" class="brand_logo" alt="Nalewki">
         </div>
      </div>
      <div class="d-flex justify-content-center form_container">
        <form>
        <div class="input-group mb-3">
         <div class="input-group-append">
           <span class="input-group-text"><i class="fas fa-user"></i></span>
           </div>
           <input type="text" name="username" id="username" class="form-control input_user" required>
           </div>
        
        <div class="input-group mb-2">  
         <div class="input-group-append">
           <span class="input-group-text"><i class="fas fa-key"></i></span>
         </div>
           <input type="password" name="password" id="password" class="form-control input_pass" required>
        </div>
    
    <div class="form-group">
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="rememberme" class="custom-control-input" id="customControlInLine">
      <label class="custom-control-label" for="customControlInLine">Zapamiętaj mnie</label>
      </div>
     </div>
    
   </div>
   <div class="d-flex justify-content-center mt-3 login-container">
     <button type="button" name="button" id="login" class="btn login_btn">Login</button>
     </div>
     </form>
     <div class="mt-4"> 
        <div class="d-flex justify-content-center links">
        Nie posiadasz konta? <a href="#" class="ml-2">Zarejestruj sie</a>
         </div>
          <div class="d-flex justify-content-center">
            <a href="#">Forgot your password?</a>
         </div>
        </div>
    </div>
  </div>
  
</div>
  

<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
<script>
$(function(){
    $('#login').click(function(e){
        var valid = this.form.checkValidity();
        if(valid){
            var username = $('username').val();
            var password = $('password').val();
        }
        e.preventDefault();
        
        $.ajax({
            type: 'POST',
            url: 'jslogin.php',
            data: {username: username, password: password},
            success: function(data){
                alert(data);
                if ($.trim(data)==="1"){
                    setTimeout(' window.location.href = "index.php"',2000);
                }
            },
            error: function(data){
                alert('wystapil blad');
            }
        });
    });
});

</script>
</body>
</html>
Bordo
  • 39
  • 7
  • Please fix your question and properly format the code. It is currently showing syntax errors, which makes it hard for us to help you debug the logic. If you've pasted all of this from your code, then please go back and fix the syntax errors first. The first line shouldn't even be accepted by your IDE as the single quote is mismatched. – Ro Achterberg Nov 08 '20 at 10:39
  • Are you sure you've set the right `action` and `method` on your `form`? Please provide the full form and any JavaScript you may be using to submit it. – Ro Achterberg Nov 08 '20 at 10:42
  • How do you submit, via Javascript? Is your form method `POST`? Is the form on the same page? Do you get this error only when you submit? – brombeer Nov 08 '20 at 10:42
  • I've edit my post, thanks for help in advance. – Bordo Nov 08 '20 at 11:08
  • `$('username').val();` should be either `$('#username').val();` or `$('input[name="username"]').val();`. – Ro Achterberg Nov 08 '20 at 11:16

0 Answers0