user1783675
user1783675

Reputation: 356

Redirect to the next page after clicking on submit button php mysql

I would like to redirect to the next page after clicking on the submit page, but I get the following error:

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\forms\P_details.php:80) in C:\xampp\htdocs\P_details.php on line 70

the following is the php code on P_details.php

//connect to connect to the database and initialize all functions
include 'scripts/functions/init.php';
include 'html/head.php';
include 'html/page_title.php';
include 'html/top_menu.php';
include 'titles/P_details.php';

//Generate a random Personid
$Personid = rand(1,9999999);
$_SESSION['Personid'] = $Personid;
//Carry over the Jobid to this page
$Jobid = $_SESSION['SESS_MEMBER_JOB'];

if(empty($_POST)=== false)
    {
        $R_fields = array('Title','Surname','Forename','IdentityNumber','Gender','Race','Nationality');
        foreach($_POST as $key=>$value)
        {
            if (empty($value) && in_array($key,$R_fields)=== true)
                {
                    $errors[] = 'fields marked with (*) are required';
                    break 1;
                }
        }

        if(empty($errors)=== true)
            {

                if($_POST['title'] == '-Select-')
                    {
                        $errors[] ='Please select Title';
                    }
                if($_POST['Gender'] == '-Select-')
                    {
                        $errors[] ='Please select Gender';
                    }
                if($_POST['Race'] == '-Select-')
                    {
                        $errors[] ='Please select Race';
                    }
                if($_POST['Nationality'] == '-Select-')
                    {
                        $errors[] ='Please select Nationality';
                    }

            }

    }

    include 'forms/P_details.php';

    if(empty($_POST) === false && empty($errors)=== true)
        {
            //submit personal details
            $personal_details = array(
            'Personid'=>$Personid,
            'Title' => $_POST['title'],
            'Surname'=>$_POST['Surname'],
            'Forename' => $_POST['Forename'],
            'IdentityNumber'=>$_POST['IdentityNumber'],                                             
            'Gender'=>$_POST['Gender'],
            'Race'=>$_POST['Race'],
            'Nationality'=>$_POST['Nationality'],
            'WorkPermitNumber'=>$_POST['WorkPermitNumber'],
            'JobID'=>$Jobid);                       
            personal_details($personal_details);                        
            //redirect
            header('Location:Address_insert.php');
            exit();                                                                                                                             
        }
    else if(empty($errors) === false)
        {
            //output errors if the errors array is not empty
            echo output($errors);
        }

?>

Please assist

Upvotes: 0

Views: 1738

Answers (4)

Fountain
Fountain

Reputation: 16

header('Location:Address_insert.php'); before any output.

Possible your included files contains any output?

include 'html/head.php';
include 'html/page_title.php';
include 'html/top_menu.php';
include 'titles/P_details.php';

Or

include 'forms/P_details.php';

Any output possible to insert only after header('Location:Address_insert.php');

Upvotes: 0

user1783675
user1783675

Reputation: 356

I moved the line include 'forms/P_details.php' below the header statement

<?php

//connect to connect to the database and initialize all functions
include 'scripts/functions/init.php';
include 'html/head.php';
include 'html/page_title.php';
include 'html/top_menu.php';
include 'titles/P_details.php';

//Generate a random Personid
$Personid = rand(1,9999999);
$_SESSION['Personid'] = $Personid;
//Carry over the Jobid to this page
$Jobid = $_SESSION['SESS_MEMBER_JOB'];

if(empty($_POST)=== false)
    {
        $R_fields = array('Title','Surname','Forename','IdentityNumber','Gender','Race','Nationality');
        foreach($_POST as $key=>$value)
        {
            if (empty($value) && in_array($key,$R_fields)=== true)
                {
                    $errors[] = 'fields marked with (*) are required';
                    break 1;
                }
        }

        if(empty($errors)=== true)
            {

                if($_POST['title'] == '-Select-')
                    {
                        $errors[] ='Please select Title';
                    }
                if($_POST['Gender'] == '-Select-')
                    {
                        $errors[] ='Please select Gender';
                    }
                if($_POST['Race'] == '-Select-')
                    {
                        $errors[] ='Please select Race';
                    }
                if($_POST['Nationality'] == '-Select-')
                    {
                        $errors[] ='Please select Nationality';
                    }

            }

    }



    if(empty($_POST) === false && empty($errors)=== true)
        {
            //submit personal details
            $personal_details = array(
            'Personid'=>$Personid,
            'Title' => $_POST['title'],
            'Surname'=>$_POST['Surname'],
            'Forename' => $_POST['Forename'],
            'IdentityNumber'=>$_POST['IdentityNumber'],                                             
            'Gender'=>$_POST['Gender'],
            'Race'=>$_POST['Race'],
            'Nationality'=>$_POST['Nationality'],
            'WorkPermitNumber'=>$_POST['WorkPermitNumber'],
            'JobID'=>$Jobid);                       
            personal_details($personal_details);                        
            //redirect
            header('Location: Address_insert.php');
            exit();                                                                                                                             
        }
    else if(empty($errors) === false)
        {
            //output errors if the errors array is not empty
            echo output($errors);
        }

        include 'forms/P_details.php';

?>

Upvotes: 0

v0d1ch
v0d1ch

Reputation: 2748

Either call ob_start() at the top of page and ob_end_flush() at the bottom or don't echo anything on the page before your header()

Upvotes: 1

Ivo Pereira
Ivo Pereira

Reputation: 3500

That error appears when you've echoed any output to the browser before heading to another page.

Check your includes and be sure that you are not doing an echo anywhere.

Upvotes: 0

Related Questions