使用多个条目将PHP表单转换为CSV

使用多个条目将PHP表单转换为CSV,php,html,forms,csv,Php,Html,Forms,Csv,我正在尝试使用html和php创建一个简单的在线表单,并将其输出为.csv。我对单个表单执行此操作没有问题,但是此表单的用户通常会同时有多个条目。为了让他们更容易,我使用了一种形式,你可以添加行提交更多的条目在一次提交。下面是HTML和PHP代码 我遇到的问题是PHP部分。我只能让它提交第一个条目。 有什么想法吗?我有一个站点/表单的工作版本,但同样,只有第一个条目被输入到.csv中。谢谢你的帮助 HTML: <html> <head> <meta http-equ

我正在尝试使用html和php创建一个简单的在线表单,并将其输出为.csv。我对单个表单执行此操作没有问题,但是此表单的用户通常会同时有多个条目。为了让他们更容易,我使用了一种形式,你可以添加行提交更多的条目在一次提交。下面是HTML和PHP代码

我遇到的问题是PHP部分。我只能让它提交第一个条目。

有什么想法吗?我有一个站点/表单的工作版本,但同样,只有第一个条目被输入到.csv中。谢谢你的帮助

HTML:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="mdcstyle.css" />
<title>Submission form</title>

<script src="http://code.jquery.com/jquery-latest.js"></script>

<script type='text/javascript'>
//<![CDATA[
 $(document).ready(function() {
  var currentItem = 0;
  $('#addnew').click(function(){
   currentItem++;
   $('#items').val(currentItem);
var strToAdd = '<tr><td>Area:<input class="textfield"  name="area'+currentItem+'" id ="area'+currentItem+'"type="text" /></td><td>Contractor:<input class="textfield"  name="contractor'+currentItem+'" id ="contractor'+currentItem+'"type="text" /></td></tr>';
   $('#data').append(strToAdd);

  });
 });

//]]>
</script> 

</head>

<body>

    <div class="content">
    <h2>header text</h2>
    <p>Please complete this form for Udpates. Add a new line if you have more than one project.</p>
  <form id="myform" name="form1" method="post" action="signup2.php">  
<table class="dd" width="100px" id="data">
  <tr>

    <td>Area:<input class="textfield" name="area0" id="area0" type="text" /></td>
    <td>Contractor:<input class="textfield"  name="contractor0" id="contractor0" type="text" /></td>

  </tr>
</table>
      <input class="subbutton" type="submit" name="Submit" value="Submit Form"> 
 </form>   



<button id="addnew" name="addnew" value="Add new item">Add new entry</button>
<input type="hidden" id="items" name="items" value="1" /> 
        <br>
</div>
</body>


</html>
<?php

for( $i = 0; $i <= $count; $i++ )
{
    date_default_timezone_set('America/New_York'); 
    $today = date("m.d.y");   
    $area0 = $_POST['area'.$i];

//the data
$data = "$today, $area0, $contractor, $hours, $project, $town, $street\n";

//open the file and choose the mode
$fh = fopen("users2.csv", "a");
fwrite($fh, $data);

//close the file
fclose($fh);
}
?>

提交表格
//
标题文本
请填写这张表格。如果有多个项目,请添加新行

面积: 承包商: 添加新条目
PHP:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="mdcstyle.css" />
<title>Submission form</title>

<script src="http://code.jquery.com/jquery-latest.js"></script>

<script type='text/javascript'>
//<![CDATA[
 $(document).ready(function() {
  var currentItem = 0;
  $('#addnew').click(function(){
   currentItem++;
   $('#items').val(currentItem);
var strToAdd = '<tr><td>Area:<input class="textfield"  name="area'+currentItem+'" id ="area'+currentItem+'"type="text" /></td><td>Contractor:<input class="textfield"  name="contractor'+currentItem+'" id ="contractor'+currentItem+'"type="text" /></td></tr>';
   $('#data').append(strToAdd);

  });
 });

//]]>
</script> 

</head>

<body>

    <div class="content">
    <h2>header text</h2>
    <p>Please complete this form for Udpates. Add a new line if you have more than one project.</p>
  <form id="myform" name="form1" method="post" action="signup2.php">  
<table class="dd" width="100px" id="data">
  <tr>

    <td>Area:<input class="textfield" name="area0" id="area0" type="text" /></td>
    <td>Contractor:<input class="textfield"  name="contractor0" id="contractor0" type="text" /></td>

  </tr>
</table>
      <input class="subbutton" type="submit" name="Submit" value="Submit Form"> 
 </form>   



