フォームを2つ用意する。AとBのフォームとする。Aのフォームは、AだけPOSTする。BのフォームはAとBの2つをPOSTする
フォームAとフォームBを用意し、フォームAは独立してPOSTし、フォームBはAとBのデータを同時にPOSTする場合、以下のようにHTMLを書くことができます。
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Two Forms</title>
</head>
<body>
<!-- フォームA -->
<form method="post" action="{% url 'form_a_submit_url' %}">
{% csrf_token %}
<label for="form_a_input">Form A Input:</label>
<input type="text" id="form_a_input" name="form_a_input">
<button type="submit">Submit Form A</button>
</form>
<!-- フォームB -->
<form method="post" action="{% url 'form_b_submit_url' %}">
{% csrf_token %}
<!-- フォームAのデータ -->
<input type="hidden" name="form_a_input" value="{% if request.method == 'POST' %}{{ request.POST.form_a_input }}{% endif %}">
<!-- フォームB独自のデータ -->
<label for="form_b_input">Form B Input:</label>
<input type="text" id="form_b_input" name="form_b_input">
<button type="submit">Submit Form B</button>
</form>
</body>
</html>
この例では、フォームBがフォームAのデータ(form_a_input)を隠しフィールドとして持ち、フォームB独自のデータ(form_b_input)を入力フィールドとして持っています。それぞれのフォームは独立して送信され、必要な場合はフォームBでフォームAのデータを取得できます。

ディスカッション
コメント一覧
まだ、コメントがありません