Jump to content

[SOLVED] Dynamic form fields/arrays - can I have some1 look at this please?


Bisa

Recommended Posts

First of all, I suck at arrays, I never really understood how they work, that said here is my problem.

 

I have been working all day on this form where the user can add more fields if they need to by submitting the form and thus getting back to the same form but with one extra field, I've got some static fields and I've managed to transmit the values of those fields so the only thing the user sees is the same form with his/her values and a new field to add something new into.

 

Now this is the problem, while I am able to transmit the static fields values I cant get my head around how I would do with the dynamic fields as the amount of fields (and thus the $_POST['dynamicfield'] variables) are not the same from one user to the other.

 

This is how my code looks like.

 


//My attempt to retrieve and put the current dynamic values to use.
for ($t=0; $t<$i; $t++)
{
$y = "artnr" . $t;
$x = "antal" . $t;

$artnr[$t] = secureFormInput($_POST['$y']);
$antal[$t] = secureFormInput($_POST['$x']);
}

/*This works to the extent that I am able to add the "old" fields and a new one but I am not able to get the old
values to transmit from the old form. (I suspect it has to do with the array but as I said I suck at arrays).*/

for($t=0; $t<$i; $t++)
{
echo "<tr><td><label>";
echo "<input name=\"artnr" . $t . "\" type=\"text\" value=\""; $artnr[$t]; echo "\" size=\"15\" maxlength=\"3\" />";
echo "</label></td>";
echo "<td><label>";
echo "<input name=\"antal" . $t . "\" type=\"text\" value=\""; $antal[$t]; echo "\" size=\"6\" />";
echo "</label></td>";
echo "<td><label>";
echo "<input name=\"artnr"; $t++; echo $t . "\" type=\"text\" value=\""; $artnr[$t]; echo "\" size=\"15\" maxlength=\"3\" />";
echo "</label></td>";
echo "<td><label>";
echo "<input name=\"antal" . $t; echo "\" type=\"text\"value=\""; $artnr[$t]; $t--; echo "\"  size=\"6\" />";
echo "</label></td><tr>";
}

Link to comment
Share on other sites

sure thing, I'll include the lot:

 

<?php

function secureFormInput($data) //Säkerhetsfunktion för att rensa bort skadlig input
{
$data = ereg_replace("[\'\")(;|`,<>]", "", $data);
stripslashes($data);
return $data;
}

$i = $_POST['i']; //Antalet artiklar som lagt till + 1
$i++;

$addfields = $_POST['addfields'];

if(strlen($addfields) != "0") //Om "Lägg till fler rader"-knappen har använts:
$submit = 0;
else
{
if ($i == 1)
	$submit = 0;
else
{
	$submit = 1;
	echo "Vänligen granska din beställning och klicka sedan pä \"Skicka\" en gäng till. <br /><br />";
}
}

//Tilldelar variabler värden från $_POST och säkrar input fån skadlig kod
$namn = secureFormInput($_POST['namn']);

$tele = secureFormInput($_POST['tele']);
$teldag = $_POST['teldag'];
$telkvall = $_POST['telkvall'];

$mobil = secureFormInput($_POST['mobil']);
$mobdag = $_POST['mobdag'];
$mobkvall = $_POST['mobkvall'];

$adress = secureFormInput($_POST['adress']);
$postnr = secureFormInput($_POST['postnr']);
$stad = secureFormInput($_POST['stad']);

$meddelande = secureFormInput($_POST['meddelande']);

for ($t=0; $t<$i; $t++)
{
$y = "artnr" . $t;
$x = "antal" . $t;

$artnr[$t] = secureFormInput($_POST['$y']);
$antal[$t] = secureFormInput($_POST['$x']);

//A temporary echo while dev. to see the variable values.
echo $artnr[$t] . $t . "<br />";
echo $antal[$t] . $t . "<br /><br />";

}

?>

<form class="bform" method="post" action="index.php?do=2">

<?php

