0

送信ボタンを押すと、必要に応じてデータを処理するフォームがあります。私が欲しいのは、別のページ、または前のページに移動することです。私のページとは、最初はリンクのリストです。リンクをクリックすると、別のページが開き、回答する質問が表示されます。回答した後、選択ページに戻る必要があります。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

4 に答える 4

1

現在のページでデータを処理したと述べたので、あるページから別のページにデータを渡す必要はありません。したがって、データの処理が終了するとすぐに、別のページに移動できます。したがって、データを処理したコードのすぐ下に適切なヘッダーを追加するだけです。

header('Location:otherpage.php');

新しく投稿されたコードで、header() 呼び出しの上に HTML があることに気付きました。これは、開発者が不要なコードを非効率的に処理することを防ぐために許可されていません。(別名、データが表示される前に即座にリダイレクトされる場合、データを表示する理由)

次のように、ページの上部に処理コードと 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>
于 2013-06-10T17:46:00.950 に答える
-1

データの処理が完了したら、http_redirect();を発行できます。これは と同じことを行いheader('Location:...')ますが、少し使いやすいかもしれません。ドキュメントを参照してください: http://php.net/manual/en/function.http-redirect.php

于 2013-06-10T17:46:59.733 に答える
-1

アクション属性を使用する

<form method="post" action="other-page.php"> 
...
于 2013-06-10T17:45:43.630 に答える