0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

フォームについて

Posted at

フォームとは

  • ユーザーがウェブページからデータをサーバに送信するためのもの

form要素

forn要素
<form action="#" method="POST">
</form>
  • action属性・・・どこにデータを送信するのかを記述する
  • method属性・・・HTTPリクエストメソッドの種類を記述

HTTPメソッドの種類

  • GET・・・サーバからデータを取得する場合に使用

  • POST・・・サーバへデータを送信する場合に使用

input要素

input要素
<input type="text">

単一行のテキスト入力欄の作成

スクリーンショット 2022-10-15 3.32.16.png

テキスト入力欄の作成
<dt>お名前</dt>
<dd><input type="text" name="username"></dd>

<dt>メールアドレス</dt>
<dd><input type="text" name="email"></dd>

プルダウンメニューの作成

スクリーンショット 2022-10-15 3.34.02.png

プルダウン
<select name="area">
  <option value="" selected>選択してください</option>
  <option value="hokkaido">北海道</option>
  <option value="okinawa">沖縄</option>
</select>
  • selectedを指定することで、選択肢が初期状態で選択される
  • select要素内で1つのオプションだけがselected属性を持てる

[MDN:select]https://developer.mozilla.org/ja/docs/Web/HTML/Element/select
[MDN:option]https://developer.mozilla.org/ja/docs/Web/HTML/Element/option

ラジオボタン

スクリーンショット 2022-10-15 3.59.21.png

ラジオボタン
<dt>性別</dt>
<dd>
<input type="radio" name="gender" value="male" checked>男性
<input type="radio" name="gender" value="female">女性
</dd>
  • name属性には同じ名前をつける
  • checkedをつけると初期状態で選択されるようになる

[MDN:input]https://developer.mozilla.org/ja/docs/Web/HTML/Element/input

テキストエリア

スクリーンショット 2022-10-15 4.00.46.png

テキストエリア
<dt>お問い合わせ内容</dt>
<dd><textarea name="contents"></textarea></dd>

[MDN:textarea]https://developer.mozilla.org/ja/docs/Web/HTML/Element/textarea

チェックボックス

スクリーンショット 2022-10-15 4.00.52.png

チェックボックス
<dt>プライバシポリシーに同意する</dt>
<dd><input type="checkbox" name="privacy" value="1"></dd>
  • 初期状態でチェックを付けたい場合はcheckedをつける

[MDN:checkbox]https://developer.mozilla.org/ja/docs/Web/HTML/Element/input/checkbox

送信ボタン

スクリーンショット 2022-10-15 4.01.59.png

送信ボタン
<input type="submit" name="submit" value="送信">
  • form属性のアクション属性に指定したパスにデータが送られる

ラベル

  • ラベルをクリックしても選択が可能になる
  • ラベルを付けたい要素にidをつけることでラベルを付与できる
ラベル
<dt><label for="username">お名前</label></dt>
<dd><input id="username" type="text" name="username"></dd>
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?