Hi Team
I am using correct table and fields, but my back end is not working when inspect i get 200 status but the file itself login.php line 18 and 19 shows undefined array key email and password. How do i solve this issue?
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
exit;
}
$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";
}
?>
// login js
$(document).ready(function() {
$('#login-form').validate({
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();
$.ajax({
type: "POST",
url: "login.php",
data: {email: email, password: password},
success: function(response) {
if (response === "success") {
} else {
$('#login-messages').html('Login failed. Please check your email and password and try again.');
}
},
error: function() {
$('#login-messages').html('Error logging in. Please try again later.');
}
});
}
});
});
Rajkiran SwainPosted May 4, 2023, 5:47 AM
The error "undefined array key email and password" indicates that the `$_POST` superglobal array does not contain the expected keys `email` and `password`. This can happen if the form data is not properly submitted or if the input fields do not have the correct `name` attributes.
To fix this issue, make sure that the form fields for email and password have the correct `name` attributes:
```html
```
Also, check that the jQuery selectors in your JavaScript code match the `id` attributes of the form fields:
```javascript
var email = $('#email').val().trim();
var password = $('#password').val().trim();
```
Make sure that the `id` attributes of the form fields match the jQuery selectors in your JavaScript code.