用户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(把你的网页的头标记):
<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()
它,你有这样一个数组,在名称和值之间交替:'家庭电话','(324)444-4356','工作电话','(444)546-5678'等。这是未经测试的,告诉我它是否失败。
任何关于如何在PHP中做到这一点的建议,如果除了手机之外,该页面上有更多的东西。那么我怎么能设置控制器上if($ this-> request->是('post')来做数组序列化只有当手机值保存? – 2013-03-13 19:03:51