0

When the form is submitted, it will verify the user by sending OTP message over mail.

Everything is working fine, email is also being received but index.php still remains after form submission. The page must be redirected to patient_verification.php. Below is the code which I've tried so far.

HTML code:

<form name="poster" id="poster" method="post" class="s12" enctype="multipart/form-data" data-toggle="validator">
    <div class="row">
        <div class="input-field col s5">
            <input id="first_name" name="first_name" type="text" class="validate">
            <label for="first_name" class="">First Name</label>
            <span id="first" class="info text-danger text"></span>
        </div>
        <div class="input-field col s5">
            <input id="last_name" name="last_name" type="text" class="validate">
            <label for="last_name" class="">Last Name</label>
            <span id="last" class="info text-danger text"></span>

        </div>
    </div>
    <div class="row">
        <div class="input-field col s12">
            <input type="email" id="email" name="email" onblur="checkemail();" onkeyup="checkemail();"
                onchange="checkemail();" class="validate">
            <label>Email id</label>
            <span id="userEmail-info" class="info text-danger text"></span>
            <span id="email_status" name="email_status" class="text"></span>
        </div>
    </div>
    <div class="row">
        <div class="input-field col s6">
            <input type="password" name="password" id="password" class="validate">
            <label>Password</label>
            <span id="real" class="info text-danger text"></span>
        </div>
        <div class="input-field col s6">
            <input type="password" name="password1" id="password1" class="validate">
            <label>Confirm password</label>
            <span id="pass1" class="info text-danger text"></span>
            <span id="invalid" class="info text-danger text"></span>
        </div>
    </div>
          <input type="submit" value="Register" id="Register" name="Register" class="waves-effect waves-light log-in-btn">
          
</form>

Below is the Ajax query:

    // save comment to database
  $('#poster').on('submit',function(e){

        //    var need_test = $(".gender:checked").val();

               e.preventDefault();
        
        var fd = new FormData();

        var valid;
        valid = validateContact();

        if (valid) {

            $.ajax({
                url: "phpquery/check.php", // Url to which the request is send
                type: "POST",             // Type of request to be send, called as method
                data: new FormData(this),                
                cache: false,
                contentType: false,
                processData: false,

                success: function (data) {
                                
                }

            });
        };

        // check validations
        function validateContact() {
            var valid = true;
            $(".demoInputBox").css('background-color', '');
            $(".info").html('');

            var minLength = 8;
                var value = $("#password").val();

                  if (value.length < minLength){ 
                     $("#real").html("(Password contains Minimum 8 Characters)");
                        valid = false;
                             }

            if (!$("#first_name").val()) {
                $("#first").html("(First Name Required)");
                $("#first_name").css('background-color', '#FFFFDF');
                valid = false;
            }

            if (!$("#last_name").val()) {
                $("#last").html("(Last Name Required)");
                $("#last_name").css('background-color', '#FFFFDF');
                valid = false;
            }
            
            if (!$("#email").val()) {
                $("#email_status").html("(Email is required)");
                $("#email").css('background-color', '#FFFFDF');
                valid = false;
            }
            if (!$("#email").val().match(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/)) {
                $("#userEmail-info").html("(Invalid Email Address)");
                $("#email").css('background-color', '#FFFFDF');
                valid = false;
            }
            if (!$("#password").val()) {
                $("#real").html("(Password is required)");
                $("#password").css('background-color', '#FFFFDF');
                valid = false;
            }
            if (!$("#password1").val()) {
                $("#pass1").html("(Conform password required)");
                $("#password1").css('background-color', '#FFFFDF');
                valid = false;       
            }
            if ($("#password").val() != $("#password1").val()) {
                $("#invalid").html("(Password do not match)");
                // $("#password1").css('background-color', '#FFFFDF');
                valid = false;
            }

            return valid;
        }

    });

Below is the check.php

<?php
        // Initialize the session
         session_start();

         include "dbconnection.php";
         date_default_timezone_set('Asia/Colombo');

          $cur_date = date('Y-m-d H:i:s', time());
          $cur_date = strtotime($cur_date);
          $cur_date = date('Y-m-d H:i:s', $cur_date); 

            $email =$_POST['email'];
            $password = $_POST['password'];
            $first_name = $_POST['first_name'];
            $last_name= $_POST['last_name'];

            $otp= rand(100000,999999);

            $expiry="1";

            $stmt1 = $con->prepare("INSERT INTO patient_verification (email,activation_code,expire,created) VALUES ( ?, ?, ?, ?)");
            $stmt1->bind_param("ssss",$email,$otp,$expiry,$cur_date);
            $status=$stmt1->execute();

            if($status === true){

            $to      = $email;
            $subject = 'Account Verification';
            $message = '<p> Dear '.$first_name.' '. $last_name.', <br> <br> The account verification code is '.$otp.'. <br><br>Thank You. <br><br> Best Regards from </br> <br><b> <i>  SEUS Hospitals </i></b> </br>';
            
           $headers = 'From: seus@gmail.com' . "\r\n" ;
           $headers .= 'Reply-To: seus@gmail.com' . "\r\n" ;
           $headers .= "MIME-Version: 1.0\r\n";
           $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
            
          $send= mail($to, $subject, $message, $headers);

            header("Location:../patient_verification.php?email=$email&password=$password&first_name=$first_name&last_name=$last_name"); 

          }else{

            echo 'send fail';

          }

        ?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
MTM NASIK
  • 13
  • 5
  • you are sending a request with `ajax` so if u want to redirect after that process, do that inside `success: function(data){}` https://stackoverflow.com/questions/3513971/page-redirect-with-successful-ajax-request – Emre Karakuz Jul 24 '21 at 11:45
  • @EmreKarakuz I want torediect with some data – MTM NASIK Jul 24 '21 at 12:12

1 Answers1

1

Instead of posting the form using the form post method you use javascript to intercept the form post and instead use ajax to post the data to check.php. This means the Location header is also send back to the javascript response. If you want to navigate the browser to another page you need to check the response and use javascript to navigate to the next page (also maybe you want to handle errors in that place):

...
            $.ajax({
                url: "phpquery/check.php", // Url to which the request is send
                type: "POST",             // Type of request to be send, called as method
                data: new FormData(this),                
                cache: false,
                contentType: false,
                processData: false,

                success: function (data) {
                                
                }

            }).done(function(){
                window.open('../patient_verification.php?ema...', '_self');
            }).error(function(){
                alert('Something went wrong!')
            });
...

Then instead of:

...
          }else{

            echo 'send fail';

          }
...

You can use:

          }else{
              http_response_code(400);
          }
Mark
  • 129
  • 2
  • How to send data via `window.open('../patient_verification.php?ema...', '_self');` according to youe suggesstion – MTM NASIK Jul 24 '21 at 12:14
  • You can append it much like you did in php but then in javascript way: `window.open('../patient_verification.php?email=' + $("#email").val() + '&password=' + $("#password").val(), '_self');` – Mark Jul 24 '21 at 12:44