1

我正在尝试使用 PHP 从动态创建的字段中存储数据。固定字段不是问题。我在获取“flex”字段时遇到了麻烦;也就是说,用户可以生成的字段(最多为 3 个)以添加更多活动参与者。我已经搜索了这个网站,一般的互联网并研究了 PHP 手册。似乎解决方案在于与foreach.

到目前为止我想出的代码(正在进行中)是:

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>
4

1 回答 1

2

您在 html / javascript 中设置这样的动态输入:

 name='myInputs[]'
               ^^ an array

因此,要在 php 中获取值,您需要遍历:

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

使用例如foreach.

如果您只想可视化这些值,您还可以使用以下内容:

implode(', ', $_GET['myInputs'])
于 2013-07-20T13:33:53.180 回答