serialize()
あなたが言ったように、ユーザー。ただし、機能する別のボタンを追加するには、JavaScript が必要です。フォームは次のとおりです。
<form action="action.php" method="post">
<div id="phoneNumbers">
<input type="text" value="home phone">: <input type="text" name="0-value" value="(xxx)xxx-xxxx">
</div>
<button onclick="addAnother();">Add Another Phone Number</button>
<input type="submit>
</form>
これがJavaScriptです(ページのheadタグに入れます):
<script type="text/javascript">
var nums=0;
function addAnother(){
document.getElementById('phoneNumbers').innerHTML+='<input type="text" name="'+++nums+'-name">: <input type="text" name="'+nums+'-value">';
}
</script>
action.php は次のとおりです。
<?php
$arrayOfNums=array();
foreach($_POST as $curVal){
array_push($arrayOfNums,$curVal);
}
$serializedArray=serialize($arrayOfNums);
#now do whatever code you have to add serializedArray to your database. It is a string, so this is easy.
?>
これで、データベースにシリアル化された配列ができました。それだけunserialize()
で、次のような配列が得られ、名前と値が交互に表示されます: 'home tel','(324)444-4356','work tel','(444)546-5678' など。これはテストされていません。失敗したら私。