要将复选框的所有值保存在csv(php)中,不幸的是只保存了一个值
我有一个表格,我正在将答案导出到csv文件中。单选按钮工作正常。不会导出textarea的值,也不会导出所有选定复选框的值。只导出一个值。我做错了什么?请帮忙 My code index.php要将复选框的所有值保存在csv(php)中,不幸的是只保存了一个值,php,forms,csv,checkbox,export-to-csv,Php,Forms,Csv,Checkbox,Export To Csv,我有一个表格,我正在将答案导出到csv文件中。单选按钮工作正常。不会导出textarea的值,也不会导出所有选定复选框的值。只导出一个值。我做错了什么?请帮忙 My code index.php <?php error_reporting(E_ALL); ini_set('display_errors', 0); include 'php/create-csv.php'; ?> <!DOCTYPE html> &
<?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;
}
}
尝试更新
if(!empty($_POST['topic'])) {
foreach($_POST['topic'] as $value){
echo $value;
}
}
到
首先,您发布的表单中没有“评级”。也许是你之前提到的单选按钮
此外,您似乎正在尝试将数组转换为字符串:
$data = "$rating, $topic, $other\n";
我建议您使用管道(|)内爆阵列,如下所示:
$data = "$rating, " . implode ("|", $topic) . ", $other\n";
只有一个值被导出什么值?关于你的代码,我试过了,但不幸的是我现在得到了一个空值。谢谢你的回复。这对我有帮助!!!你知道有没有办法将文本区域的值添加到主题的列中吗?当然!如果需要$other与$topic位于同一列中,则在分配$data的值之前,将$other添加为$topic数组的新索引:这样->$topic[]=$other。不过要小心线路断了。
$data = "$rating, " . implode ("|", $topic) . ", $other\n";