【问题标题】:Want to save all values of checkbox in csv (php), unfortunately only one gets saved想要在 csv (php) 中保存复选框的所有值,不幸的是只有一个被保存
【发布时间】:2018-08-13 16:17:28
【问题描述】:

我有一个表格,我正在将答案导出到一个 csv 文件中。单选按钮工作正常。 textarea 的值没有被导出,所有选中的复选框的值也没有被导出。只有一个值被导出。我做错了什么?请帮忙!

我的代码 index.php

    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', 0);
    include 'php/create-csv.php';
    ?>


    <!DOCTYPE html>
    <html lang="de">
    <body>
    <form method="post" action="" name="contactform">
    <div class="control-group">
     <label class="control control-checkbox"> Insurances            
      <input type="checkbox" value="insurances" name="topic[]"/>
      <div class="control_indicator"></div>
     </label>
     <label class="control control-checkbox"> Savings
      <input type="checkbox"  value="savings" name="topic[]"/>
      <div class="control_indicator"></div>
     </label>
     <label class="control control-checkbox"> Other
      <input type="checkbox" value="other_topic" name="topic[]"/>
      <div class="control_indicator"></div>            
     </label>
     <div id="textarea">
      <textarea rows="4" cols="40"  name="other"></textarea>
      </div>
     </div>
    <input type="submit" name="submit" value="Send" id="submit" 
    class="submit-btn">
     <?php
       if (isset($errors)) {
        foreach ($errors as $error) {
          echo $error;
        }
       }      
      ?>
   </form>

</body>

</html>

create-csv.php 的代码

<?php
  if (isset($_POST['submit'])) {
   $topic = isset($_POST['topic']) ? $_POST['topic'] : '';
   $rating = isset($_POST['rating']) ? $_POST['rating'] : '';
   $other = isset($_POST['other']) ? $_POST['other'] : '';

    if ($rating == '') {
        $errors[] = '<div class="notification error clearfix"><p>Please select a number.</p></div>';
    }
    if ($topic == '') {
     $errors[] = '<div class="notification error clearfix"><p>Please select at 
          least one topic.</p></div>';
    }  
    if (!isset($errors)) {

        if(!empty($_POST['topic'])) {    
            foreach($_POST['topic'] as $value){  
                echo $value; 
            }
        }

        $header = "Rating,Topics,Other\n";

        $data = "$rating, $topic, $other\n";
    $fileName = dirname(__DIR__) . "/results.csv";



            if (file_exists($fileName)) {

                file_put_contents($fileName, $data, FILE_APPEND);
            } else {

                file_put_contents($fileName, $header . $data);
            } 
             header("Location: thankyou.html");
            exit;


        }
    }

【问题讨论】:

  • 只导出一个值什么值?

标签: php forms csv checkbox export-to-csv


【解决方案1】:

首先,您发布的此表单中没有“评级”。也许是你之前提到的单选按钮。

此外,您似乎正在尝试将数组转换为字符串:

$data = "$rating, $topic, $other\n";

我建议您使用管道 (|) 内爆您的数组,如下所示:

$data = "$rating, " . implode ("|", $topic) . ", $other\n";

【讨论】:

  • 感谢您的快速回复。这对我有帮助!!!不知道有没有办法把textarea的值加到topic的列中?
  • 当然!如果您需要 $other 与 $topic 位于同一列,那么在分配 $data 的值之前,将 $other 作为新索引添加到 $topic 数组:这样 -> $topic[] = $other.不过要注意换行符。
【解决方案2】:

尝试更新

    if(!empty($_POST['topic'])) {    
        foreach($_POST['topic'] as $value){  
            echo $value; 
        }
    }

    if(!empty($_POST['topic'])) {    
        foreach($_POST['topic'] as $value){  
            $topic .= ', '.$value; 
        }
    }

【讨论】:

  • 关于您的代码,我试过了,但不幸的是我现在得到了一个空值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 2013-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多