I can't find the bug. I've written a php script that produces a form, filled with an array. The last line is a input field to add a new item to the array.
My problem is that the last entry of the array is overwritten when I add new one. Here's the code:
<?php
$array = array("a");
function printForm($array)
{
if(!isset($array)){
$array = array();
}
$out=" <form name='' action='".$_SERVER['PHP_SELF']."' method='POST'><table border='1'> <tr><th colspan='2'>Namen</th></tr>";
$out = $out. "<input type='hidden' name='posted' value='yes' />";
foreach ($array as $m) {
$out = $out . "<tr><td> </td>";
$out = $out . "<td>".$m["name"]."</td></tr>";
}
$out = $out. "<tr><td> <input type='Submit' value='add' /></td>";
$out = $out. "<td><input type='text' name='name' value='' /></td>";
$out = $out."</table></form>";
echo $out;
}
if( $_POST['name']!=""){
array_push($array, $_POST['name'] );
}
printForm($array);
?>
any ideas?
Regards Michaba
thanks to Erik that pointin me to the Session. here is the solution that works for me:
<?php
session_start();
if ( ! isset ( $_SESSION['myArray'] ) )
{
$_SESSION['myArray'] = array();
}
function printForm()
{
$out=" <form name='' action='".$_SERVER['PHP_SELF']."' method='POST'><table border='1'><tr><th colspan='2'>Namen</th></tr>";
$out .= "<input type='hidden' name='posted' value='yes' />";
foreach ($_SESSION['myArray'] as $m) {
$out .= "<tr><td> </td>";
$out .= "<td>".$m["name"]."</td></tr>";
}
$out .= "<tr><td> <input type='Submit' value='s' /></td>";
$out .= "<td><input type='text' name='name' value='' /></td>";
$out .= "</table></form>";
echo $out;
}
if( $_POST['name']!="")
{
array_push($_SESSION['myArray'], $_POST['name'] );
}
printForm();
?>
lateron i will store the array in the database.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.