Click here to Skip to main content
15,745,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Team

On my back end i am getting undefined key array email and password on line 18 and 19. Basically i am doing a session so once users are validated to login must be redirected to profile.php. But when debuggin from the form i get this error.

What I have tried:

//php code


$dbHost = 'localhost';
$dbName = 'ecommerce_store';
$dbUser = 'root';
$dbPass = '';

try {
  $pdo = new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUser, $dbPass);
} catch(PDOException $e) {
  echo "Connection failed: " . $e->getMessage();

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

$stmt = $pdo->prepare("SELECT * FROM users WHERE email = :email");
$stmt->execute(['email' => $email]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);

if ($user && password_verify($password, $user['password'])) {
  // valid credentials, store user session
  $_SESSION['user'] = $user;
  echo "success";
} else {
  // invalid credentials
  echo "failure";


// js file
<pre lang="Javascript">$(document).ready(function() {
    rules: {
      email: {
        required: true,
        email: true
      password: {
        required: true
    messages: {
      email: {
        required: "Please enter your email address",
        email: "Please enter a valid email address"
      password: {
        required: "Please enter your password"
    submitHandler: function(form) {
      var email = $('#email').val().trim();
      var password = $('#password').val().trim();
        type: "POST",
        url: "login.php",
        data: {email: email, password: password},
        success: function(response) {
          if (response === "success") {
            window.location.href = "profile.php";
          } else {
            $('#login-messages').html('<div class="alert alert-danger" role="alert">Login failed. Please check your email and password and try again.</div>');
        error: function() {
          $('#login-messages').html('<div class="alert alert-danger" role="alert">Error logging in. Please try again later.</div>');

// form
<!--Form registration-->

<pre lang="HTML"><div class="card">
  <div class="card-body">
<form id="login-form" action="login.php" method="POST">
  <div class="form-group text-center">
   <input type="email" class="form-control" id="email" placeholder="Email" name="email" required>
  <div class="form-group text-center">
   <input type="password" class="form-control" id="password" placeholder="Password" name="password" required>
  <button type="submit" class="btn btn-primary">Login</button>
<div id="login-messages"></div>

<!----Javascript files validation during form submission ---->
<script src=""></script>
<script src=""></script>
<script src="js/login.js"></script>
Updated 3-May-23 18:33pm

1 solution

your 'data' field is an Array

url: "login.php",
        data: {"email": email, "password": password},
        success: function(response) {

to get values in :

$_POST[0][0] ; // email
$_POST[0][1] ; // password

/or, as it's an associative Array ;

$_POST[0]["email"] ; // email
$_POST[0]["password] ; // password

the data field is at index = 0 : $_POST[0]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900