I am working on ChatApp by Php ,MySQL and javascript
But i face alot of errors
now when i am trying to make the error text give me the right error message there somthing happens and i can`t understand (i have added an picture)
please any one can help!!!
The code error:
Uncaught TypeError: Cannot read properties of null (reading ‘style’)
i am trying to make the error text give me the right error message like this :
signup.js:
const form = document.querySelector(".signup form"),
continueBtn = form.querySelector(".button input"),
errorText = form.querySelector(".error-text");
form.onsubmit = (e) => {
e.preventDefault();
}
continueBtn.onclick = () => {
let xhr = new XMLHttpRequest();
xhr.open("POST", "php/signup.php", true);
xhr.onload = () => {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
let data = xhr.response;
if (data === "success") {
location.href = "users.php";
} else {
errorText.style.display = "block";
errorText.textContent = data;
}
}
}
}
let formData = new FormData(form);
xhr.send(formData);
}
signup.php:
<?php
session_start();
include_once "config.php";
$fname = mysqli_real_escape_string($conn, $_POST['fname']);
$lname = mysqli_real_escape_string($conn, $_POST['lname']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$password = mysqli_real_escape_string($conn, $_POST['password']);
if(!empty($fname) && !empty($lname) && !empty($email) && !empty($password)){
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
$sql = mysqli_query($conn, "SELECT * FROM users WHERE email = '{$email}'");
if(mysqli_num_rows($sql) > 0){
echo "$email - This email already exist!";
}else{
if(isset($_FILES['image'])){
$img_name = $_FILES['image']['name'];
$img_type = $_FILES['image']['type'];
$tmp_name = $_FILES['image']['tmp_name'];
$img_explode = explode('.',$img_name);
$img_ext = end($img_explode);
$extensions = ["jpeg", "png", "jpg"];
if(in_array($img_ext, $extensions) === true){
$types = ["image/jpeg", "image/jpg", "image/png"];
if(in_array($img_type, $types) === true){
$time = time();
$new_img_name = $time.$img_name;
if(move_uploaded_file($tmp_name,"images/".$new_img_name)){
$ran_id = rand(time(), 100000000);
$status = "Active now";
$encrypt_pass = md5($password);
$insert_query = mysqli_query($conn, "INSERT INTO users (unique_id, fname, lname, email, password, img, status)
VALUES ({$ran_id}, '{$fname}','{$lname}', '{$email}', '{$encrypt_pass}', '{$new_img_name}', '{$status}')");
if($insert_query){
$select_sql2 = mysqli_query($conn, "SELECT * FROM users WHERE email = '{$email}'");
if(mysqli_num_rows($select_sql2) > 0){
$result = mysqli_fetch_assoc($select_sql2);
$_SESSION['unique_id'] = $result['unique_id'];
echo "success";
}else{
echo "This email address not Exist!";
}
}else{
echo "Something went wrong. Please try again!";
}
}
}else{
echo "Please upload an image file - jpeg, png, jpg";
}
}else{
echo "Please upload an image file - jpeg, png, jpg";
}
}
}
}else{
echo "$email is not a valid email!";
}
}else{
echo "All input fields are required!";
}
?>