Creating a CSV from a web form output

时间:2017-04-24 17:18:32

标签: php html html5 csv

I'm currently trying to make a web form such that when a user enters their information (they're applying for a service upgrade), their application will be put through to a CSV file.

I've created my web form, albeit very roughly as I'm not skilled in web dev/design. This is it here:

<!DOCTYPE html>
<html>
<style type="text/css">
.fieldset-auto-width {
     display:inline;
}

form {
text-align: center;
}


select.my_dropdown {
width:130px;
}
</style>

<head>

    <!-- head stuff goes here -->
    <script src="users.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js" ></script>
    <script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js" ></script> 
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css" >
</head>

<body>
    <h2 align="center"><u>Service Request Application Form</u></h2>
    <hr>

<?php
    if($_POST['formSubmit'] == "Submit Application") 
    {
    $fn = $_POST['CustomerName'];
    }
?>

    <form method="post" align="center" name="myForm" id="applicationform" action="export.php">

        <!--###################################### CONTACT INFORMATION FIELDSET ######################################-->
        <fieldset  style="border: 1px black solid">
            <legend style="font-weight:bold"><u>Contact Information</u></legend>
            <table>
            <tr>
            <th></th>
            <th><u>Customer</u></th>
            <th title="Electrician"><u>Consultant/Contractor</u></th>
            </tr>
            <tr>
                <td>
                    <tr>
                        <td align="right" id="namelabel">Contact Name:</td>
                        <td><input type="text" id="customername" name="CustomerName" title="Name of contact on account"/></td>
                        <td><input type="text" id="contractorname" name="ContractorName" title="Name of contractor or consultant" /></td>
                    </tr>

                    <tr>
                        <td align="right" id="addresslabel">Mailing Address:</td>
                        <td><input type="text" id="customeraddress" name="CustomerMailingAddress" title="Enter your mailing address"/></td>
                        <td><input type="text" id="contractoraddress" name="ContractorMailingAddress" title="Enter your contractor's mailing address" /></td>
                    </tr>

                    <tr>
                        <td align="right" id="phonelabel">Phone Number:</td>
                        <td><input type="text" id="customerphone" name="CustomerPhoneNumber" title="Enter your phone number here" /></td>
                        <td><input type="text" id="contractorphone" name="ContractorPhoneNumber" title="Enter contractor's phone number here" /></td>
                    </tr>

                    <tr>
                        <td align="right" id="mobilephonelabel">Mobile Number:</td>
                        <td><input type="text" id="customermobilephone" name="CustomerMobilePhoneNumber" title="Enter your mobile phone number here"/></td>
                        <td><input type="text" id="contractormobilephone" name="ContractorMobilePhoneNumber" title="Enter contractor's mobile phone number here"/></td>
                    </tr>

                    <tr>
                        <td align="right" id="faxlabel">Fax:</td>
                        <td><input type="text" id="customerfax" name="CustomerFax" title="Enter fax information here" /></td>
                        <td><input type="text" id="contractorfax" name="ContractorFax" title="Enter fax information here"/></td>
                    </tr>

                    <tr>
                        <td align="right" id="emaillabel">E-mail:</td>
                        <td><input type="text" id="customeremail" name="CustomerEmail" title="Enter your email address here" /></td>
                        <td><input type="text" id="contractoremail" name="ContractorEmail" title="Enter contractor's email address here" /></td>
                    </tr>

                    <tr>
                        <td align="right" id="accountnumberlabel">Account Number:</td>
                        <td colspan="2"><input type="text" id="accountnumber" name="AccountNumber" size="46" title="Enter account number for requested upgrade here"/></td>
                    </tr>


                </td>

            </tr>
            </table>            
        </fieldset>

        <!--###################################### SERVICE INFORMATION FIELDSET ######################################-->
        <fieldset  style="border: 1px black solid">
            <legend style="font-weight:bold"><u>Service Information</u></legend>
            <table>
            <tr>
            <th></th>
            <th></th>
            <th></th>
            </tr>
            <tr>
                <td>
                    <tr>
                        <td align="right" id="serviceaddresslabel">Service Address:</td>
                        <td><input type="text" id="serviceaddress" name="ServiceAddress" title="Location of requested service"/></td>
                    </tr>

                    <tr>
                    <td align="right" id="unitlabel">Unit #:</td>
                    <td align="left"><input type="text" id="unitnumber" name="UnitNumber" title="Unit for requested service"/></td>
                    </tr>

                    <tr>
                        <td align="right" id="servicetypelabel">Service Type:</td>
                        <td align="left">
                            <select class="my_dropdown" name="ServiceType" id="servicetype" title="Select the type of service you require">
                                <option value="-1" selected>[select--service]</option>
                                <option value="1">Upgrade</option>
                                <option value="2">Temporary</option>
                                <option value="3">New Permanent</option>
                                <option value="4">Other</option>
                            </select>
                        </td>

                    </tr>

                    <tr>
                        <td align="right" id="customerclasslabel">Customer Class:</td>
                        <td align="left">
                            <select class="my_dropdown" name="CustomerClass" id="customerclass" title="Select the type of account the service is for">
                                <option value="-1" selected>[select--type]</option>
                                <option value="1">Residential</option>
                                <option value="2">Commercial</option>
                                <option value="3">Industrial</option>
                            </select>
                        </td>
                    </tr>

                    <tr>
                        <td align="right" id="proposedloadlabel">Proposed Load:</td>
                        <td align="left"><input type="text" id="proposedload" name="ProposedLoad" size="10" title="Enter the proposed Kilowatt load the upgrade will handle"/>kW</td>   
                    </tr>

                    <tr>
                        <td align="right" id="proposedmainlabel">Proposed Main Switch:</td>
                        <td align="left"><input type="text" id="proposedmainswitch" name="ProposedMainSwitch" size="10" title="Enter the proposed Amperage (Amps) load the main switch will handle"/>A</td> 

                        <td align="left" id="existingmainlabel">Existing Main Switch:</td>
                        <td align="left"><input type="text" id="existingmainswitch" name="ExistingMainSwitch" title="Enter the current Amperage load the main switch handles"size="10"/>A</td>  
                    </tr>   

                    <tr>
                        <td align="right" id="voltagelabel">Voltage:</td>
                        <td align="left">
                            <select class="my_dropdown"  name="Voltage" id="voltage" title="Select the Voltage rating required">
                                <option value="-1" selected>[select--voltage]</option>
                                <option value="1">240/120V</option>
                                <option value="2">208/120V</option>
                                <option value="3">600/347V</option>
                            </select>
                        </td>
                    </tr>

                    <tr>
                        <td align="right" id="phaselabel">Phase:</td>
                        <td align="left">
                            <select class="my_dropdown"  name="Phase" id="phase" title="Select the upgrade phase preferred">
                                <option value="-1" selected>[select--phase]</option>
                                <option value="1">Single-Phase</option>
                                <option value="2">Three-Phase</option>
                            </select>
                        </td>
                    </tr>           

                        <tr>
                        <td align="right" id="powersupplylabel">Power Supply:</td>
                        <td align="left">
                            <select class="my_dropdown"  name="PowerSupply" id="powersupply" width="90" title="Select position of power supply required">
                                <option value="-1" selected size>[select--supply]</option>
                                <option value="1">Overhead (OH)</option>
                                <option value="2">Underground (UG)</option>
                                <option value="3">OH to UG</option>
                                <option value="4">UG to OH</option>
                            </select>
                        </td>
                    </tr>   

                    <tr>
                        <td align="right" id="meterbaselabel">Meter Base Location:</td>
                        <td align="left">
                            <select class="my_dropdown"  name="MeterBaseLocation" id="MeterBaseLocation" title="Select the location of the Meter">
                                <option value="-1" selected>[select--location]</option>
                                <option value="1">Existing Outside</option>
                                <option value="2">Inside Moving Out</option>
                                <option value="3">Relocate</option>
                                <option value="4">Ganged Position*</option>
                            </select>
                            <td align="left"><input type="text" id="existingmainswitch" name="ExistingMainSwitch" size="5" title="If meter location requires a Ganged (multiple) position installation, how many positions are needed?"/>*Positions</td>
                        </td>
                    </tr>
                </td>
            </tr>
            </table>            
        </fieldset>
        <table>

            <tr>
                <td align="center" id="commentslabel" title="Anything you want to add? Tell us here"><b><u>Comments/Reason for Upgrade:</b></u></td>
            </tr>

            <tr>
                <td><textarea align="center" id="comments" rows="4" cols="80" style="border: 1px black solid" title="Anything you want to add? Tell us here"></textarea></td>
            </tr>

            <tr>
            <td align="left">
            <input type="checkbox" align="left" id="consent" value="consent1"><b> I confirm that the information I have given in this form is true to the best of my knowledge.</b><br>
            <input type="checkbox" align="left" id="consent" value="consent2"><b>
            I acknowledge and accept that if we schedule the connection to be done after hours (3:30PM) a charge
            of $415 + HST will be added to my electricity bill for the month. </b><br>
            </td>
            </tr>
            <tr>
            <td colspan="2" align="center"><input type="submit" name="formSubmit" value="Submit Application"></td>
            </tr>

        </table>
    </form>
