user3408255
user3408255

Reputation: 3

Trying to make a HTML form using PHP send contents to a CSV file

I currently have this code exactly:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Reg</title>
    </head>

    <body>
        <form id="form1" name="form1" method="post" action="post.php">
            <table width="597" class="formatTblClass">
                <tr>
                    <th colspan="4"></th>
                </tr>
                <tr>
                    <td width="99"><span>First Name</span></td>
                    <td width="217"><input class="" type="text" name="fn" id="fn" /></td>
                    <td width="99"><span>Last Name</span></td>
                    <td width="211"><input class="" name="ln" type="text" id="ln" /></td>
                </tr>

                <tr>
                    <td colspan="4">Check groups that you would like to receive updates about</td>
                </tr>
                <tr>
                    <td><input name="1" type="checkbox" id="1" value="a" /></td>
                    <td>A</td>
                    <td><input name="4" type="checkbox" id="4" value="b" /></td>
                    <td>B</td>
                </tr>
                <tr>
                    <td><input name="2" type="checkbox" id="2" value="c" /></td>
                    <td>C</td>
                    <td><input name="5" type="checkbox" id="5" value="d" /></td>
                    <td>D</td>
                </tr>
                <tr>
                    <td><input name="3" type="checkbox" id="3" value="e" /></td>
                    <td>E</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td colspan="4">
                        <div align="center">
                            <input type="submit" name="Submit" id="Submit" value="Submit" />
                            <input type="reset" name="Reset" id="button" value="Reset" />
                        </div>
                    </td>
                </tr>
        </table>
    </form>
</body>
</html>

<?php
    $fn = $_POST['fn'];
    $ln = $_POST['ln'];
    $1 = (isset($_POST[1])) ? $_POST[1] : No;
    $2 = (isset($_POST[2])) ? $_POST[2] : No;
    $3 = (isset($_POST['3'])) ? $_POST['3'] : 'No';
    $4 = (isset($_POST['4'])) ? $_POST['4'] : 'No';
    $5 = (isset($_POST['5'])) ? $_POST['5'] : 'No';

    //validate

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

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

    $fp = fopen("pwrtest.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
    }

?>

On the PHP page I get an error:

Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in /home/aaaaaaa/public_html/aaaaaaaa.com/formtest/post.php on line 5

--

I am pretty new to code so I would appreciate any help greatly.

Thanks for your time.

Best Regards, V. Kije

Upvotes: 0

Views: 52

Answers (1)

Marc B
Marc B

Reputation: 360572

PHP variable names MUST start with an alphabetical character. $1, $2, etc... are NOT valid, except when used in preg_*() expressions.

$1 = (isset($_POST[1])) ? $_POST[1] : No;
 ^--- illegal variable name

Upvotes: 1

Related Questions