【问题标题】:PHP Form not creating .csv filePHP 表单未创建 .csv 文件
【发布时间】:2019-05-29 20:14:25
【问题描述】:

尝试填写表单并在每次提交时生成单独的 .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 文件拖放到服务器,并让用户知道表单已成功提交。

【问题讨论】:

标签: php html forms post


【解决方案1】:

这是成功的

<?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>

【讨论】:

  • 这很棒。我看到的唯一问题是“录制”和“防火墙”选项是必需的,它们不应该是。如何排除这些选项?以下是它们在表单中的设置方式。 &lt;p&gt;&lt;label class="checkbox"&gt;Call Recording?&lt;br&gt;&lt;input type='checkbox' name='recording' value=''&gt;&lt;span class="checkmark"&gt;&lt;/span&gt;&lt;/label&gt;&lt;/p&gt; &lt;p&gt;&lt;label class="checkbox"&gt;Firewall?&lt;br&gt;&lt;input type='checkbox' name='firewall' value=''&gt;&lt;span class="checkmark"&gt;&lt;/span&gt;&lt;/label&gt;&lt;/p&gt;
  • @quigleyvision 对于非必填字段,如果您不想在第一个 foreach 循环中手动编写 if 语句,您可以将 N/A 放入后备分配中? '不适用'
  • 在数组之前有这样的东西吗? if (isset($_POST['recording'])) { $recording = "Yes"; } else { $recording = "No";
  • 感谢这有很大帮助!我确实将文件输出更改为包含时间戳,因此每次填写时都会创建多个文件。 $timestamp = date("Y-m-d_H-i-s"); //$timestamp takes the current time $myFile = "new_deployment.".$timestamp.".csv"; // add timestamp to the file name $csv_handler = fopen ($myFile,'w');
猜你喜欢
  • 2013-03-08
  • 1970-01-01
  • 2016-12-06
  • 1970-01-01
  • 2018-07-16
  • 2015-05-10
  • 2015-03-09
  • 2022-01-11
相关资源
最近更新 更多