I am trying to create a form in which the user can enter data, and then, when they click the "Submit" button, this data will be added in a new line to the csv file stored on the server that I can upload. I tried both some php files and javascript, both of which seem to be good, but I am also not very experienced.
Here is the basic html form:
<form name="xyz_form">
<section class="border-bottom">
<div class="content">
<h3>ID Number</h3>
<div class="form-control-group">
<div class="form-control form-control-number">
<input type="number" id="id_number">
</div>
</div>
<h3>First Name</h3>
<div class="form-control-group">
<div class="form-control form-control-text">
<input type="text" id="first_name">
</div>
</div>
</div>
<section class="data-capture-buttons one-buttons">
<div class="content">
<input type="submit" value="Submit" onClick="javascript:addToCSVFile()">
</div>
</section>
Javascript is in the header of the html file, and it is:
<script type="text/javascript">
function addToCSVFile() {
var csvData = new Array();
var csvFilePath = "Data.csv";
csvData[0] = document.getElementById('id_number').value;
csvData[1] = document.getElementById('first_name').value;
var fso = new ActiveXObject('Scripting.FileSystemObject');
var oStream = fso.OpenTextFile(csvFilePath, 8, true, 0);
oStream.WriteLine(csvData.join(','));
oStream.Close();
clearData();
alert("Data Added Successfully");
}
function clearData() {
document.getElementById('id_number').value = "";
document.getElementById('first_name').value = "";
}
</script>
And right now I have it onClick="javascript:addToCSVFile()">, but it also doesn’t work when I set the form action to the handler.php and mothod file to publish and remove onlick. This is a php file.
<?
if(isset($_POST['match_scouting'])){
$del = "\t";
$data[1] = $_POST['id_number'];
$data[2] = $_POST['first_name'];
$file = fopen("Data.csv", "a");
$data = "\r\n".implode($del, $data);
fwrite($file, $data);
fclose($file);
echo "The data has been added successfully";
}else{
header("location: form.html");
}
?>
, ? , , , , , , SQL, .