//För antalet fält vidare via FORM
echo "<input name=\"i\" type=\"hidden\" value=\"" . $i . "\" />";

?>

  <table width="400" border="0" cellspacing="0">

    <tr>
      <td>Artikelnummer</td>
      <td>Antal</td>
      <td>Artikelnummer</td>
      <td>Antal</td>
    </tr>
          
<?php

//Skapar gammla fält och lägger till ett nytt
for($t=0; $t<$i; $t++)
{
echo "<tr><td><label>";
echo "<input name=\"artnr" . $t . "\" type=\"text\" value=\""; $artnr[$t]; echo "\" size=\"15\" maxlength=\"3\" />";
echo "</label></td>";
echo "<td><label>";
echo "<input name=\"antal" . $t . "\" type=\"text\" value=\""; $antal[$t]; echo "\" size=\"6\" />";
echo "</label></td>";
echo "<td><label>";
echo "<input name=\"artnr"; $t++; echo $t . "\" type=\"text\" value=\""; $artnr[$t]; echo "\" size=\"15\" maxlength=\"3\" />";
echo "</label></td>";
echo "<td><label>";
echo "<input name=\"antal" . $t; echo "\" type=\"text\"value=\""; $artnr[$t]; $t--; echo "\"  size=\"6\" />";
echo "</label></td><tr>";
}

/*
<tr>
<td><label>
        <input name="artnr1" type="text" value="artnr1" size="15" maxlength="3" />
      </label></td>
      <td><label>
        <input name="antal1" type="text" value="antal1" size="6" />
      </label></td>
      <td><label>
        <input name="artnr2" type="text" value="artnr2" size="15" maxlength="3" />
      </label></td>
      <td><label>
        <input name="antal2" type="text"  value="antal2" size="6" />
      </label></td>
</tr>
*/

?>
      

    <tr>
      <td colspan="4"><input type="submit" name="addfields" value="Lägg till fler artiklar" id="addfields" /></td>
    </tr>
  </table>  
  <br />
  <table width="400" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td colspan="2">Vid beställning kommer vi att kontakta er f&oumlr en bekräftelse per telefon!<br>
          <br>
Vänligen uppge namn och minst ett telefonnummer där nä er dag- eller kvällstid.<br></td>
      </tr>
      <tr>
        <td colspan="2"> </td>
      </tr>
      <tr>
        <td width="60">Namn:</td>
        <td width="340"><label>
          <input name="namn" type="text" value="<?php echo $namn; ?>" size="30">
        </label></td>
      </tr>
      <tr>
        <td>Tele:</td>
        <td width="340"><label>
          <input name="tele" type="text" value="<?php echo $tele; ?>" size="15" maxlength="9">
        Dag: 
        <input name="teldag" type="checkbox" <?php if ($teldag == "on") echo "checked=\"checked\""; ?>>
        Kväll: 
        <input type="checkbox" name="telkvall" <?php if ($telkvall == "on") echo "checked=\"checked\""; ?>>
        </label></td>
      </tr>
      <tr>
        <td>Mobil:</td>
        <td width="340"><label>
          <input name="mobil" type="text" value="<?php echo $mobil; ?>" size="15" maxlength="10">
        Dag: 
        <input type="checkbox" name="mobdag" <?php if ($mobdag == "on") echo "checked=\"checked\""; ?>>
        Kväll: 
        <input type="checkbox" name="mobkvall" <?php if ($mobkvall == "on") echo "checked=\"checked\""; ?>>
        </label></td>
      </tr>
  </table>
  <br>
