Notification not appearing in PHP

When working with a specific php file, I am encountering an issue where the alert box I have placed before the header is being ignored and the header is executed directly. Can anyone assist me in resolving this issue? Any help would be greatly appreciated! Thank you. .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);
          }

?>  

Answer №1

header('location: formprofile.php');

This snippet of code is responsible for redirecting the page to formprofile.php, causing everything below it to be ignored. To avoid this, simply delete that line. If you still want to redirect after executing some commands, consider moving the redirection to the end of the file without any output before it.

Alternatively, if you need to display an alert before redirecting, avoid using header(). Instead, include the following JavaScript at the end of your file:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilize Moment.js in AngularJS for formatting dates

I have been attempting to use moment.js in Angularjs to format a date, but it seems like I am running into some issues. Here is the link to my code snippet on jsfiddle http://jsfiddle.net/sed6x5e8/ and below you can find the HTML and JS code that I am work ...

Python event-driven long polling server (similar to the current PHP server)

I have a straightforward php script (currently running on xampp) for a long polling server, but I am interested in converting it to Python and creating a Python server instead. Despite my limited experience with Python, especially in the realm of web serve ...

Is there a way to generate a distinctive curved design using CSS for a

I am currently using CSS and div elements in an attempt to create these particular lines: https://i.stack.imgur.com/Ytowq.png .line { width: 1px; height: 100px; background-color: black; position: absolute; border-radius: 50%/100px 1 ...

Encountering the error message "Uncaught Error: [vuex] Getters must be functions, but 'getters.getters' is {}. This occurred while splitting the Vuex store into modules in Vue.js."

As a beginner in VUEX, I am experimenting with building a test application to dive deeper into the world of VUEX. I have organized my VUEX store into modules, where each module has its own getter.js file. Getters, actions, and mutations are imported into i ...

What is the proper method in PHP to insert a space between numeric values?

Seeking a simple solution for those in the know. The Current Situation Numbers without spaces. $str = 10000; $str2 = 100000; Desired Outcome Transform above into below... $str = '10 000'; $str2 = '100 000'; Personal Reflections ...

Pass the object either in JSON format or as a variable using the drag and drop feature

Here's a quick question: when using the Drag and Drop system, I'm not sure which method is better. Is it more efficient to : utilize setData and getData to transfer a JavaScript object? (Utilizing JSON conversion since setData only passes st ...

How to apply multiple filters in JavaScript?

I have a set of objects that require filtering. If the object has an active status of true, it should always be included in the filtered result regardless of other conditions. If there is text entered for search, then the remaining items should be filter ...

Transferring a variable value between functions using autocomplete and AJAX communication

I am facing difficulties with implementing autocomplete jQuery along with AJAX call. The issue arises when a user enters text in the input field, triggering an AJAX POST request to the controller which retrieves values from the database and sends them back ...

Manipulate line breaks in a base 64 encoded string using PHP

My base64 encoded string is extremely long and lacks spaces or line breaks, causing it to be displayed on a single line with an unsightly horizontal scroll bar in a textarea. Is there a way for me to manually insert carriage returns after using base64_enc ...

Saving information in node.js

My latest project involves creating an address book app using HTML, CSS, and JavaScript. The company provided me with a zip file containing the necessary resources to implement the app using node.js. However, my knowledge of node.js is limited and I have ...

When using a foreach loop in CodeIgniter, encountering the error "Attempting to access a property of a non

Within the controller: if ( $training_code == null || $batch_no == null) { $data = $this->_public_data(); $no_reg = $this->sys_model->get_registration($this->session->userdata('admin'))->no_reg; ...

Top technique for verifying the presence of duplicates within an array of objects

How can I efficiently check for duplicates in typescript within a large array of objects and return true or false based on the results? let testArray: { id: number, name: string }[] = [ { "id": 0, "name": "name1" }, ...

Paginating content without the need for a database

Seeking assistance on implementing pagination for displaying trading history API responses, without utilizing a database. Can anyone provide guidance and help with the necessary logic? Here is an excerpt of my code: <?php error_reporting(E_ALL) ...

Are the features of emulation mode in IE11 identical to implementing the meta EmulateIE7 tag?

When using the IE11 browser, I encountered an interesting message on the console: “HTML1122: Internet Explorer is running in Enterprise Mode emulating IE8.” Upon researching this error code on this link, I found out that it is a mode configured by IT ...

Having difficulty installing the yarn package from GitHub

I'm attempting to install a GitHub package using yarn. I've tried this process many times before, but I have not been successful with this particular repository: https://github.com/coolwanglu/pdf2htmlEX I have already attempted the following w ...

What is the proper way to connect my JavaScript code to my HTML form?

My JavaScript function is not working properly when I try to submit a form on my website. <form onsubmit="validateRegistration()"> <p> // Email registration <input type="text" id="e-mail" placeholder="Email" /> </p> ...

Does the PHP include function act as an HTTP request?

After coming across an article highlighting the negative impact of using excessive HTTP requests on server speed, I started wondering about the performance implications of PHP includes. Specifically, I'm curious if sending 4 AJAX requests compared to ...

"What's the best way to update the src attribute of an iFrame when a user clicks

I'm new to coding in PHP or JavaScript, so please bear with me if my question isn't quite right. I'm looking for a way to dynamically pass and embed a URL from a link into the src attribute of an iframe. Essentially, I want users to click o ...

What is the best scenario to implement ftp_pasv?

Is it necessary to set ftp_pasv to TRUE? Can this setting be enabled all the time or are there certain scenarios where it shouldn't be used? Additionally, is there a method to automatically determine if the server requires this setting? ...

Tips for modifying the width of the mat-header-cell in Angular

Is there a way to customize the mat-header-cell in Angular? I've been trying to change its width without success. Any suggestions would be greatly appreciated. <ng-container cdkColumnDef="name"> <mat-header-cell *cdkHeaderCellDe ...