我正在尝试使用可以从输入字段填充的二维数据创建数组。 我有二维数据的数组已经工作,请参阅下面的代码。
$kaarten = array
(
array("Linksys Cisco EA2700","129,99"),
array("Apple iPad 4","479,00"),
array("Linksys Cisco RE1000","54,99")
);
foreach($kaarten as $subArray)
{
echo $subArray[0]; // test
echo $subArray[1]; // 12.99
}
但现在数据是从php代码中的数据加载的,我想制作它以便你可以用输入字段填充它,例如:
您从2个输入字段开始(1.名称2.价格)。当你完成填充后,将有2个按钮1.发送或2.再添加1个,如果按下添加1个,则会出现2个输入字段,以便您可以在阵列中输入更多数据。
我希望有人可以帮我解决问题。
如果有人有更好的解决方案,请不要犹豫告诉我。
答案 0 :(得分:1)
<form action="" method="post">
<input type="hidden" name="action" id="action" value="process" />
Name: <input type="text" name="itemname" id="itemname" /><br />
Value: <input type="text" name="itemvalue" id="itemvalue" /><br />
<input type="submit" value="Add Item" />
</form>
<?php
$kaarten = array();
(
array("Linksys Cisco EA2700","129,99"),
array("Apple iPad 4","479,00"),
array("Linksys Cisco RE1000","54,99")
);
if ($_POST['action']=="process") {
//add item to array now we have to do this AFTER the array has been pre-populated and created
$itemname = $_POST['itemname'];
$itemvalue= $_POST['itemvalue'];
//add it to the actual array
$kaarten[] = array($itemname,$itemvalue);
//note there's a major issue here as you're not passing the array between form posts and so its being reset every post
//need to add in facility to pass items between form posts to allow for it to be an N length array otherwise it'll only ever container a max of 4 elements the 3 pre-defined ones and the one being added
}
foreach($kaarten as $subArray)
{
echo $subArray[0]; // test
echo $subArray[1]; // 12.99
}
?>
这是您可以看看在表单帖子之间分流数组的基础知识我建议将其存储在一个会话中,如果不存在则创建如果在最顶层或者json编码它并在页面之间发布它隐藏的领域