How can I link ISSET process page with Javascript function?

Viewed 22

I want to send mail using php and elastic mail config: The two codes work very well when i use submit form that call senEmail function directly like this.

forgot.php

    <form onsubmit="sendEmail()";>
      <input type="email" name="email" placeholder="Email">
      <input type="submit" value="Vérifier">
    </form>

sendEmail script

    <script src="https://smtpjs.com/v3/smtp.js"></script>
    <script>
        function sendEmail(){
            Email.send({
                Host : "smtp.elasticemail.com",
                Username : "test@gmail.com",
                Password : "57AC71719A",
                Port : "2525",
                To : document.getElementById("email").value,
                From : 'test@gmail.com',
                Subject : "This is the subject",
                Body : "message body here",
            }).then(
            message => alert(message)
            );
        }
    </script>

So far it works very well the mail is sent

Now I want to add more conditions like if the mail doesn't exist in DB and more detail, for that I want to link the submit form to another process page. This is what I change in the form of forgot.php page :

    <form action="forgot.php" method="POST" autocomplete="off">
       <input type="email" name="email" placeholder="Email">
       <input type="submit" name="forgot_password" value="Vérifier">
    </form>

This form call that page :

    if (isset($_POST['forgot_password'])) {
    $email = $_POST['email'];
    $_SESSION['email'] = $email;

    $emailCheckQuery = "SELECT * FROM tbl_users WHERE email = '$email'";
    $emailCheckResult = mysqli_query($con, $emailCheckQuery);

    // if query run
    if ($emailCheckResult) {
        // if email matched
        if (mysqli_num_rows($emailCheckResult) > 0) {
            $code = rand(999999, 111111);
            $updateQuery = "UPDATE tbl_users SET code = $code WHERE email = '$email'";
            $updateResult = mysqli_query($con, $updateQuery);
            if ($updateResult) {


   // here how can I call sendEmail function ?
                
                
            }
        }else{
            $errors['invalidEmail'] = "Adresse e-mail invalide";
        }
    }else {
        $errors['db_error'] = "Échec lors de la vérification des e-mails de la base de données !";
    }
0 Answers
Related