Parsing error while requesting an OTP in php site

Viewed 37

I am trying to make a site where when we enter an email it sent the OTP, to the mail. however, what is happening is that when I provide the mail, it throws me this error and got stuck there only.

The error I am getting: Uncaught (in promise) SyntaxError: Unexpected token '<', "<br /> <b>"... is not valid JSON.

this happens in the main.js (line 115) file.

error snapshot

PS: however there is a catch, it works on my friend's local computer, but not working on where I hosted in EC2 in AWS and my own localhost.

main.js

email_submit.addEventListener(
    "click",
    async function () {
        email_loader.classList.remove( "hidden-class" );
        email_text.classList.add( "hidden-class" );
        email_loader.classList.add( ...loading_classes );
        if (ValidateEmail( email.value )) {
            let resp               = await asyncAjaxRequest( verify_email, "POST", { email: email.value } );
            const { status, data } = resp;
            switch (status) {
                case 200:
                    sessionStorage.setItem( "email", email.value );
                    alert_msg.innerHTML = data;
                    showOTPDiv();
                    break;
                default:
                    alert_msg.innerHTML = data;
                    break;
            }

        } else {
            alert_msg.innerHTML = "Please enter a valid email address!";
        }
        email_loader.classList.add( "hidden-class" );
        email_text.classList.remove( "hidden-class" );
        email_loader.classList.remove( ...loading_classes );
    }
)
otp_submit.addEventListener(
    "click",
    async function () {
        email_loader.classList.remove( "hidden-class" );
        email_text.classList.add( "hidden-class" );
        email_loader.classList.add( ...loading_classes );
        if (ValidateOTP( otp.value )) {
            let email              = sessionStorage.getItem( "email" );
            let resp               = await asyncAjaxRequest( verify_otp, "POST", { email: email, otp: otp.value } );
            const { status, data } = resp;
            console.log( resp );
            switch (status) {
                case 200:
                    alert_msg.innerHTML = data;
                    showRegisterSuccessDiv();
                    break;
                default:
                    alert_msg.innerHTML = data;
                    break;
            }

        } else {
            alert_msg.innerHTML = "Please enter a valid email address!";
        }
        email_loader.classList.add( "hidden-class" );
        email_text.classList.remove( "hidden-class" );
        email_loader.classList.remove( ...loading_classes );
    }
)

const asyncAjaxRequest = async(
    link,
    method,
    body
) => {
    let res;
    console.log( body );
        body           = JSON.stringify( body );
    if (body === null) {
        res = await fetch(
            link,
            {
                method: method,
                }
        );
    } else {
        res = await fetch(
            link,
            {
                method: method,
                body: body,
                }
        );
    }
        const data = await res.json();
        console.log( data );
        //console.log(res.status);
        return { status: res.status, data: data };
    };

There's more to the file but just posted the relevant part. network tab after making request

const ValidateEmail = function (email) {
    if (/^\w+([.-]?\w+)@\w+([.-]?\w+)(.\w{2,3})+$/.test( email )) {
        return (true)
    }
    return (false)
}

This is my validate function.

Register_mail.php

<?php
require_once './functions.php';
require_once '../includes/db.php';
$data  = file_get_contents( 'php://input' );
$data  = json_decode( $data, true );
$email = $data['email'];
if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL )) {
    http_response_code( 400 );
    returnResponse( 'Enter Valid Email' );
    exit();
}

$OTP = genetateOTP();
if ( $con ) {
    try {
        $stmt = $con->prepare( 'SELECT is_activated FROM `subscribers` WHERE email=?' );
        $stmt->bind_param( 's', $email );
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result( $is_activated );
        $stmt->fetch();
        $numRows = $stmt->num_rows;
        if ($is_activated === 1) {
            http_response_code( 201 );
            returnResponse( 'Already Registered, please try different email address' );
            exit();
        }
        if ($numRows === 0) {
            $email_hash = md5( $email );
            $stmt       = $con->prepare( 'INSERT INTO `subscribers` (`email`, `otp`, `email_hash`) VALUES (?, ?, ?)' );
            $stmt->bind_param( 'sis', $email, $OTP, $email_hash );
            $stmt->execute();
        } else {
            $stmt = $con->prepare( 'UPDATE `subscribers` SET `otp`=? WHERE `email`=?' );
            $stmt->bind_param( 'is', $OTP, $email );
            $stmt->execute();
        }
        $SERVER_NAME = isset( $_SERVER['SERVER_NAME'] ) ? $_SERVER['SERVER_NAME'] : '';
        $mail_text   = "<h1>Welcome to XKCD Comic Mailer.</h1>\nYour OTP for email validation is $OTP.\nEnjoy!\n or";
        $mail_text  .= "click <a href='http://" . $SERVER_NAME . '/subscribe.php?email=' . $email . '&otp=' . md5( $OTP ) . "'>here.</a>";

        $mail_subject = 'XKCD Comic Mailer - OTP';
        sendMail( $email, $mail_subject, $mail_text, '' );
        http_response_code( 200 );
        returnResponse( 'Check your inbox for OTP' );
        exit();
    } catch (\Throwable $th) {
        echo $th;
        http_response_code( 500 );
        returnResponse( 'Something went wrong' );
        exit();
    }
}
0 Answers
Related