<table width="400" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td colspan="2">Vid leverans per post ser vi gärna att ni fyller i en leveransadress.</td>
      </tr>
      <tr>
        <td colspan="2"> </td>
      </tr>
      <tr>
        <td width="60">Adress:</td>
        <td width="340"><label>
          <input name="adress" type="text" value="<?php echo $adress; ?>" size="30">
        </label></td>
      </tr>
      
      <tr>
        <td>Postnr:</td>
        <td width="340"><label>
          <input name="postnr" type="text" value="<?php echo $postnr; ?>" size="10" maxlength="5">
        </label></td>
      </tr>
      <tr>
        <td>Stad:</td>
        <td width="340"><label>
          <input name="stad" type="text" value="<?php echo $stad; ?>" size="20">
        </label></td>
      </tr>
    </table>
<br>
<table width="400" border="0" cellspacing="0">
      <tr>
        <td>Vill ni bifoga ett meddelande eller några speciella önskemål med er beställning går det bra att göra det i textrutan här nedanf&oumlr.</td>
      </tr>
      <tr>
        <td><label>
          <textarea name="meddelande" cols="60" rows="8"><?php echo $meddelande; ?></textarea>
        </label></td>
      </tr>
    </table>
  <br>
<p>
  <label></label>
  <input name="submit" type="submit" value="Skicka" />
    </p>
</form>

 

(most comments are made in Swedish as well as the text output - I figured ppl would hopefully understand just by checking the code but if need be I will be able to translate it for you)

Link to comment
Share on other sites

If you give a form field (or multiple fields) the name foo[] then you can access $_POST['foo'] on the processing page as an array. You can also add an index inside the square brackets exactly like you would when working with arrays normally.

 

I see, thank you, however - would I have to name the field specifically using the []? In my code it would then be for example namn[]?

Link to comment
Share on other sites

cheers, as I said in my original post I never really understood arrays (I never had to use them before  ::))

 

any way, thank you, I'm currently in the works of giving my code a more advanced set of comments in English for my own sake, I'll get back if I run into any more problems later.

Link to comment
Share on other sites

okey, it's not working and I think I did what Daniel just said (obviously I did not as it isn't working but yea...) so could anyone check my changes and see if I missed something?

 

First of all, I'm not sure if handling the arrays like this is valid or if I by doing this won't get all the data out of them etcetera?

$artnr[] = secureFormInput($_POST['artnr[]']);
$antal[] = secureFormInput($_POST['antal[]']);

 

Next up where I create the dynamic fields and try to enter the user submitted data looks like this (where $i is the number of times this form has been resubmitted to create new fields)

for($t=0; $t<$i; $t++)
{
echo "<tr><td><label>";
echo "<input name=\"artnr[]\" type=\"text\" value=\""; $artnr[$t]; echo "\" size=\"15\" maxlength=\"3\" />";
echo "</label></td>";
echo "<td><label>";
echo "<input name=\"antal[]\" type=\"text\" value=\""; $antal[$t]; echo "\" size=\"6\" />";
echo "</label></td>";
echo "<td><label>";
echo "<input name=\"artnr[]\" type=\"text\" value=\""; $t++; echo $artnr[$t]; echo "\" size=\"15\" maxlength=\"3\" />";
echo "</label></td>";
echo "<td><label>";
echo "<input name=\"antal[]\" type=\"text\"value=\""; echo $artnr[$t]; $t--; echo "\"  size=\"6\" />";
echo "</label></td><tr>";
}

 

