I am new and beginner and got some codes from internet by searching and editted according to my needs, but now i want to save the data entry in html form to data.csv file, when i click submit button it should save the contents into data.csv in new line for every click submit button. This html codes i am running local computer only. I don't know how do i that here is MWE. Thanking you in advance for any help
<html>
<head>
<script type="text/javascript" src="validate.js"></script>
</head>
<body>
<form action="#" name="StudentRegistration" onSubmit="return(validate());">
<table cellpadding="2" width="20%" bgcolor="99FFFF" align="center"
cellspacing="2">
<tr>
<td colspan=2>
<center><font size=4><b>Student Registration Form</b></font></center>
</td>
</tr>
<tr>
<td>Name</td>
<td><input type=text name=textnames id="textname" size="30"></td>
</tr>
<tr>
<td>Father Name</td>
<td><input type="text" name="fathername" id="fathername"
size="30"></td>
</tr>
<tr>
<td>Postal Address</td>
<td><input type="text" name="paddress" id="paddress" size="30"></td>
</tr>
<tr>
<td>Personal Address</td>
<td><input type="text" name="personaladdress"
id="personaladdress" size="30"></td>
</tr>
<tr>
<td>Sex</td>
<td><input type="radio" name="sex" value="male" size="10">Male
<input type="radio" name="sex" value="Female" size="10">Female</td>
</tr>
<tr>
<td>Stream</td>
<td><select name="Stream">
<option value="-1" selected>Select..</option>
<option value="ARTS">ARTS</option>
<option value="COMMERCE">COMMERCE</option>
<option value="SCIENCE">SCIENCE</option>
</select></td>
</tr>
<tr>
<td>Core-1</td>
<td><select name="Core-1">
<option value="-1" selected>Select..</option>
<option value="English">English</option>
</select></td>
</tr>
<tr>
<td>Core-2</td>
<td><select name="Core-2">
<option value="-1" selected>Select..</option>
<option value="Alternative English">Alternative English</option>
</select></td>
</tr>
<tr>
<td>Elective-1</td>
<td><select name="Elective-1">
<option value="-1" selected>Select..</option>
<option value="MIL (Hindi)">MIL (Hindi)</option>
<option value="MIL (Bengali)">MIL (Bengali)</option>
<option value="MIL (Nepali)">MIL (Nepali)</option>
<option value="Political Science">Political Science</option>
<option value="Education">Education</option>
<option value="Economics">Economics</option>
<option value="History">History</option>
<option value="Geography">Geography</option>
<option value="Sociology">Sociology</option>
<option value="Logic & Philosophy">Logic & Philosophy</option>
<option value="Advance Assamese">Advance Assamese</option>
<option value="Advance Bengali">Advance Bengali</option>
<option value="Physics">Physics</option>
<option value="Chemistry">Chemistry</option>
<option value="Biology">Biology</option>
<option value="Mathematics">Mathematics</option>
<option value="Computer Science & Applications">Computer Science & Applications</option>
<option value="Accountancy">Accountancy</option>
<option value="Business Studies">Business Studies</option>
<option value="Banking">Banking</option>
<option value="Statistics">Statistics</option>
</select></td>
</tr>
<tr>
<td>Elective-2</td>
<td><select name="Elective-2">
<option value="-1" selected>Select..</option>
<option value="MIL (Hindi)">MIL (Hindi)</option>
<option value="MIL (Bengali)">MIL (Bengali)</option>
<option value="MIL (Nepali)">MIL (Nepali)</option>
<option value="Political Science">Political Science</option>
<option value="Education">Education</option>
<option value="Economics">Economics</option>
<option value="History">History</option>
<option value="Geography">Geography</option>
<option value="Sociology">Sociology</option>
<option value="Logic & Philosophy">Logic & Philosophy</option>
<option value="Advance Assamese">Advance Assamese</option>
<option value="Advance Bengali">Advance Bengali</option>
<option value="Physics">Physics</option>
<option value="Chemistry">Chemistry</option>
<option value="Biology">Biology</option>
<option value="Mathematics">Mathematics</option>
<option value="Computer Science & Applications">Computer Science & Applications</option>
<option value="Accountancy">Accountancy</option>
<option value="Business Studies">Business Studies</option>
<option value="Banking">Banking</option>
<option value="Statistics">Statistics</option>
</select></td>
</tr>
<tr>
<td>Elective-3</td>
<td><select name="Elective-3">
<option value="-1" selected>Select..</option>
<option value="MIL (Hindi)">MIL (Hindi)</option>
<option value="MIL (Bengali)">MIL (Bengali)</option>
<option value="MIL (Nepali)">MIL (Nepali)</option>
<option value="Political Science">Political Science</option>
<option value="Education">Education</option>
<option value="Economics">Economics</option>
<option value="History">History</option>
<option value="Geography">Geography</option>
<option value="Sociology">Sociology</option>
<option value="Logic & Philosophy">Logic & Philosophy</option>
<option value="Advance Assamese">Advance Assamese</option>
<option value="Advance Bengali">Advance Bengali</option>
<option value="Physics">Physics</option>
<option value="Chemistry">Chemistry</option>
<option value="Biology">Biology</option>
<option value="Mathematics">Mathematics</option>
<option value="Computer Science & Applications">Computer Science & Applications</option>
<option value="Accountancy">Accountancy</option>
<option value="Business Studies">Business Studies</option>
<option value="Banking">Banking</option>
<option value="Statistics">Statistics</option>
</select></td>
</tr>
<tr>
<td>Elective-4</td>
<td><select name="Elective-4">
<option value="-1" selected>Select..</option>
<option value="MIL (Hindi)">MIL (Hindi)</option>
<option value="MIL (Bengali)">MIL (Bengali)</option>
<option value="MIL (Nepali)">MIL (Nepali)</option>
<option value="Political Science">Political Science</option>
<option value="Education">Education</option>
<option value="Economics">Economics</option>
<option value="History">History</option>
<option value="Geography">Geography</option>
<option value="Sociology">Sociology</option>
<option value="Logic & Philosophy">Logic & Philosophy</option>
<option value="Advance Assamese">Advance Assamese</option>
<option value="Advance Bengali">Advance Bengali</option>
<option value="Physics">Physics</option>
<option value="Chemistry">Chemistry</option>
<option value="Biology">Biology</option>
<option value="Mathematics">Mathematics</option>
<option value="Computer Science & Applications">Computer Science & Applications</option>
<option value="Accountancy">Accountancy</option>
<option value="Business Studies">Business Studies</option>
<option value="Banking">Banking</option>
<option value="Statistics">Statistics</option>
</select></td>
</tr>
<tr>
<td>Compulsory</td>
<td><select name="Compulsory">
<option value="-1" selected>Select..</option>
<option value="Environmental Education">Environmental Education</option>
</select></td>
</tr>
<tr>
<td>Institute</td>
<td><select name="Institute">
<option value="-1" selected>Select..</option>
<option value="Institute-1">Institute-1</option>
<option value="Institute-2">Institute-2</option>
<option value="Institute-3">Institute-3</option>
</select></td>
</tr>
<tr>
<td>State</td>
<td><select Name="State">
<option value="-1" selected>select..</option>
<option value="New Delhi">NEW DELHI</option>
<option value="Mumbai">MUMBAI</option>
<option value="Goa">GOA</option>
<option value="Bihar">BIHAR</option>
</select></td>
</tr>
<tr>
<td>PinCode</td>
<td><input type="text" name="pincode" id="pincode" size="30"></td>
</tr>
<tr>
<td>EmailId</td>
<td><input type="text" name="emailid" id="emailid" size="30"></td>
</tr>
<tr>
<td>DOB</td>
<td><input type="text" name="dob" id="dob" size="30"></td>
</tr>
<tr>
<td>MobileNo</td>
<td><input type="text" name="mobileno" id="mobileno" size="30"></td>
</tr>
<tr>
<td><input type="reset"></td>
<td colspan="2"><input type="submit" value="Submit Form" /></td>
</tr>
</table>
</form>
</body>
</html>
Here is the output of the above html codes
I have created a .php as proces.php file according to Bill suggestion but content not writing but giving only ,,,,,,, here is my php script
<?php
$fieldA = $_POST["textnames"];
$fieldB = $_POST["fathername"];
$fieldC = $_POST["paddress"];
$fieldD = $_POST["personaladdress"];
$fieldE = $_POST["sex"];
$fieldF = $_POST["Stream"];
$fieldG = $_POST["Core-1"];
$fieldH = $_POST["Core-2"];
$fieldI = $_POST["Elective-1"];
$fieldJ = $_POST["Elective-2"];
$fieldK = $_POST["Elective-3"];
$fieldL = $_POST["Elective-4"];
$fieldM = $_POST["Compulsory"];
$keys = array($fieldA,$fieldB,$fieldC,$fieldD,$fieldE,$fieldF,$fieldG,$fieldH,$fieldI,$fieldJ,$fieldK,$fieldL,$fieldM); //THIS IS WHERE YOU PUT THE FORM ELEMENTS ex: array('$fieldA','$fieldB',etc)
$csv_line = array();
foreach($keys as $key){
array_push($csv_line,'' . $_GET[$key]);
}
$fname = 'data.csv'; //NAME OF THE FILE
$csv_line = implode(',',$csv_line);
if(!file_exists($fname)){$csv_line = "\r\n" . $csv_line;}
$fcon = fopen($fname,'a');
$fcontent = $csv_line;
fwrite($fcon,$fcontent);
fclose($fcon);
?>
and edited my original html codes from <form action="#" name="StudentRegistration" onSubmit="return(validate());">
to <form name="StudentRegistration" action="proces1.php" method="post">
so here is my new final html coding
<html>
<head>
<script type="text/javascript" src="validate.js"></script>
</head>
<body>
<form name="StudentRegistration" action="proces.php" method="post">
<table cellpadding="2" width="20%" bgcolor="99FFFF" align="center"
cellspacing="2">
<tr>
<td colspan=2>
<center><font size=4><b>Student Registration Form</b></font></center>
</td>
</tr>
<tr>
<td>Name</td>
<td><input type=text name=textnames id="textname" size="30"></td>
</tr>
<tr>
<td>Father Name</td>
<td><input type="text" name="fathername" id="fathername"
size="30"></td>
</tr>
<tr>
<td>Postal Address</td>
<td><input type="text" name="paddress" id="paddress" size="30"></td>
</tr>
<tr>
<td>Personal Address</td>
<td><input type="text" name="personaladdress"
id="personaladdress" size="30"></td>
</tr>
<tr>
<td>Sex</td>
<td><input type="radio" name="sex" value="male" size="10">Male
<input type="radio" name="sex" value="Female" size="10">Female</td>
</tr>
<tr>
<td>Stream</td>
<td><select name="Stream">
<option value="-1" selected>Select..</option>
<option value="ARTS">ARTS</option>
<option value="COMMERCE">COMMERCE</option>
<option value="SCIENCE">SCIENCE</option>
</select></td>
</tr>
<tr>
<td>Core-1</td>
<td><select name="Core-1">
<option value="-1" selected>Select..</option>
<option value="English">English</option>
</select></td>
</tr>
<tr>
<td>Core-2</td>
<td><select name="Core-2">
<option value="-1" selected>Select..</option>
<option value="Alternative English">Alternative English</option>
</select></td>
</tr>
<tr>
<td>Elective-1</td>
<td><select name="Elective-1">
<option value="-1" selected>Select..</option>
<option value="MIL (Hindi)">MIL (Hindi)</option>
<option value="MIL (Bengali)">MIL (Bengali)</option>
<option value="MIL (Nepali)">MIL (Nepali)</option>
<option value="Political Science">Political Science</option>
<option value="Education">Education</option>
<option value="Economics">Economics</option>
<option value="History">History</option>
<option value="Geography">Geography</option>
<option value="Sociology">Sociology</option>
<option value="Logic & Philosophy">Logic & Philosophy</option>
<option value="Advance Assamese">Advance Assamese</option>
<option value="Advance Bengali">Advance Bengali</option>
<option value="Physics">Physics</option>
<option value="Chemistry">Chemistry</option>
<option value="Biology">Biology</option>
<option value="Mathematics">Mathematics</option>
<option value="Computer Science & Applications">Computer Science & Applications</option>
<option value="Accountancy">Accountancy</option>
<option value="Business Studies">Business Studies</option>
<option value="Banking">Banking</option>
<option value="Statistics">Statistics</option>
</select></td>
</tr>
<tr>
<td>Elective-2</td>
<td><select name="Elective-2">
<option value="-1" selected>Select..</option>
<option value="MIL (Hindi)">MIL (Hindi)</option>
<option value="MIL (Bengali)">MIL (Bengali)</option>
<option value="MIL (Nepali)">MIL (Nepali)</option>
<option value="Political Science">Political Science</option>
<option value="Education">Education</option>
<option value="Economics">Economics</option>
<option value="History">History</option>
<option value="Geography">Geography</option>
<option value="Sociology">Sociology</option>
<option value="Logic & Philosophy">Logic & Philosophy</option>
<option value="Advance Assamese">Advance Assamese</option>
<option value="Advance Bengali">Advance Bengali</option>
<option value="Physics">Physics</option>
<option value="Chemistry">Chemistry</option>
<option value="Biology">Biology</option>
<option value="Mathematics">Mathematics</option>
<option value="Computer Science & Applications">Computer Science & Applications</option>
<option value="Accountancy">Accountancy</option>
<option value="Business Studies">Business Studies</option>
<option value="Banking">Banking</option>
<option value="Statistics">Statistics</option>
</select></td>
</tr>
<tr>
<td>Elective-3</td>
<td><select name="Elective-3">
<option value="-1" selected>Select..</option>
<option value="MIL (Hindi)">MIL (Hindi)</option>
<option value="MIL (Bengali)">MIL (Bengali)</option>
<option value="MIL (Nepali)">MIL (Nepali)</option>
<option value="Political Science">Political Science</option>
<option value="Education">Education</option>
<option value="Economics">Economics</option>
<option value="History">History</option>
<option value="Geography">Geography</option>
<option value="Sociology">Sociology</option>
<option value="Logic & Philosophy">Logic & Philosophy</option>
<option value="Advance Assamese">Advance Assamese</option>
<option value="Advance Bengali">Advance Bengali</option>
<option value="Physics">Physics</option>
<option value="Chemistry">Chemistry</option>
<option value="Biology">Biology</option>
<option value="Mathematics">Mathematics</option>
<option value="Computer Science & Applications">Computer Science & Applications</option>
<option value="Accountancy">Accountancy</option>
<option value="Business Studies">Business Studies</option>
<option value="Banking">Banking</option>
<option value="Statistics">Statistics</option>
</select></td>
</tr>
<tr>
<td>Elective-4</td>
<td><select name="Elective-4">
<option value="-1" selected>Select..</option>
<option value="MIL (Hindi)">MIL (Hindi)</option>
<option value="MIL (Bengali)">MIL (Bengali)</option>
<option value="MIL (Nepali)">MIL (Nepali)</option>
<option value="Political Science">Political Science</option>
<option value="Education">Education</option>
<option value="Economics">Economics</option>
<option value="History">History</option>
<option value="Geography">Geography</option>
<option value="Sociology">Sociology</option>
<option value="Logic & Philosophy">Logic & Philosophy</option>
<option value="Advance Assamese">Advance Assamese</option>
<option value="Advance Bengali">Advance Bengali</option>
<option value="Physics">Physics</option>
<option value="Chemistry">Chemistry</option>
<option value="Biology">Biology</option>
<option value="Mathematics">Mathematics</option>
<option value="Computer Science & Applications">Computer Science & Applications</option>
<option value="Accountancy">Accountancy</option>
<option value="Business Studies">Business Studies</option>
<option value="Banking">Banking</option>
<option value="Statistics">Statistics</option>
</select></td>
</tr>
<tr>
<td>Compulsory</td>
<td><select name="Compulsory">
<option value="-1" selected>Select..</option>
<option value="Environmental Education">Environmental Education</option>
</select></td>
</tr>
<tr>
<td>Institute</td>
<td><select name="Institute">
<option value="-1" selected>Select..</option>
<option value="Institute-1">Institute-1</option>
<option value="Institute-2">Institute-2</option>
<option value="Institute-3">Institute-3</option>
</select></td>
</tr>
<tr>
<td>State</td>
<td><select Name="State">
<option value="-1" selected>select..</option>
<option value="New Delhi">NEW DELHI</option>
<option value="Mumbai">MUMBAI</option>
<option value="Goa">GOA</option>
<option value="Bihar">BIHAR</option>
</select></td>
</tr>
<tr>
<td>PinCode</td>
<td><input type="text" name="pincode" id="pincode" size="30"></td>
</tr>
<tr>
<td>EmailId</td>
<td><input type="text" name="emailid" id="emailid" size="30"></td>
</tr>
<tr>
<td>DOB</td>
<td><input type="text" name="dob" id="dob" size="30"></td>
</tr>
<tr>
<td>MobileNo</td>
<td><input type="text" name="mobileno" id="mobileno" size="30"></td>
</tr>
<tr>
<td><input type="reset"></td>
<td colspan="2"><input type="submit" value="Submit Form" /></td>
</tr>
</table>
</form>
</body>
</html>
This post should get you where you need to go...
How to export html form to csv file
The answer there gives a good explanation on how the script works.
edit: You're going to need to create a PHP file and pass the data when you hit submit FORM should look like this notice the POST and .php file.
<form action="NAMEOFFILE.php" method="post">
NAMEOFFILE.PHP will contain the form data and the export to CSV script in the link attached. If are not sure how to pass the data this is a straightforward explanation. https://www.tutorialrepublic.com/php-tutorial/php-get-and-post.php
<?php
$fieldA = $_POST["textnames"];
$keys = array('id_number','first_name'); //THIS IS WHERE YOU PUT THE FORM ELEMENTS ex: array('$fieldA','$fieldB',etc)
$csv_line = array();
foreach($keys as $key){
array_push($csv_line,'' . $_GET[$key]);
}
$fname = 'file_to_write_to.csv'; //NAME OF THE FILE
$csv_line = implode(',',$csv_line);
if(!file_exists($fname)){$csv_line = "\r\n" . $csv_line;}
$fcon = fopen($fname,'a');
$fcontent = $csv_line;
fwrite($fcon,$csv_line);
fclose($fcon);
?>
The file will be saved on your server with the name "file_to_write_to.csv"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With