完成处理表单后转到另一页

时间:2013-06-10 17:41:40

标签: php html redirect

我有一个表单,当我按下提交按钮时,我按照自己的意愿处理数据。我想要的是转到另一页或上一页。我的页面是什么,首先是链接列表,当我按下链接时,另一个页面打开并回答问题,在我回答之后,我需要回到选择页面。我尝试了header(),因为这是大多数人所说的,但我保持在同一页面上,我尝试使用<meta http-equiv="refresh" content="0" url="the page to go to">,但这似乎一遍又一遍刷新同一页面。当我按下提交并处理问题时,如何回到选择页面。

由于

注意:如果需要代码,请告诉我,以便发布它们。

编辑:

以下是我尝试使用header()http_redirect()

的代码
<?php

// topic 1

try {

    $db = new PDO("mysql:dbname=mawarid;host=localhost", "khaled", "");
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $q_qry = "select * from question where question_topic='1';";
    $questions = $db->query($q_qry)->fetchAll();

} catch (PDOException $ex) {
    // code here for handling error
    print "error";
    print "\n".$ex->getMessage()."\n";
}

?>

<!doctype html>

<html>
    <html>
        <meta charset="UTF-8">
        <title>topic 1</title>
    </html>

    <body>

        <h1>topic 1:</h1>

        <form method="post" action="">
            <ol>
<?php
                foreach ($questions as $question) {

                    $qa_qry = "select * from answer where question='$question[0]';";
                    $ans = $db->query($qa_qry)->fetchAll();             
?>

                    <li><?= $question[1] ?></li>

<?php
                    foreach ($ans as $a) {
                        $radio_name = "q".$question[0];
?>
                        <label><input type="radio" name="<?= $radio_name ?>" value="<?= $a[0] ?>"><?= $a[1] ?></label><br/>
<?php
                    }
                }
?>
            </ol>

            <input type="submit" value="submit" name="submit">
        </form>

    </body>
</html>

<?php 

session_start();

if(isset($_POST["submit"])) {
    $user_ans = array();
    for($i = 1; $i <= 10; $i++) {
        $ans_access = "q".$i;
        $user_ans[$i] = $_POST[$ans_access];
    }

//  $correct = $_SESSION["contestant_name"]["correct"];
//  $wrong = $_SESSION["contestant_name"]["wrong"];

    $correct = 0;
    $wrong = 0;

    foreach ($questions as $question) {
        if ($question[2] == $user_ans[$question[0]])
            $correct++;
        else
            $wrong++;
    }

    $_SESSION["contestant_name"]["topics_score_correct"][0] = $correct;
    $_SESSION["contestant_name"]["topics_score_wrong"][0] = $wrong;

    $_SESSION["contestant_name"]["topics_done"][0] = TRUE;
    $_SESSION["contestant_name"]["correct"] += $correct;
    $_SESSION["contestant_name"]["wrong"] += $wrong;

    $_SESSION["contestant_name"]["last_topic_id"] = 1;

    // header("Location:choosetopic.php");
    http_redirect("choosetopic.php");
}
?>

4 个答案:

答案 0 :(得分:1)

由于您声明已处理当前页面上的数据,因此无需将数据从一个页面传递到另一个页面。因此,只要您完成数据处理,您就可以移动到另一个页面。因此,只需在处理数据的代码下添加适当的标题。

header('Location:otherpage.php');

使用新发布的代码,我发现你的header()调用上方有HTML。这是不允许的,以防止开发人员无效地处理不必要的代码。 (AKA为什么显示数据,如果你要在你看到它之前立即重定向)

尝试将处理代码和header()调用添加到页面顶部,如下所示:

<?php

// topic 1

try {

    $db = new PDO("mysql:dbname=mawarid;host=localhost", "khaled", "");
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $q_qry = "select * from question where question_topic='1';";
    $questions = $db->query($q_qry)->fetchAll();

} catch (PDOException $ex) {
    // code here for handling error
    print "error";
    print "\n".$ex->getMessage()."\n";
}



session_start();

if(isset($_POST["submit"])) {
    $user_ans = array();
    for($i = 1; $i <= 10; $i++) {
        $ans_access = "q".$i;
        $user_ans[$i] = $_POST[$ans_access];
    }

//  $correct = $_SESSION["contestant_name"]["correct"];
//  $wrong = $_SESSION["contestant_name"]["wrong"];

    $correct = 0;
    $wrong = 0;

    foreach ($questions as $question) {
        if ($question[2] == $user_ans[$question[0]])
            $correct++;
        else
            $wrong++;
    }

    $_SESSION["contestant_name"]["topics_score_correct"][0] = $correct;
    $_SESSION["contestant_name"]["topics_score_wrong"][0] = $wrong;

    $_SESSION["contestant_name"]["topics_done"][0] = TRUE;
    $_SESSION["contestant_name"]["correct"] += $correct;
    $_SESSION["contestant_name"]["wrong"] += $wrong;

    $_SESSION["contestant_name"]["last_topic_id"] = 1;

    // header("Location:choosetopic.php");
    http_redirect("choosetopic.php");
}


?>

<!doctype html>

<html>
    <html>
        <meta charset="UTF-8">
        <title>topic 1</title>
    </html>

    <body>

        <h1>topic 1:</h1>

        <form method="post" action="">
            <ol>
<?php
                foreach ($questions as $question) {

                    $qa_qry = "select * from answer where question='$question[0]';";
                    $ans = $db->query($qa_qry)->fetchAll();             
?>

                    <li><?= $question[1] ?></li>

<?php
                    foreach ($ans as $a) {
                        $radio_name = "q".$question[0];
?>
                        <label><input type="radio" name="<?= $radio_name ?>" value="<?= $a[0] ?>"><?= $a[1] ?></label><br/>
<?php
                    }
                }
?>
            </ol>

            <input type="submit" value="submit" name="submit">
        </form>

    </body>
</html>

答案 1 :(得分:1)

header("Location: http:your link")

应该做的伎俩。但是,确保在该行之前页面中没有回声。

答案 2 :(得分:-1)

使用动作属性

<form method="post" action="other-page.php"> 
...

答案 3 :(得分:-1)

完成数据处理后,您可以发出http_redirect();这与header('Location:...')完全相同,但可能更容易使用。请参阅文档:http://php.net/manual/en/function.http-redirect.php