0

I would like to submit a form and get the info written into the CSV file and stored in the hosting. And every new form should add a new line in csv. Also, lets say I have few groups that a user can join - A,b,c,d,e. How do I make it so if user choose to join 2 or three groups(1,2 and 3rd groups), it is going to store this info as 123 in the same cell. Following is my HTML code:

<!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>Phone</td>
  <td><input class="" type="text" name="phone" id="phone" /></td>
  <td>&nbsp;</td>
  <td>&nbsp;</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>

and this is my post.php file:

<?php
$fn = $_POST['fn'];
$ln = $_POST['ln'];
$phone = $_POST['phone'];
$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 = $phone . "," . $fn . "," . $ln . "," . $phone . "," . $1 ."\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
?>

When I submit the form, I get a blank page. What is wrong? index.html, post.php and testForm.csv are all in the same folder.

1 Answer 1

1

Your missing a closing bracket for

if($fp){ and if(empty($fn) || empty($ln) || empty($phone)){

You should enable error_reporting(E_ALL) it will show the error and the use of

$fn = $_POST['fn'];
$ln = $_POST['ln'];
$phone = $_POST['phone'];

as undefined if the forms not posted.

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

2 Comments

I got it working, How do I make it show a success message with return button, also how do I make it so if user chooses two or more groups, it shows all of them in 1 cell in csv file?
i guess you can have concurrency issues with this approach, if mutliple users submit at the same time

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.