ryoakg
8/14/2016 - 12:13 PM

form-variables.php

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>FORM</title>
    <style>
     .input-group, .output-group {
       border-radius:10px;
       background-color:#bbd;
       margin: 20px;
       padding: 10px;
     }
     .content {
       border-radius:5px;
       background-color:#ddf;
       margin: 10px;
       padding: 10px;
       font-size: 16px;
     }
    </style>
  </head>
  <?php if($_SERVER['REQUEST_METHOD'] === 'GET'): ?>
    <body>
      <h1>どういう値がformで送信されるか?</h1>
      <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">
        <div class="input-group">
          <h2>type=text :: 空文字列でも常に送信される</h2>
          <div class="content">
            <label for="name">text</label>
            <input type="text" name="name" id="name" />
          </div>
        </div>

        <div class="input-group">
          <h2>type=checkbox :: チェックした場合だけ送信される</h2>
          <div class="content">
            <label for="check">checkbox</label>
            <input type="checkbox" name="check" id="ckeck" value="a">
          </div>
        </div>

        <div class="input-group">
          <h2>type=radio :: チェックした場合だけ送信される</h2>
          <div class="content">
            <label for="radio-a">a</label>
            <input type="radio" name="radio" id="radio-a" value="a"><br>
            <label for="radio-b">b</label>
            <input type="radio" name="radio" id="radio-b" value="b"><br>
            <label for="radio-c">c</label>
            <input type="radio" name="radio" id="radio-c" value="c"><br>
          </div>
        </div>

        <input type="submit" value="送信" />
      </form>
    </body>
  <?php else: ?>
    <body>
      <h1>送信されたデータ</h1>
      <div class="output-group">
        <h2>HTTP REQUEST BODY</h2>
        <pre class="content"><?php echo file_get_contents('php://input'); ?></pre>
      </div>
      <div class="output-group">
        <h2>$_POST</h2>
        <div class="content">
          <?php var_dump($_POST); ?>
        </div>
      </div>
      <a href="<?php echo $_SERVER['REQUEST_URI']; ?>">戻る</a>
    </body>
  <?php endif; ?>
</html>