<button id="addnew" name="addnew" value="Add new item">Add new entry</button>
<input type="hidden" id="items" name="items" value="1" /> 
        <br>
</div>
</body>


</html>
<?php

for( $i = 0; $i <= $count; $i++ )
{
    date_default_timezone_set('America/New_York'); 
    $today = date("m.d.y");   
    $area0 = $_POST['area'.$i];

//the data
$data = "$today, $area0, $contractor, $hours, $project, $town, $street\n";

//open the file and choose the mode
$fh = fopen("users2.csv", "a");
fwrite($fh, $data);

//close the file
fclose($fh);
}
?>

好吧,如果这是所有代码,并且您没有遗漏任何内容,那么我会说您需要初始化$count

此外,我可能会在循环之前打开文件句柄,然后在循环之后关闭它,而不是不必要地打开和关闭它

编辑-添加代码:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="mdcstyle.css" />
<title>Submission form</title>

<script src="http://code.jquery.com/jquery-latest.js"></script>

<script type='text/javascript'>
//<![CDATA[
 $(document).ready(function() {
  var currentItem = 0;
  $('#addnew').click(function(){
   currentItem++;
   $('#items').val(currentItem);
var strToAdd = '<tr><td>Area:<input class="textfield"  name="area['+currentItem+']" id =" area['+currentItem+']" type="text" /></td><td>Contractor:<input class="textfield"  name="contractor['+currentItem+']" id ="contractor['+currentItem+']"type="text" /></td></tr>';
   $('#data').append(strToAdd);

  });
 });

//]]>
</script> 

</head>

<body>

    <div class="content">
    <h2>header text</h2>
    <p>Please complete this form for Udpates. Add a new line if you have more than one project.</p>
  <form id="myform" name="form1" method="get" action="signup2.php">  
<table class="dd" width="100px" id="data">
  <tr>

    <td>Area:<input class="textfield" name="area[0]" id="area[0]" type="text" /></td>
    <td>Contractor:<input class="textfield"  name="contractor[0]" id="contractor[0]" type="text" /></td>

  </tr>
</table>
      <input class="subbutton" type="submit" name="Submit" value="Submit Form"> 
 </form>   



<button id="addnew" name="addnew" value="Add new item">Add new entry</button>
<input type="hidden" id="items" name="items" value="1" /> 
        <br>
</div>
</body>


</html>

提交表格
//
标题文本
请填写这张表格。如果有多个项目,请添加新行

面积: 承包商: 添加新条目
php:



我只做了最小的编辑,除了表单中的区域变量,您没有使用任何东西,而且您列出的许多值在任何地方都没有初始化(小时、项目等)。另外,我现在还没有访问任何地方来编写实际的php代码,这是从内存中获取的,因此可能会出现输入错误等。

您的提交是否成功地按照预期发布了所有表单数据?(“…只提交第一个条目…”我不清楚)Matthias。是的,第一个条目向.csv提交代码没有问题,正如预期的那样。如果再加上一行,填写并提交,那就什么都没用了。我不明白。“第一个条目将代码提交到.csv…”是什么意思?你建立了你的表单,最后提交,希望包含所有的条目,对吗?然后循环发送数据并将其写入文件。。。好吗?我有一张有两个字段的空白表格。我填写这些内容,并添加一个新行以输入第二个条目。我填写第二个条目,然后单击提交。当我打开.csv时,只有一个条目。谢谢你的评论。在for循环之前,您能否提供一个示例说明$count将放置在何处。不管提交了多少条目,它都必须知道如何迭代循环,因此必须想出一种方法将$count变量初始化为该数字。您可以将其添加到表单本身,使其通过发送计数,或者将表单更改为将变量作为数组发送(而不是区域1,区域2使其成为区域[1]和区域[2],然后您可以使用计数($area)在php脚本中。如果你说不出我是一个新手。我真的不知道如何做这两件事…对不起。你有任何例子吗?谢谢你的例子!我已经实现了这段代码,但没有得到结果,实际上是向后移动。仅使用你提供的代码,csv中没有记录任何条目。如果你转到是页面,单击“添加新条目”,然后检查页面,您可以看到它正在工作(将[1]添加到每个条目)。我认为它可能在php中,因为它正在查找命名区域[0]而且它似乎没有找到它。另外,其他变量也在那里,但在我开始工作之前还没有被使用。有什么想法吗?对,你也可以将for行更改为for($I=0;$I<$count;$I++).由于数组从0开始,如果有5个条目,则它们是0到4,因此只需增加到5(条目数减去1)