1

I am trying to store data from dynamically created fields using PHP. The fixed fields are not a problem. I am having troubles in GET-ing the "flex" fields; that is, the fields a user can generate (till the max of 3) to add more participants for an event. I have searched this site, the internet in general and studied the PHP manual. It seems as if the solutions lies in working with foreach, but I am not smart and skilled enough (yet) to compose the right function.

The code I came up with so far (work in progress) is:

HTML:

<!DOCTYPE html PUBLIC etc >
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript"> 
      var counter = 0;
      var limit = 3;
      var veldwaarde ="";

      function addInput(divName) {
        if (counter == limit)  {
          alert("More then " + counter +  " partcipants not possible ");
        } else {
          var newdiv = document.createElement('div');
          newdiv.innerHTML = "<table><tr><td>Naam deelnemer " + (counter + 1) + "</td><td><input type='text' name='myInputs[]'/></td></tr>" + "<tr><td>Schoenmaat deelnemer " + (counter + 1) + "</td><td><br><input type='text' name='myInputs2[]'></td></tr></table>";
          document.getElementById(divName).appendChild(newdiv);
          counter++;
        }
      }
    </script>
  </head>

  <form name="formulier" method="get" action="invoer_data.php" >
    <table>
      <tr><td width="15%">Naam:</td><td width="15%"><input type="text" name="naam"   >id="naam" value="" ></td></tr>
      <tr><td width="15%">Schoenmaat:</td><td><input type="text" name="schoenmaat" >    >id="schoenmaat" value="" ></td></tr>
      <!--  <tr><td width="15%">Adres:</td><td><input type="text" name="adres" id="adres"   >value="" ></td></tr>
            <tr><td width="15%">Postcode:</td><td><input type="text" name="postcode" >id="postcode" value="" ></td></tr>
      <tr><td width="15%">Woonplaats:</td><td><input type="text" name="woonplaats" >id="woonplaats" value="" ></td></tr>
      <tr><td width="15%">E-mail:</td><td><input type="text" name="email" id="email" >value="" ></td></tr>
      <tr><td width="15%">Telefoon overdag:</td><td><input type="text" name="telefoon" >id="telefoon" value=""></td></tr> -->
      <tr><td  width="15%">More partcipants:</td><td><div id="dynamicInput" > <a href="" >onClick="addInput('dynamicInput');return false;"><p style="color:red;">Add (max 3) >partcipants</p></a></td></tr>

      <td><input type="submit" name="verzenden"  value="Verzenden"></td>
    </table>
  </form>

PHP:

<?php
// collect varaiables
@$naam = addslashes($_GET['naam']);
@$schoenmaat = addslashes($_GET['schoenmaat']);
@$adres = addslashes($_GET['adres']);
@$postcode = addslashes($_GET['postcode']);
@$woonplaats = addslashes($_GET['woonplaats']);
@$email = addslashes($_GET['email']);
@$telefoon = addslashes($_GET['telefoon']);
// next two lines are for the dynamically generated fields
@$myInputs = addslashes($_GET['myInputs']);
@$myInputs2 = addslashes($_GET['myInputs2']);

// Validation
if (strlen($naam) == 0) {
  die("<p align=\"center\"><font face=\"Vrinda\" size=\"4\"color=\"#FF0000\">First field is empty</font></p>");
}

//saving record in a text file to test the GET function
$pfw_file_name = "invoerDynvelden.txt";
$pfw_first_raw = "naam,schoenmaat,adres,postcode,woonplaats,email,telefoon,naam2,schoenmaat2,naam3,schoenmaat3,naam4,schoenmaat4\r\n";
$pfw_values = "$naam,$schoenmaat,$adres,$postcode,$woonplaats,$email,$telefoon,$naam2,$schoenmaat2,$naam3,$schoenmaat3,$naam4,$schoenmaat4\r\n";
$pfw_is_first_row = false;
if(!file_exists($pfw_file_name)) {
  $pfw_is_first_row = true;
}
if (!$pfw_handle = fopen($pfw_file_name, 'a+')) {
  die("Cannot open file ($pfw_file_name)");
  exit;
}
if ($pfw_is_first_row)
{
  if (fwrite($pfw_handle, $pfw_first_raw ) === FALSE) {
    die("Cannot write to file ($pfw_filename)");
    exit;
  }
}
if (fwrite($pfw_handle, $pfw_values) === FALSE) {
  die("Cannot write to file ($pfw_filename)");
  exit;
}
fclose($pfw_handle);
//saving record to MySQL database DBxxxxx still to be developed
?>
<body>
<a href="index.htm">Returm to entry screen</a><br/>
</body>
2
  • 3
    Do you think this text is readable? Commented Jul 20, 2013 at 13:26
  • Dear Sir,I appereciate your feedback and need some tips to improve the readabilty, since I followed the forums guidelines to publish the code. Later on I added the link <dynveld.howardwoei.nl> but I als read that it has te be reviewed before publishing. Commented Jul 21, 2013 at 10:01

1 Answer 1

2

You are setting your dynamic inputs like this in the html / javascript:

 name='myInputs[]'
               ^^ an array

So to get your values in php, you would need to loop through:

 $_GET['myInputs']    // this is an array

using for example a foreach.

If you just want to visualise the values, you could also use something like:

implode(', ', $_GET['myInputs'])
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot Jeroen; Ik managed to aplly the for each and it works fine. The next problem I ran into is how to store the data tha the foreach retrieved from the input array, tot the MYSQLdatabase. The fields for the extra data (participants) are there, but I am having a hard time to store in an appropiate format(just like the fixed fields). What I have found so far is :> $sqldata = array (serialize($session_data), $_SERVER['PHP_AUTH_USER']);> but I just cannot figure out how to implement this the right way. Tips are appreciated (you can use the form for your bizz overthere in Peru :-)
Oops Don't bother anymore: I finally managed to get it working with> $extras=serialize($deelnemer2); > $klompmaat=serialize($schoenmaat2);next issue however is that only the last (of three) added fields (= the last element of the array)gets stored in the txt file.I will work on that and would appreciate a tip from anyone.Thnxs in advance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.