首页 > 代码库 > php页面跳转

php页面跳转

php中如何跳转,我们看下面的代码

form3.php

<html>
    <head></head>
    <title></title>
    <script type="text/css"></script>
        <form action="formprocess3.php" method="post">
            <body>
                <table><tr>
                    <td>Name</td>
                    <td><input type="text" name="name" /></td>
                </tr>
                <tr>
                    <td>Movie type</td>
                    <td><select name="movie_type" id="movie_type">
                        <option value="">Select a movie type...</option>
                        <option value="Action">Action</option>
                        <option value="Drama">Drama</option>
                        <option value="Comedy">Comedy</option>
                        <option value="Sci-Fi">Sci-Fi</option>
                        <option value="War">War</option>
                        <option value="Other">Other</option></select></td>
                </tr>
                <tr>
                    <td>Item type</td>
                    <td>
                    <input type="radio" name="type" value="movie" checked="checked" />Movie<br />
                    <input type="radio" name="type" value="actor"/>Acotr<br />
                    <input type="radio" name="type" value="director"/>Director<br />
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="checkbox" name="debug" id="debug" checked="checked" />
                    Display Debug Info
                    </td>
                </tr>
                <tr>
                    <td colspan="2" style="text-aligh:center">
                    <input type="submit" name="submit" value="Search" />
                    <input type="submit" name="submit" value="Add" />
                    </td>
                    <td></td>
                </tr></table>
            </body>
        </form>
</html>

formprocess3.php

<?php
if($_POST["type"] == ‘movie‘ && $_POST["movie_type"] == ‘‘)
{
    header(‘Location:form3.php‘);
}
?>
<html>
    <head>
        <title><?php echo $_POST[‘submit‘].‘ ‘.$_POST[‘type‘].‘ ‘.$_POST[‘name‘];?></title>
    </head>
    <body>
    <?php
    if(isset($_POST[‘debug‘])){
        echo‘<pre>‘;
        print_r($_POST);
        echo‘</pre>‘;
    }
    else{
        echo ‘post‘;
    }
    $name = ucfirst($_POST[‘name‘]);
    if($_POST[‘type‘] == ‘movie‘){
        $foo=$_POST[‘movie_type‘].‘ ‘.$_POST[‘type‘];
    }
    else{
        $foo = $_POST[‘type‘];
    }
    
    echo ‘<p>You are ‘. $_POST[‘submit‘] .‘int‘;
    echo ($_POST[‘submit‘] == ‘Search‘)? ‘for‘:‘‘;
    echo ‘a ‘. $foo .‘ named ‘. $name .‘</p>‘;
    ?>
    </body>
</html>

 

注意这一句

if($_POST["type"] == ‘movie‘ && $_POST["movie_type"] == ‘‘)
{
header(‘Location:form3.php‘);
}

如果选择movie类型并且传入的movie_type为空则返回到form3.php页面中header()方法参数是以分号分割的字符串前面是Location,后面是要跳转的页面地址。

head()函数是输出一个原始的http头,php请求页面的时候会首先自动输出http头,并且值输出一次,所以这个方法必须在html文件的开头使用,前面不能有echo,print之类的输出语句,也不能有html代码和空格之类的,否则有可能调用失败。

 还有一点这里有两个submit按钮但是value的值不一样,我们可以根据这个值来进行不同的逻辑处理。