Figured I had missed something important and would appreciate if anyone could check up on me :-[

 

Here comes the entire code with the applied changes if you want to try it out locally. (and English comments  :D)

<?php

//Function to prevents SQL Injection
function secureFormInput($formdata) {
   $formdata = ereg_replace("[\'\")(;|`,<>]", "", $formdata);
   $formdata = mysql_real_escape_string(trim($formdata));
   return stripslashes($formdata);
}

//i represents the number of times this form has been submitted back to itself
$i = $_POST['i']; 
$i++;

//If $_POST['addfields'] has been set it means the user wants to add another field
if(isset($_POST['addfields']))
$submit = 0;
else
{
if ($i == 1) //If i==1 the form is viewed for the first time and thus should not be submitted
	$submit = 0;
else
{
	$submit = 1;
	echo "Vänligen granska din beställning och klicka sedan pä \"Skicka\" en gäng till. <br /><br />";
}
}

//Secures the input and fetches user input data everytime but the first time the user
$artnr[] = secureFormInput($_POST['artnr[]']);
$antal[] = secureFormInput($_POST['antal[]']);

$namn = secureFormInput($_POST['namn']);

$tele = secureFormInput($_POST['tele']);
$teldag = $_POST['teldag'];
$telkvall = $_POST['telkvall'];

$mobil = secureFormInput($_POST['mobil']);
$mobdag = $_POST['mobdag'];
$mobkvall = $_POST['mobkvall'];

$adress = secureFormInput($_POST['adress']);
$postnr = secureFormInput($_POST['postnr']);
$stad = secureFormInput($_POST['stad']);

$meddelande = secureFormInput($_POST['meddelande']);

?>

<form class="bform" method="post" action="index.php?do=2">

<?php

//Adds a hidden field with the value $i to keep track of how many times the form has been submitted to itself (how many extra fields that have been created)
echo "<input name=\"i\" type=\"hidden\" value=\"" . $i . "\" />";

?>

  <table width="400" border="0" cellspacing="0">

    <tr>
      <td>Artikelnummer</td>
      <td>Antal</td>
      <td>Artikelnummer</td>
      <td>Antal</td>
    </tr>
          
<?php

//Re-creates the old fields along with the users submitted values and adds one new set of fields
for($t=0; $t<$i; $t++)
{
echo "<tr><td><label>";
echo "<input name=\"artnr[]\" type=\"text\" value=\""; $artnr[$t]; echo "\" size=\"15\" maxlength=\"3\" />";
echo "</label></td>";
echo "<td><label>";
echo "<input name=\"antal[]\" type=\"text\" value=\""; $antal[$t]; echo "\" size=\"6\" />";
echo "</label></td>";
echo "<td><label>";
echo "<input name=\"artnr[]\" type=\"text\" value=\""; $t++; echo $artnr[$t]; echo "\" size=\"15\" maxlength=\"3\" />";
echo "</label></td>";
echo "<td><label>";
echo "<input name=\"antal[]\" type=\"text\"value=\""; echo $artnr[$t]; $t--; echo "\"  size=\"6\" />";
echo "</label></td><tr>";
}

?>
      

    <tr>
      <td colspan="4"><input type="submit" name="addfields" value="Lägg till fler artiklar" id="addfields" /></td>
    </tr>
  </table>  
  <br />
  <table width="400" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td colspan="2">Vid beställning kommer vi att kontakta er f&oumlr en bekräftelse per telefon!<br>
          <br>
Vänligen uppge namn och minst ett telefonnummer där nä er dag- eller kvällstid.<br></td>
      </tr>
      <tr>
        <td colspan="2"> </td>
      </tr>
      <tr>
        <td width="60">Namn:</td>
        <td width="340"><label>
          <input name="namn" type="text" value="<?php echo $namn; ?>" size="30">
        </label></td>
      </tr>
      <tr>
        <td>Tele:</td>
        <td width="340"><label>
          <input name="tele" type="text" value="<?php echo $tele; ?>" size="15" maxlength="9">
        Dag: 
        <input name="teldag" type="checkbox" <?php if ($teldag == "on") echo "checked=\"checked\""; ?>>
        Kväll: 
        <input type="checkbox" name="telkvall" <?php if ($telkvall == "on") echo "checked=\"checked\""; ?>>
        </label></td>
      </tr>
      <tr>
        <td>Mobil:</td>
        <td width="340"><label>
          <input name="mobil" type="text" value="<?php echo $mobil; ?>" size="15" maxlength="10">
        Dag: 
        <input type="checkbox" name="mobdag" <?php if ($mobdag == "on") echo "checked=\"checked\""; ?>>
        Kväll: 
        <input type="checkbox" name="mobkvall" <?php if ($mobkvall == "on") echo "checked=\"checked\""; ?>>
        </label></td>
      </tr>
  </table>
  <br>
<table width="400" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td colspan="2">Vid leverans per post ser vi gärna att ni fyller i en leveransadress.</td>
      </tr>
      <tr>
        <td colspan="2"> </td>
      </tr>
      <tr>
        <td width="60">Adress:</td>
        <td width="340"><label>
          <input name="adress" type="text" value="<?php echo $adress; ?>" size="30">
        </label></td>
      </tr>
      
      <tr>
        <td>Postnr:</td>
        <td width="340"><label>
          <input name="postnr" type="text" value="<?php echo $postnr; ?>" size="10" maxlength="5">
        </label></td>
      </tr>
      <tr>
        <td>Stad:</td>
        <td width="340"><label>
          <input name="stad" type="text" value="<?php echo $stad; ?>" size="20">
        </label></td>
      </tr>
    </table>
<br>
<table width="400" border="0" cellspacing="0">
      <tr>
        <td>Vill ni bifoga ett meddelande eller några speciella önskemål med er beställning går det bra att göra det i textrutan här nedanf&oumlr.</td>
      </tr>
      <tr>
        <td><label>
          <textarea name="meddelande" cols="60" rows="8"><?php echo $meddelande; ?></textarea>
        </label></td>
      </tr>
    </table>
  <br>
<p>
  <label></label>
  <input name="submit" type="submit" value="Skicka" />
    </p>
</form>

Link to comment
Share on other sites

Just quickly glancing over it, this is where you got it wrong:

 

$artnr[] = secureFormInput($_POST['artnr[]']);
$antal[] = secureFormInput($_POST['antal[]']);

 

$_POST['artnr'] and $_POST['antal'] are arrays, so what you'd do is:

 

$artnr = array_map('secureFormInput', $_POST['artnr']);
$antal = array_map('secureFormInput', $_POST['antal']);

 

That'll sanitize the arrays using your function. You can try to print_r($artnr); and print_r($antal); after you've done that to get an idea of how it works.

Link to comment
Share on other sites

If I resubmit the form to itself a few times after changing the sanitation lines you suggested I get the following while printing:

 

Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => )

Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => )

 

