简体   繁体   中英

alert not popping up in php

In a particular php file iam using a header and an alert box before it executes header, but its neglecting alert and executing header directly..!!please help me resolving this...!!pardon if i went wrong somewere. .php

<?php
header('location: formprofile.php');
session_start();
        require_once 'DB_Functions.php';
        $db = new DB_Functions();

        // json response array
        $response = array("error" => false);
        if (!empty($_POST['salutation']) && !empty($_POST['fname']) && !empty($_POST['lname']) && !empty($_POST['dob']) && !empty($_POST['mobile']) && !empty($_POST['country']) && !empty($_POST['state']) && !empty($_POST['city']) && !empty($_POST['pin'])){
                /*
                if required include seperate validation
                for some fields which require validation
                */
                // receiving the post params
                $salutation = ($_POST['salutation']);
                $fname = trim($_POST['fname']);
                $lname = trim($_POST['lname']);
                $dob = trim($_POST['dob']);
                $mobile = trim($_POST['mobile']);
                $country = trim($_POST['country']);
                $state = trim($_POST['state']);
                $city = trim($_POST['city']);
                $pin = trim($_POST['pin']);

                /*
                validation process
                starts from here
                */

                // validate your email address
               if(strlen($mobile) == 10){

                  if($db->isMobileNumberExisted($mobile)) {
                         //user already existed
                         $response["error"] = true;
                         $response["error_msg"] = "user already existed with" . $mobile;
                         echo json_encode($response);
                   }else{  
                           // create a new user
                           $user = $db->storeUser($salutation, $fname, $lname, $dob, $mobile, $country, $state, $city, $pin);
                           if ($user) {
                               // user stored successfully
                               $response["error"] = false;
                               $_SESSION['fullname'] = $user['fullname'];
                               $_SESSION['vault_no'] = $user['vault_no'];
                                                                        $message = "Registration successful";
                               echo "<script type='text/javascript'>alert('$message');</script>";

                             } else {
                                // user failed to store
                                $response["error"] = true;
                                $response["error_msg"] = "Unknown error occurred in registration!";
                                echo json_encode($response);
                             }
                       }

                }else{
                        //invalid mobile number
                        $response["error"] = true;
                        $response["error_msg"] = "PLEASE ENTER VALID MOBILE NUMBER!";
                        echo json_encode($response);
                    }

        }else{
               //missing the required fields
                $response["error"] = true;
                $response["error_msg"] = "Please fill all the required parameters!";
                echo json_encode($response);
          }

?>  
header('location: formprofile.php');

This line is redirecting the page to formprofile.php and that causes to ignore everything below it. Just remove that line or if you want redirect after execution, then move it to the end of the file and do not echo or print anything before it.

But if you want to have some alert before redirecting to other page don't use header() for redirect. Add this JavaScript at the end of the file.

?>
<script>
    alert("your message");
    document.location = "formprofile.php";
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM