Jump to content
Sign in to follow this  
princesohrab

php - data from online form to Excel Spreadsheet

Recommended Posts

Hi all,Online registration form which allow users to fill in their details and send the data by submitting the form to my email using php.I need to create an online registration form and when the form is submitted, the data needs to go in to a spreadsheet in (MS EXCEL). Maybe a database I guess..The data should be able to be accessed at any time and read by a spreadsheet by someone in the office. Someone sent me this code

$df = fopen("php://output", 'w');   fputcsv($df, array_keys(reset($array)));   foreach ($_POSTas $row) {	  fputcsv($df, $row);   }   fclose($df);

Can anyone help me as to where and how I use this snippet to get it to work. Basically the form data needs to be written into a CSV file. Here is the form:

<form action="http://www.sohrabgoodar.com/w-hill-contactformprocess.php" method="post" id="comments_form"><div class="row">    <div class="label">Name</div><!--end .label-->    <div class="input">    <input type="text" for="name" id="name" class="detail" name="name" />    </div><!--end .input-->     </div><!--end .row-->          <div class="row">    <div class="label">Work Location</div><!--end .label-->    <div class="input">    <input type="text" for="worklocation" id="worklocation" class="detail" name="worklocation" />    </div><!--end .input-->    </div><!--end .row-->          <div class="row">    <div class="label">Work Contact Number</div><!--end .label-->    <div class="input">    <input type="text" for="worknumber" id="worknumber" class="detail" name="worknumber" />    </div><!--end .input-->     </div><!--end .row-->          <div class="row">    <div class="label">Mobile Contact Number</div><!--end .label-->    <div class="input">    <input type="text" for="mobile" id="mobile" class="detail" name="mobile" />    </div><!--end .input-->     </div><!--end .row-->          <div class="row">    <div class="label">Email Address</div>    <!--end .label-->    <div class="input">    <input type="text" for="email" id="email" class="detail" name="email" />    </div><!--end .input-->      </div><!--end .row-->          <div class="row">    <div class="label">Ability Level</div>    <!--end .label-->    <div class="input3">	   <select name="ability" for="ability" id="ability" class="detail">		  <option>Please Choose</option>    <option value="strong">Strong</option>    <option value="medium">Medium</option>    <option value="weak">Weak</option>    </select>    </div><!--end .input3-->     </div><!--end .row-->          <div class="row">    <div class="label">Tennis Standard</div>    <!--end .label-->    <div class="input3">	   <select name="standard" for="standard" id="standard" class="detail">		  <option>Please Choose</option>    <option value="school">School</option>    <option value="club">Club</option>    <option value="county">County</option>		  <option value="international">International</option>    </select>    </div><!--end .input3-->     </div><!--end .row-->          <div class="row">    <div class="label">Frequency of Play</div>    <!--end .label-->    <div class="input3">	   <select name="frequency" for="frequency" id="frequency" class="detail">		  <option>Please Choose</option>    <option value="weekly">Weekly</option>    <option value="monthly">Monthly</option>    <option value="fewtimes">Few Times a Year</option>    </select>    </div><!--end .input3-->     </div><!--end .row-->      <div class="row">    <div class="label2">Anything else you would like to tell us?</div><!--end .label2-->    <div class="input2">    <textarea id="comment" name="comment" class="mess"></textarea>    </div><!--end .input-->    </div><!--end .row-->          <div class="submit">    <input type="submit" id="submit" name="submit" value="Send Message" />    </div><!--end .submit--></form>

Here is the old php file I was using to send the form data to emails.

<?php    $emailSubject = 'Tennis Registration!';$webMaster = 'someone@yahoo.co.uk';$nameField = $_POST['name'];$worklocationField = $_POST['worklocation'];$worknumberField = $_POST['worknumber'];$mobileField = $_POST['mobile'];$emailField = $_POST['email'];$abilityField = $_POST['ability'];$standardField = $_POST['standard'];$frequencyField = $_POST['frequency'];$commentField = $_POST['comment'];$body = <<<EOD<br><hr><br>Name: $nameField <br>Work location: $worklocationField <br>Work Number: $worknumberField <br>Mobile: $mobileField <br>Email: $emailField <br>Ability: $abilityField <br>Standard: $standardField <br>Frequency: $frequencyField <br>Comment: $commentField <br>EOD;$headers = "From: $email\r\n";$headers .= "Content-type: text/html\r\n";$success = mail($webMaster, $emailSubject, $body, $headers);$theResults = <<<EOD<p>Thank you for your Message! You will be redirected to the home page in a moment</p><script type="text/javascript" >setTimeout ('ourRedirect()', 3000)function ourRedirect(){  location.href='http://www.google.com/'}</script>EOD;echo "$theResults";?>

Share this post


Link to post
Share on other sites

That code isn't going to work as written. It has a syntax error, they aren't using reset correctly, and it's going to write CSV data to memory but then it doesn't get that data. Look at the manual for fputcsv, that will let you open a file and write an array to the file as a CSV. But you should be writing CSV data from the database, not the form. The form should just insert the data to the database, and then you need to create another PHP page for the purpose of getting records from the database and creating a CSV file.

  • Like 1

Share this post


Link to post
Share on other sites

That is exacly what needs to be done.. But I am more of a front end guy..Im not too sure how to proceed about it.Your help is greatly appreciated. I found this tutorial

Not sure if its the same thing..I really need to figure this out.

Share this post


Link to post
Share on other sites

Well, take it in steps. Step 1 is to save the form data in a database. If you don't know how to do that, check the tutorials for processing forms and using a database. Once you have everything with the database working, then you can move to creating a CSV file. To start with, just create a static CSV file from hard-coded data, rather than from a database. Once you can create a CSV file at all then combine the steps and create a CSV from data in the database.

  • Like 1

Share this post


Link to post
Share on other sites

Thanks.. Appreciate it..I will do it step by step and if I get stuck, Ill get back to you.I found a few tutorials and hopefully Ill get my head around it.Thanks

Share this post


Link to post
Share on other sites

The one thing Im lost on is creating the CSV file..Does that file stay opn the server? Do I create it with Excel? Then how to link the 2 together?

Share this post


Link to post
Share on other sites

It starts on the server, but you can do whatever you want with it. You can just leave it on the server and put a link somewhere, maybe have a page that checks a certain directory and lists links for all of the files in that directory, or you can just create it temporarily. If you create it temporarily then you can email it or upload it via FTP or something else, and then delete it after you're done. PHP doesn't interact with Excel (normally anyway, there might be a COM object you can use if Excel is installed on the server). The CSV file is just a text file formatted a specific way that Excel and other spreadsheet programs know how to open. There's no link between the file and Excel. If someone clicks on a link to the file or if you have a page that dynamically creates it and sends it out to be downloaded using headers, then the user can choose to save or open it and if they have Excel installed then it will probably be the default program to open the file with.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...