0

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

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

//close the file
fclose($fh);
}
?>
4
  • Does your submit successfully post all form data like expected? ("...only submit first entry..." is unclear to me) Commented May 21, 2014 at 19:41
  • Matthias. Yes, the first entry submits code to the .csv no problem and as expected. If another 'line' is added, filled out and submitted it does nothing. Commented May 21, 2014 at 19:54
  • I don't get it. What do you mean with "the first entry submits code to the .csv..."? You build your form and - in the end - submit it, hopefully containing all entries, right? THEN loop over posted data and write it to file... okay? Commented May 21, 2014 at 19:57
  • I have a blank form with two fields. I fill those in and add a new line to enter a second entry. I fill in the second entry and click submit. When I open the .csv there is only one entry. Commented May 21, 2014 at 20:00

1 Answer 1

2

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\n";
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.

Sign up to request clarification or add additional context in comments.

12 Comments

Thank you for your comment. Can you provide an example of where $count would be placed?
Before your for loop. It has to somehow know to iterate the loop for however many entries are submitted, so you have to come up with a way to initailize the $count variable to that number. You could add it to the form itself so it sends that count through, or change the form to sent your variables as array (instead of area1, area2 make them area[1] and area[2] and then you can use count($area) in the php script.
If you could't tell I am a novice. I don't really understand how to do either of those... Sorry. Do you have any examples?
Thanks for the example! I have implemented this code but am not getting the results, actually moving backwards. Using just the code you provided there are no entries being recorded in the csv. If you go to this page, selectbylocation.com/index2.html, and click 'add new entry', then inspect the page you can see it is working (adding [1] to each entry). I think it may be in the php as it is looking for something named area[0] and it doesn't seem to be finding it.. Also, the other variables are there but not being used yet until I can get the thing working in the first place. Any ideas?
right, you also could have changed the for line to for( $i = 0; $i < $count; $i++ ). Since the array starts at 0, if there are say 5 entries they are 0 through 4, so you only need to go up to 5 (the number of entries minus 1)
|

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.