Currently I have a form that I can fill out that generates a csv file with a single row of data with multiple columns. The columns are my select elements in my HTML Form and the selection is applied to the row below the header. I am needing the ability to duplicate the select elements which I have but I am not sure how to have that populate a second,third,fourth, etc row in the csv.
Expected Results
HEADER
Name, Email, Customer, Reseller, ActivationDate, Bandwidth, DeploymentType, Location, Type, OperatingSystem
ROW2
NameInput, EmailInput, CustomerInput, ResellerInput, ActivationDateInput, BandwidthInput, DeploymentTypeInput, LocationInput, TypeInput, OperatingSystemInput
ROW3
NameInput, EmailInput, CustomerInput, ResellerInput, ActivationDateInput, BandwidthInput, DeploymentTypeInput, LocationInput, TypeInput, OperatingSystemInput
ROW4
NameInput, EmailInput, CustomerInput, ResellerInput, ActivationDateInput, BandwidthInput, DeploymentTypeInput, LocationInput, TypeInput, OperatingSystemInput
ETC
<div id="serveroptions">
<table id="server">
<tr id="header">
<td class="space">Deployment Location</td>
<td>Server Type</td>
<td>Server OS</td>
</tr>
<tr id="datainput">
<td><select name="location[]" class="dropdown" id="location">
<option value="" selected="selected">Select</option>
<option value="cavern">Cavern</option>
<option value="greencloud">Green Cloud</option>
<option value="onprem">On-Premise</option>
</select><br></td>
<td><select name="type[]" class="dropdown" id="type">
<option value="" selected="selected">Select</option>
<option value="dc">Domain Controller</option>
<option value="ca">CA</option>
<option value="app">Application</option>
<option value="sql">SQL</option>
<option value="rds">RDS</option>
<option value="exch">Exchange</option>
<option value="util">Utility</option>
<option value="other">Other</option>
</select><br></td>
<td><select name="os[]" class="dropdown" id="os">
<option value="" selected="selected">Select</option>
<option value="windows">Windows</option>
<option value="linux">Linux</option>
<option value="ova">OVA</option>
<option value="other">Other</option>
</select><br></td>
</tr>
</table>
</div>
<button type="button" id="btnAdd">Add Row</button></br></br>
</div>
<p><label class="submitbutton"><input type='submit' name='submit' value='Submit Form'></label></p>
</form>
</body>
if(isset($_POST["submit"])){
$data = array();
$data['name'] = $_POST["name"] ?? '';
$data['email'] = $_POST["email"] ?? '';
$data['customer'] = $_POST["customer"] ?? '';
$data['reseller'] = $_POST["reseller"] ?? '';
$data['activationdate'] = $_POST["activationdate"] ?? '';
$data['firewall'] = $_POST["bandwidth"] ?? '';
$data['deploytype'] = $_POST["deploytype"] ?? '';
$data['location'] = $_POST["location"] ?? '';
$data['type'] = $_POST["type"] ?? '';
$data['os'] = $_POST["os"] ?? '';
$errors = '';
foreach ($data as $key => $value) {
if ($key == 'type' || $key == 'os' || $key == 'deploytype'){ } //do nothing
else {
if (empty($value)) {
//Field is empty && email
if ($key == 'email') {
if(!filter_var($data['email'], FILTER_VALIDATE_EMAIL)){
$errors .= 'Please enter a valid email address <br> ';
}
} else {
//Field is empty && !email
$errors .= $key. ' field is required. <br> ';
}
}
}
}
//if no errors carry on
if(empty($errors)){
//# Title of the CSV
$Content = "Name, Email, Customer, Reseller, ActivationDate, Bandwidth, DeploymentType, Location, Type, OperatingSystem\n";
//set the data of the CSV
// Added '' in each field as a delimiter
$row = '';
foreach ($data as $key => $value){
# Appending value encased in quotes and trailing comma to row
$row .= '"'. $value. '",';
}
//Remove trailing comma
$row = mb_substr($row, 0, -1);
$Content .= $row;
//# set the file name and create CSV file
$timestamp = date("Y-m-d_H-i-s"); //$timestamp takes the current time
$myFile = $timestamp."_new-voice-deployment.csv"; // add timestamp to the file name
$csv_handler = fopen ($myFile,'a'); //changed 'w' to 'a'
if(fwrite ($csv_handler,$Content)){
echo 'Form Submission Successfull';
} else {
echo "Form Submission UnSuccessfull";
}
fclose ($csv_handler);
} else {
echo $errors;
}
}
Header to have select element options Rows to have selections made in the form
Continue the comments above, it seems you need to change just those two lines:
$myFile = $timestamp."_new-voice-deployment.csv"; // add timestamp to the file name
$csv_handler = fopen ($myFile,'w');
Into something like this:
$myFile = "data.csv"; // or any other filename...
$csv_handler = fopen ($myFile,'a'); // instead of w
Hope i finally understand..
Edit, i hope that (now) i finally understood your request (added comments on code):
<?php
if(isset($_POST["submit"])){
$data = array();
$data['name'] = $_POST["name"] ?? '';
$data['email'] = $_POST["email"] ?? '';
$data['customer'] = $_POST["customer"] ?? '';
$data['reseller'] = $_POST["reseller"] ?? '';
$data['activationdate'] = $_POST["activationdate"] ?? '';
$data['firewall'] = $_POST["bandwidth"] ?? '';
$data['deploytype'] = $_POST["deploytype"] ?? '';
$data['location'] = $_POST["location"] ?? '';
$data['type'] = $_POST["type"] ?? '';
$data['os'] = $_POST["os"] ?? '';
$errors = '';
foreach ($data as $key => $value) {
if ($key == 'type' || $key == 'os' || $key == 'deploytype'){ } //do nothing
else {
if (empty($value)) { //Field is empty && email
if ($key == 'email') {
if(!filter_var($data['email'], FILTER_VALIDATE_EMAIL)){
$errors .= 'Please enter a valid email address <br> ';
}
}
else { //Field is empty && !email
$errors .= $key. ' field is required. <br> ';
}
}
}
}
if(empty($errors)){ //if no errors carry on
$myFile = "data.csv"; // file name
if(!file_exists($myFile)) { // if the file not exist make header
$Content = "Name, Email, Customer, Reseller, ActivationDate, Bandwidth, DeploymentType, Location, Type, OperatingSystem\n"; // Title of the CSV
$csv_handler = fopen ($myFile,'a'); // changed 'w' to 'a'
fwrite ($csv_handler,$Content);
fclose ($csv_handler);
}
$row = '';
foreach ($data as $key => $value){
if (is_array($value)) { $value = implode($value); } // check if the value is array - if so convert it to string.
$row .= '"'. $value. '",'; // Appending value encased in quotes and trailing comma to row
}
$row = mb_substr($row, 0, -1); //Remove trailing comma
$timestamp = date("Y-m-d_H-i-s"); // optional - just to know when the line added to the file
$row .= "," . $timestamp;
$row .= "\n"; // add end of line so that new records would be under it
$csv_handler = fopen ($myFile,'a'); // changed 'w' to 'a'
if(fwrite ($csv_handler,$row)){ echo 'Form Submission Successfull'; }
else { echo "Form Submission UnSuccessfull"; }
fclose ($csv_handler);
}
else { echo $errors; }
}
?>
<form method="post" action="demo.php">
<div id="serveroptions">
<table id="server">
<tr id="header">
<td class="space">Deployment Location</td>
<td>Server Type</td>
<td>Server OS</td>
</tr>
<tr id="datainput">
<td>
<select name="location[]" class="dropdown" id="location">
<option value="" selected="selected">Select</option>
<option value="cavern">Cavern</option>
<option value="greencloud">Green Cloud</option>
<option value="onprem">On-Premise</option>
</select>
<br>
</td>
<td>
<select name="type[]" class="dropdown" id="type">
<option value="" selected="selected">Select</option>
<option value="dc">Domain Controller</option>
<option value="ca">CA</option>
<option value="app">Application</option>
<option value="sql">SQL</option>
<option value="rds">RDS</option>
<option value="exch">Exchange</option>
<option value="util">Utility</option>
<option value="other">Other</option>
</select>
<br>
</td>
<td>
<select name="os[]" class="dropdown" id="os">
<option value="" selected="selected">Select</option>
<option value="windows">Windows</option>
<option value="linux">Linux</option>
<option value="ova">OVA</option>
<option value="other">Other</option>
</select>
<br>
</td>
</tr>
</table>
<input type="text" name="name" placeholder="name" />
<input type="email" name="email" placeholder="email" />
<input type="text" name="customer" placeholder="customer" />
<input type="text" name="reseller" placeholder="reseller" />
<input type="text" name="activationdate" placeholder="activationdate" />
<input type="text" name="bandwidth" placeholder="firewall" />
<input type="text" name="deploytype" placeholder="deploytype" />
</div>
<button type="button" id="btnAdd">Add Row</button></br></br>
<p><label class="submitbutton"><input type='submit' name='submit' value='Submit Form'></label></p>
</form>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.