douzhuan4406 2016-01-22 05:07
浏览 85
已采纳

警告不会在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);
          }

?>  
  • 写回答

1条回答 默认 最新

  • dongyong6045 2016-01-22 05:12
    关注
    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上