douyun6781 2014-05-21 19:32
浏览 58
已采纳

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

I am trying to create a simple online form using html and php tooutput to a .csv. I have no problem doing this for a single form but the users of this form will usually have multiple entries at once. To make it easier for them I am using a form of which you can add lines to submit more entries in one submission. Below is the HTML and PHP code.

The issue I am having is the PHP part. I can only get it to ever submit the first entry.

Any ideas? I have a working version of the site/form here, but again, only the first entry every gets entered into the .csv. Thanks for the help.

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
";

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

//close the file
fclose($fh);
}
?>
  • 写回答

1条回答 默认 最新

  • dongqin1075 2014-05-21 19:51
    关注

    Well, if this is all of the code and you didn't leave anything out I'd say you need to initialize $count.

    Also, I'd probably open the file handle before the loop and close it after the loop instead of needlessly opening and closing it.

    Edit - adding code:

    <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
    ";
    fwrite($fh, $data);
    }
    
    fclose($fh);
    ?>
    

    I just made the minimal edits, you aren't using anything but the area variable from your form, and there are a lot of values you have listed that aren't initialized anywhere (hours, project, etc). Also, I haven't got access to anyplace to do actual php code right now, and this is from memory, so there may be typos/etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题