How do you put form data in a CSV file using PHP
-
Why doesn't this work? 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="get" action="processdata.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 = $_POST['area']; $count = count($area); //open the file and choose the mode $fh = fopen("data.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); ?> <html> <body> <p>Thank you.</p> <button><a href="index.html"> Back to form</a></button> <br> <form method="get" action="http://selectbylocation.com/data.csv"> <button type="submit">View CSV</button> </form> </body> </html>
-
Answer:
Why do you expect it to work? Your form, whose id is myform, sends the form as a http GET request, while, you are trying to the get $area value from $_POST instead of $_GET. Also, I would recommend a http://www.sscce.org/. You can use http://viper-7.com/ for creating a share-able example
Srihari Sankar Sahu at Quora Visit the source
Other answers
The PHP code part is correct and it should work.I think you are missing the form posting mechanismwhen you are using <form id="myform" name="form1" method="get" ..you should use $_GET to hold the form data.Some thing like this<?php if(isset($_GET)){$area = $_GET['area'];}And also while writing to a csv file use this<?phpdate_default_timezone_set('America/New_York');$area= array();$data_line = "";if(isset($_GET)){ $area = $_GET['area'];}$count = count($area);//open the file and choose the mode$fh = fopen("data.csv", "a");for( $i = 0; $i <= $count; $i++){ $data_array=array(); $data_array[] = date("m.d.y"); //today $data_array[] = $area[$i]; //area $data_array[] = $_GET['contractor']; //contractor $data_array[] = $_GET['hours']; //hours $data_array[] = $_GET['project']; //project $data_array[] = $_GET['town']; //town $data_array[] = $_GET['street']; //street $data_line = '"'.implode('","',$data_array).'"'."\n"; fwrite($fh, $data_line);}fclose($fh); ?>
Sudhakar Mangipudi
Related Q & A:
- how delete node in xml file using php?Best solution by Stack Overflow
- how to parse a xml file using jquery and phonegap?Best solution by Stack Overflow
- How to read a PDF file with PHP?Best solution by Stack Overflow
- How can i export my contacts to a csv file?Best solution by Yahoo! Answers
- How to search for a particular string in a text file using java?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.