如何重定向到PHP中的另一个页面?

时间:2014-12-12 06:19:29

标签: php mysql

以下是注册代码。值已正确插入,但页面未重定向到另一页:

 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')");

这里我是问题因为页面没有重定向到另一个页面所以请告诉我如何解决它

            if($qry){



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

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

注册页面未重定向到company_info

2 个答案:

答案 0 :(得分:3)

Location

之后删除多余的空格

所以,改变

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

要:

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

答案 1 :(得分:1)

我终于在挣扎了一下后想出来了。如果您在PHP header()函数上执行Web搜索,您会发现在发送任何输出之前必须在文件的最顶部使用它。

我的第一反应是"好吧没有帮助"但它确实是因为当从HTML输入标签点击提交按钮时,header()函数将在顶部运行

为了证明这一点,您可以将一部分PHP代码放在最顶层,并使用以下行...

print_r($_POST);

然后按"提交"在您的网页上,您将看到$ _POST值的变化。

在我的情况下,我希望用户接受条款&在重定向到另一个URL之前达成协议。

在HTML标记之前的文件顶部,我输入以下代码:

<?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;
      }

   }
?>

在HTML正文中,我提出以下内容:

<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>';
   }
?>