</body>

I've created a 'starter' file to test my form. I've only included a few variables for field testing as I'm not sure if I'm doing it the right way to output it to a CSV file. (CSV output required by employer)

Here is my action handler file named export.php:

    <?php
    $fn = $_POST['customername'];
     $ln = $_POST['contractorname'];
    // $address = $_POST['address'];

    //validate 

    if(empty($fn) || empty($ln) || empty($address) || empty($city) || empty($state) || empty($zip) || empty($phone) || empty($email)){//show the form
     $message = 'Fill in areas in red!';
     $aClass = 'errorClass';
    }

     //this is where the creating of the csv takes place
     $cvsData = $fn . "," . $ln ."\n";

     $fp = fopen("formTest.csv","a"); // $fp is now the file pointer to file $filename

     if($fp){
     fwrite($fp,$cvsData); // Write information to the file
     fclose($fp); // Close the file
     }

    ?>

I am trying to output to a file for testing named formTest.csv. Currently I believe I'm having issues with PHP on my PC at work, as the network admins restrict access on our computers they were the ones who set it up while I was away. Currently when I click submit it just opens my export.php page, which is why I say my issue is PHP on my machine. But when I tested this on my home PC which SHOULD have PHP working (but it has been a while), it would return to my form but nothing would be input into the CSV.

Any tips?

0 个答案:

没有答案