1
0

More than 3 years have passed since last update.

formタグのactionとheader関数によるリダイレクトの優先度

Last updated at Posted at 2019-09-25

actionとheader関数

結論からいうとaction属性が優先される

action属性

formの中の属性。
指定したURLに送信できる。
<form action="http://www.example.com/">

header関数

header関数はHTTPヘッダを指定できる、リダイレクトに使用できる。
header('Location: http://www.example.com/);

お問い合わせフォーム

確認画面から初期画面にもどると完了画面へ移動する分岐の際リダイレクトが必要になる

失敗例

action属性が優先されるためphpの記述でsendとbackで場合分けしているがcomplete.phpに飛ぶ

comfirm.php
<?php
session_start();
    if(isset($_POST["send"])) {
        header("Location:complete.php");
    }
    elseif (isset($_POST["back"])){
        header("Location:input.php");
    }
    else{

    }
?>
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="utf-8">
    <title>お問い合わせ</title>
</head>
<body>
<form  action="complete.php" method="post">
    名前<br />
    <?php echo $_SESSION["name"];?><br />
    電話番号<br />
    <?php echo $_SESSION["telNumber"];?><br />
    メールアドレス<br />
    <?php echo $_SESSION["mail"];?><br />
    <input type="submit" name="send" value="確認">
    <input type="submit" name="back" value="戻る">
</form>
</body>
</html>

成功例

action属性をcomfirm.phpに変更
type属性がsubmitのinputタグが押された際phpの記述が通る。

comfirm.php
<?php
session_start();
    if(isset($_POST["send"])) {
        header("Location:complete.php");
    }
    elseif (isset($_POST["back"])){
        header("Location:input.php");
    }
    else{

    }
?>
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="utf-8">
    <title>お問い合わせ</title>
</head>
<body>
<form  action="comfirm.php" method="post">
    名前<br />
    <?php echo $_SESSION["name"];?><br />
    電話番号<br />
    <?php echo $_SESSION["telNumber"];?><br />
    メールアドレス<br />
    <?php echo $_SESSION["mail"];?><br />
    <input type="submit" name="send" value="確認">
    <input type="submit" name="back" value="戻る">
</form>
</body>
</html>
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0