首页 > 解决方案 > 如何通过json_encode重定向php页面

问题描述

我有两个文件。1 是 controller.php 和 2nd verification.js 一旦验证功能成功执行,我需要重定向。

Controller.php

<?php
session_start();
error_reporting(E_ALL & ~ E_NOTICE);
require ('textlocal.class.php');

class Controller
{
    function __construct() {
        $this->processMobileVerification();
    }
    function processMobileVerification()
    {
        switch ($_POST["action"]) {
            case "send_otp":
                
                $mobile_number = $_POST['mobile_number'];
                
                $apiKey = urlencode('aphfkjshjkskshfkjshfksjhfksjdh');
                $Textlocal = new Textlocal(false, false, $apiKey);
                
                $numbers = array(
                    $mobile_number
                );
                $sender = 'TXTLCL';  //urlencode('TXTLCL')
                echo $otp = rand(100000, 999999);
                $_SESSION['session_otp'] = $otp;
                $message = "Your One Time Password is " . $otp;
                
                try{
                    $response = $Textlocal->sendSms($numbers, $message, $sender);
                    require_once ("verification-form.php");
                    exit();
                }catch(Exception $e){
                    die('Error: '.$e->getMessage());
                }
                break;
                
            case "verify_otp":
                $otp = $_POST['otp'];
                ob_start();
                if ($otp == $_SESSION['session_otp']) {
                    //unset($_SESSION['session_otp']);
                    echo json_encode(array("type"=>"success", "message"=>"Your mobile number is verified!"));
                    
                } else {
                    echo json_encode(array("type"=>"error", "message"=>"Mobile number verification failed"));
                }
                break;
        }
    }
}
$controller = new Controller();
?>

第二个文件verification.js

function sendOTP() {
    $(".error").html("").hide();
    var number = $("#mobile").val();
    if (number.length == 10 && number != null) {
        var input = {
            "mobile_number" : number,
            "action" : "send_otp"
        };
        $.ajax({
            url : 'controller.php',
            type : 'POST',
            data : input,
            success : function(response) {
                $(".container").html(response);
            }
        });
    } else {
        $(".error").html('Please enter a valid number!')
        $(".error").show();
    }
}

function verifyOTP() {
    $(".error").html("").hide();
    $(".success").html("").hide();
    var otp = $("#mobileOtp").val();
    var input = {
        "otp" : otp,
        "action" : "verify_otp"
    };
    if (otp.length == 6 && otp != null) {
        $.ajax({
            url : 'controller.php',
            type : 'POST',
            dataType : "json",
            data : input,
            success : function(response) {
                $("." + response.type).html(response.message)
                $("." + response.type).show();
                
            },
            
            error : function() {
                alert("ss");
                
            }
        });
        
    } else {
        $(".error").html('You have entered wrong OTP.')
        $(".error").show();
    }
}

我不确定,我需要在哪里放置重定向代码。我想要,一旦我的手机号码得到验证。这应该被重定向到abc.php页面。

提前谢谢,帮我解决这个问题。

标签: javascriptphpjsonmobile

解决方案


推荐阅读