Hi
I'm pretty new at php and I've come across this problem that I've been trying to solve for days.
The idea of the page is that people can submit a html form containing some information. However an input=text and a textarea can be added to the form if the user checks a checkbox and submits the form via action=\"$_SERVER[PHP_SELF].
If the user wishes he should also be able to remove the input and textarea he just added.
I have created a while loop that adds values to an array and serialized the array so I can put it in a input=hidden in the form.
I have managed to be able to add extra text areas and delete them again.
However I can't get the values in the array to be returned properly.
Below is the code that I've made:
<?php
$main_block = "
<form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
<TABLE BORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"3\" WIDTH=\"100%\">
";
$counter2 = 0;
$postedserial = $_POST[serial];
$sections = unserialize(base64_decode($_POST[serial]));
echo "sections<br>";
print_r ($sections);
if ($_POST[deletepara] != "")
{
$removed = array_splice($sections,$_POST[deletepara],2);
}
$antal = count($sections);
if ($_POST[add_para] == "accept")
{
$antal = $antal+2;
}
while ( $counter2 < ($antal/2))
{
$counter2++;
$headline = "headline".$counter2;
$text = "text".$counter2;
$main_block .= "
<tr>
<td>Write a headline for $counter2</td>
<td><INPUT TYPE=\"text\" NAME=\"$headline\" SIZE=\"43\" VALUE=\"$sections[$headline]\"></td>
<td><input type=\"hidden\" name=\"$counter2\" value=\"$counter2\"> </td>
</tr>
<tr>
<td> </td>
<td><TEXTAREA name=\"$text\" rows=\"20\" cols=\"34\">".$sections[$text]."</TEXTAREA></td>
<td><p align=\"left\"><INPUT TYPE=\"Checkbox\" NAME=\"deletepara\" VALUE=\"$counter2\">Check to delete this paragraph?</p>
</td>
</tr>
";
$aarray1[$headline] = $_POST[$headline];
$aarray1[$text] = $_POST[$text];
}
echo "<br><br>aarray<br>";
print_r ($aarray1);
$serial_afsnit = base64_encode(serialize($aarray1));
$main_block .= "
<TR>
<TD width=\"30%\"><p align=\"left\"><INPUT TYPE=\"Checkbox\" NAME=\"add_para\" VALUE=\"accept\">Check here if you want a new paragraph and then push submit.</p> </TD>
<TD width=\"30%\"><center><input type=\"submit\" name=\"sub\" value=\"submit\"></center></TD>
<TD width=\"40%\"> </TD>
</TR>
</TABLE>
<input type=\"hidden\" name=\"serial\" value=\"$serial_afsnit\">
</form>
";
?>
<HTML>
<HEAD>
<TITLE>php problem</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
</HEAD>
<BODY>
<?php echo "$main_block"; ?>
</BODY>
</HTML>
You can see it in function at with the "input" array named aarray1 and the "output" array named sections:
http://forsoegsperson.dk/test2.php
As you can see the values are added to the aarray like this:
aarray
Array ( [headline1] => headline 1 [text1] => )
However sections look like this:
sections
Array ( [headline1] => [text1] => )
But if I submit again it now looks like this:
sections
Array ( [headline1] => headline 1 [text1] => )
aarray
Array ( [headline1] => [text1] => )
And the data can be seen in the form. So you have to push the submit button twice to get the data back. Naturally I would very much like to just get the data if you push it once.
Can anybody help me please??