繁体   English   中英

警报不弹出在PHP

[英]alert not popping up in php

在特定的php文件中,iam在执行标头之前使用标头和警报框,但是忽略了警报,直接执行标头.. !!请帮助我解决这个问题... !!如果我出错了,请原谅。 .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');

此行将页面重定向到formprofile.php ,这将导致忽略其下面的所有内容。 只需删除该行,或者如果您想在执行后重定向,则将其移至文件末尾,请勿在其前回显或打印任何内容。

但是,如果您想在重定向到其他页面之前有一些警报,请不要使用header()进行重定向。 在文件末尾添加此JavaScript。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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