Ninad Bondre
Ninad Bondre

Reputation: 75

How to redirect to another page in php?

Here is the code for registration. Values are inserted properly but page is not redirected to another page:

 if(isset($_POST['submit'])){
    $company_name = $_POST['company_name'];//check whether form is submitted or not
    $email = filter_var($_POST['email'],FILTER_SANITIZE_EMAIL);//email validation
    $password = sha1($_POST['password']);
    $phone = $_POST['phone'];
    $city = $_POST['city'];
    $profession = $_POST['profession'];

    check validation of email
    if(!filter_var($email,FILTER_SANITIZE_EMAIL)){
        echo 'invalid email';
    }

    else

    {
        $result = mysql_query("SELECT * FROM registerpro WHERE email = '$email'");selecting email from database
        $data = mysql_num_rows($result);//check if there is result


        if($data==0){

            $qry = mysql_query("INSERT INTO registerpro (company_name,email,password,phone,city,profession) VALUES ('$company_name','$email','$password','$phone','$city','$profession')");

here i is the problem as page is not redirecting to another page so please tell me how to fix it

            if($qry){



                header("Location : company_info.php");//redirect to company_info

            }
            else`enter code here`
            {
                echo 'error';
            }
        }else{
            echo 'invalid email';
        }
    }
}
?>

After registration page is not redirecting to company_info.

Upvotes: 0

Views: 178

Answers (2)

eeknight
eeknight

Reputation: 11

I finally figured this out after struggling a bit. If you perform a web search on the PHP header() function you will find that it must be used at the very top of the file before any output is sent.

My first reaction was "well that doesn't help", but it does because when the submit button is clicked from the HTML input tag then the header() function will get run at the top.

To demonstrate this you can put a section of PHP code at the very top with the following line...

print_r($_POST);

When you then press the "Submit" button on your web page you will see the $_POST value change.

In my case I wanted a user to accept the Terms & Agreement before being redirected to another URL.

At the top of the file before the HTML tag I put the following code:

<?php
   $chkboxwarn = 0;
   /* Continue button was clicked */
   if(!empty($_POST['continue']) && $_POST['continue']=='Continue'){

      /* Agree button was checked */
      if(!empty($_POST['agree']) && $_POST['agree']=='yes'){
         header('Location: http://www.myurlhere.com');
      }
      /* Agree button wasn't checked */
      else{
     $chkboxwarn = 1;
      }

   }
?>

In the HTML body I put the following:

<form method="post">
   <input type="checkbox" name="agree" value="yes"  /> I understand and agree to the Terms above.<br/><br/>
   <input type="submit" name="continue" value="Continue"/>
</form>

<?php
   If($chkboxwarn == 1){
      echo '<br/><span style="color:red;">To continue you must accept the terms by selecting the box then the button.</span>';
   }
?>

Upvotes: 1

Pupil
Pupil

Reputation: 23948

Remove extra space after Location

So, change

header("Location : company_info.php");//redirect to company_info

To:

header("Location: company_info.php");//redirect to company_info
//              ^ here

Upvotes: 3

Related Questions