by looking at this I realise the values are not sent/assigned as I would want them to, too tired to really do any real work at this time but if anyone got any idea as of why this occurs I'de be glad for any hints  :-\

Link to comment
Share on other sites

I've not read this entire thread, but quick glance shows broken code:

<?php
// Your code
echo "<input name=\"antal[]\" type=\"text\" value=\""; $antal[$t]; echo "\" size=\"6\" />";

// Working code
echo "<input name=\"antal[]\" type=\"text\" value=\""; echo $antal[$t]; echo "\" size=\"6\" />";

// Some preferred code (that works too)
echo '<input name="antal[]" type="text" value="' . $antal[$t] . '" size="6" />';
?>

Also, one form of debugging is to look at your HTML ... so go look at your generated HTML (via your browser) and see if it looks like what you want. For example, it is likely showing an empty value here which will explain why PHP sees no values...

Link to comment
Share on other sites

ah yes, thank you. with applied changes it now looks as follows and it IS WORKING  ;D

 

//Re-creates the old fields along with the users submitted values and adds one new set of fields
for($t=0; $t<$i; $t++)
{
echo '<tr><td><label>';
echo '<input name="artnr[]" type="text" value="' . $artnr[$t] . '" size="15" maxlength="4" />';
echo '</label></td>';
echo '<td><label>';
echo '<input name="antal[]" type="text" value="' . $antal[$t] . '" size="6" />';
echo '</label></td>';
echo '<td><label>';
echo '</label></td><tr>';
}

 

At first this was not working either, the html code in my browser showed empty values (thnx for the tip philipolson) thus I understood where to look, I deactivated the secureFormInput-function and I immediately got the desired results.

 

Thnx for the help everyone, this topic can now be considered solved  :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.