繁体   English   中英

PHP表单到CSV,包含多个条目

[英]PHP form to CSV with multiple entries

我正在尝试使用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:

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

$area = $_GET['area'];
$count = count($area);

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

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

//the data
$data = "$today, $area0, $contractor, $hours, $project, $town, $street\n";
fwrite($fh, $data);
}

fclose($fh);
?>

我刚刚进行了最小的编辑,除了表单中的区域变量之外,你没有使用任何东西,而且你列出的很多值都没有在任何地方初始化(小时,项目等)。 此外,我现在还没有访问任何地方来实际的PHP代码,这是来自内存,所以可能有拼写错误/等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM