contact[id][type]
やのような多次元配列を実際に作成できますcontact[id][address]
。したがって、id
新しい値を追加する場合は増分値になる可能性がある がある場合は、id
両方に同じ値を指定してください。PHP は、 のような多次元配列として認識し$_POST['contact'][1]['type'] == 'sometype, $_POST['contact'][1]['address'] == 'theaddress'
ます。
したがって、新しいフォーム入力は次のようになります。
<input id='someid_type_123' name='contact[123][type]' />
<input id='someid_address_123' name='contact[123][address]' />
<input id='someid_type_124' name='contact[124][type]' />
<input id='someid_address_124' name='contact[124][address]' />
その後、ループすることができます$_POST['contact']
:
foreach ($_POST['contact'] as $id => $values) {
echo $values['type'] . ' ' . $values['address'];
}
サンプル入力を使用すると、次のvar_dump($_POST)
ようになります。
array(1) {
["contact"]=>
array(2) {
[123]=>
array(2) {
["type"]=>
string(5) "type1"
["address"]=>
string(5) "addr1"
}
[124]=>
array(2) {
["type"]=>
string(5) "type2"
["address"]=>
string(5) "addr2"
}
}
}
JavaScript で ID を生成するために使用する方法 (事前に ID がわからない場合) は、完全にあなた次第です。で変数を開始し、リンク1
をクリックするたびに増分することができます。add more
の値はid
、入力のペアで同じである限り、実際には問題ではありません。
// Initialize curId on page load.
var curId = 1;
addMoreLink.onclick = function() {
// append the new inputs using curId
// and then increment the value
curId++;
}