尝试填写表单,并在每次提交时生成单独的.csv文件。填写表单后,页面会重新加载,但当前不会生成文件。
我知道还有很多其他方法可以做到这一点,但是我对此很陌生,这样我可以比其他人更好地缠头。
<?php
if(isset($_POST["submit"])){
//collect form data
$name = $_POST["name"];
$email = $_POST["email"];
$customer = $_POST["customer"];
$reseller = $_POST["reseller"];
$standardusers = $_POST["standardusers"];
$ucusers = $_POST["ucusers"];
$recording = $_POST["recording"];
$firewall = $_POST["firewall"];
//check name is set
if($name ==''){
$error[] = 'Name is required';
}
//check for a valid email address
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$error[] = 'Please enter a valid email address';
}
//check customer name is set
if($customer ==''){
$error[] = 'Customer Name is required and must match ConnectWise';
}
//check reseller name is set
if($reseller ==''){
$error[] = 'Reseller Name is required and must match ConnectWise';
}
//check standardusers is set
if($standardusers ==''){
$error[] = 'Number of users is required';
}
//check ucusers is set
if($ucusers ==''){
$error[] = 'Number of users is required';
}
//if no errors carry on
if(!isset($error)){
//# Title of the CSV
$Content = "Name, Email, Customer, Reseller, Standard Users, UC Users, Call Recording, Firewall\n";
//set the data of the CSV
$Content .= "$name, $email, $customer, $reseller, $standardusers, $ucusers, $recording, $firewall\n";
//# set the file name and create CSV file
$filename = "formdata-".date("d-m-y-h:i:s").".csv";
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="'.$FileName.'";');
echo $Content;
echo gethostname();
exit();
}
}
?>
提交表单后,应将.csv文件拖放到服务器上,并让用户知道表单已成功提交。
答案 0 :(得分:0)
此操作成功
<?php
if(isset($_POST["submit"])){
$data = array();
$data['name'] = $_POST["name"] ?? '';
$data['email'] = $_POST["email"] ?? '';
$data['customer'] = $_POST["customer"] ?? '';
$data['reseller'] = $_POST["reseller"] ?? '';
$data['standardusers'] = $_POST["standardusers"] ?? '';
$data['ucusers'] = $_POST["ucusers"] ?? '';
$data['recording'] = $_POST["recording"] ?? '';
$data['firewall'] = $_POST["firewall"] ?? '';
$errors = '';
foreach ($data as $key => $value) {
if ($key == 'recording' || $key == 'firewall'){ } //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 \r\n ';
}
} else {
//Field is empty && !email
$errors .= $key.' field is required. \r\n ';
}
}
}
}
//if no errors carry on
if(empty($errors)){
//# Title of the CSV
$Content = "Name, Email, Customer, Reseller, Standard Users, UC Users, Call Recording, Firewall\n";
//set the data of the CSV
// Added '' in each field as a delimiter
// $Content .= "'".$data['name']."', '".$data['email']."', '".$data['customer']."', '".$data['reseller']."', '".$data['standardusers']."', '".$data['ucusers']."', '".$data['recording']."', '".$data['firewall']."'\n";
$row = '';
foreach ($data as $key => $value){
$row .= $value.","; //Append data to csv
}
//Remove trailing comma
$row = mb_substr($row, 0, -1);
$Content .= $row;
//# set the file name and create CSV file
// $filename = "formdata-".date("d-m-y-h:i:s").".csv";
// header('Content-Type: application/csv');
// header('Content-Disposition: attachment; filename="'.$FileName.'";');
// echo $Content;
// echo gethostname();
// exit();
$csv_handler = fopen ('csvfile.csv','w');
if(fwrite ($csv_handler,$Content)){
echo 'Data saved to csvfile.csv';
} else {
echo "File not saved successfully";
}
fclose ($csv_handler);
} else {
echo $errors;
}
}
?>
我使用此表单进行了测试,并发布到了脚本中
<form class="" action="3.php" method="post">
<input type="text" name="name" placeholder="name" >
<input type="text" name="email" placeholder="email" >
<input type="text" name="customer" placeholder="customer" >
<input type="text" name="reseller" placeholder="reseller" >
<input type="text" name="standardusers" placeholder="standardusers" >
<input type="text" name="ucusers" placeholder="ucusers" >
<input type="text" name="recording" placeholder="recording" >
<input type="text" name="firewall" placeholder="firewall" >
<input type="submit" name="submit" value="submit">
</form>