Jump to content

Help with posting an array in a html form


scheibyem

Recommended Posts

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.

 

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];

$afsnitarray = unserialize(base64_decode($_POST[serial]));

 

print_r ($afsnitarray);

 

if ($_POST[deletepara] != "")

{

$removed = array_splice($afsnitarray,$_POST[deletepara],2);

}

 

$antal = count($afsnitarray);

 

if ($_POST[add_para] == "accept")

{

$antal = $antal+2;

}

 

while ( $counter2 <  ($antal/2))

{

 

$counter2++;

 

$overskriften = "overskrift".$counter2;

$teksten = "tekst".$counter2;

 

$main_block .= "

<tr>

        <td>Write a headline for $counter2</td>

        <td><INPUT TYPE=\"text\" NAME=\"$overskriften\" SIZE=\"43\" VALUE=\"$afsnitarray[$overskriften]\"></td>

<td><input type=\"hidden\" name=\"$counter2\" value=\"$counter2\"> </td>

    </tr>

<tr>

      <td> </td>

      <td><TEXTAREA name=\"$teksten\" rows=\"20\" cols=\"34\">".$afsnitarray[$teksten]."</TEXTAREA></td>

      <td><p align=\"left\"><INPUT TYPE=\"Checkbox\" NAME=\"deletepara\" VALUE=\"$counter2\">Slet dette afsnit?</p>

</td>

    </tr>

";

 

$aarray1[$overskriften] = $_POST[$overskriften];

$aarray1[$teksten] = $_POST[$teksten];

 

}

 

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=\"Udfør\"></center></TD>

      <TD width=\"40%\"> </TD>

  </TR>

 

</TABLE>

 

<input type=\"hidden\" name=\"serial\" value=\"$serial_afsnit\">

 

</form>

";

 

 

 

?>

<HTML>

  <HEAD>

  <TITLE>Annoncering</TITLE>

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">

  </HEAD>

  <BODY>

 

<?php echo "$main_block"; ?>

 

 

 

 

 

  </BODY>

  </HTML>

 

-------------------------------------------

 

The problem is that I want the values in the array to be put back into the while loop when it is posted again so that the values are not lost. If I put in headline 1 in $overskrift1, text 1 in $tekst1 and headline 2 in $overskrift2, text 2 in $tekst2 the arrays look like this:

 

afsnitarray:

Array ( [overskrift1] => [tekst1] => [overskrift2] => [tekst2] => )

 

aarray:

Array ( [overskrift1] => headline 1 [tekst1] => text 1 [overskrift2] => headline 2 [tekst2] => text 2 ) 

 

When I push the submit button again I get the values put into the right place and the arrays now look like this:

 

afsnitarray:

Array ( [overskrift1] => headline 1 [tekst1] => text 1 [overskrift2] => headline 2 [tekst2] => text 2 ) 

 

aarray:

Array ( [overskrift1] => [tekst1] => [overskrift2] => [tekst2] => )

 

I've been starring at the code for days now and I can't seem to figure out how to fix this. Getting desperat!!!

 

Can anybody help, please?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.