submitting login and sign up forms using AJAX code example
Example: submitting login and sign up forms using AJAX
<?php
session_start();
if (isset($_POST['loginbtn'])) {
include_once 'dbh.inc.php';
$uid = trim($_POST['uid']);
$pwd = trim($_POST['pwd']);
if (empty($uid) || empty($pwd)) {
$conn = null;
header("Location: ../index.php?login=error_field");
exit();
} else {
$stmt = $conn->prepare("SELECT user_uid FROM users WHERE user_uid = ?");
$stmt->execute([$uid]);
if ($stmt->rowCount() < 1) {
$conn = null;
header("Location: ../index.php?login=error_username");
exit();
} else {
$stmt = $conn->prepare("SELECT user_pwd FROM users WHERE user_uid = ?");
$stmt->execute([$uid]);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$hashedPwdCheck = password_verify($pwd, $result[0]['user_pwd']);
if ($hashedPwdCheck == false) {
$conn = null;
header("Location: ../index.php?login=error_password");
exit();
} else {
$stmt = $conn->prepare("SELECT user_id, user_first, user_last, user_email, user_uid FROM users WHERE user_uid = ?");
$stmt->execute([$uid]);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$_SESSION['user_id'] = $result[0]['user_id'];
$_SESSION['user_first'] = $result[0]['user_first'];
$_SESSION['user_last'] = $result[0]['user_last'];
$_SESSION['user_email'] = $result[0]['user_email'];
$_SESSION['user_uid'] = $result[0]['user_uid'];
$conn = null;
header("Location: ../updates.php?login=success");
exit();
}
}
}
} else {
header("Location: ../index.php?login=error");
exit();
}
?>