如何通过PHP获取CSV中输入的表单值
我已经写了下面的代码,但它存储预定义的输入,我的要求是用户提供输入,并将其存储在CSV中 我不能用数据库来做同样的事情 我需要在哪里更改代码如何通过PHP获取CSV中输入的表单值,php,html,csv,Php,Html,Csv,我已经写了下面的代码,但它存储预定义的输入,我的要求是用户提供输入,并将其存储在CSV中 我不能用数据库来做同样的事情 我需要在哪里更改代码 <?php //read data from form $lName = filter_input(INPUT_POST, "lName"); $fName = filter_input(INPUT_POST, "fName"); $email = filter_input(INPUT_POST, "email"); $phone = f
<?php
//read data from form
$lName = filter_input(INPUT_POST, "lName");
$fName = filter_input(INPUT_POST, "fName");
$email = filter_input(INPUT_POST, "email");
$phone = filter_input(INPUT_POST, "phone");
//generate output for text file
$output = $fName . "t";
$output .= $lName . "t";
$output .= $email . "t";
$output .= $phone . "n";
你可以试试这个
<?php
$lName = filter_input(INPUT_POST, "lName");
$fName = filter_input(INPUT_POST, "fName");
$email = filter_input(INPUT_POST, "email");
$phone = filter_input(INPUT_POST, "phone");
$output = $fName . "t";
$output .= $lName . "t";
$output .= $email . "t";
$output .= $phone . "n";
$fp = fopen('file.csv', 'w');
fputcsv($fp, $output);
fclose($fp);
?>
简单按照我的步骤操作
1) 通过连接制表符和换行符构建字符串,如下所示
2) 使用带有附加模式的fopen
打开一个文件(如果文件不存在,它将使用您指定的名称创建一个新文件)
3) 使用fwrite
编写带有输出字符串的文件
4) 然后使用fclose
关闭文件
form_page.php
<html>
<head>
</head>
<body>
<form action="next_page.php" method="post" >
<input type="text" name="lName" />
<input type="text" name="fName" />
<input type="text" name="email" />
<input type="text" name="phone" />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
//read data from form
$lName = filter_input(INPUT_POST, "lName");
$fName = filter_input(INPUT_POST, "fName");
$email = filter_input(INPUT_POST, "email");
$phone = filter_input(INPUT_POST, "phone");
//generate output for text file
$output = $fName . "t";
$output .= $lName . "t";
$output .= $email . "t";
$output .= $phone . "n";
//here "t" means tab
//here "n" newline
//open file for output with append mode "a"
$fp = fopen("contacts.csv", "a");
//write to the file
fwrite($fp, $output);
fclose($fp);
}
else
{
echo "empty submit";
}
?>
next_page.php
<html>
<head>
</head>
<body>
<form action="next_page.php" method="post" >
<input type="text" name="lName" />
<input type="text" name="fName" />
<input type="text" name="email" />
<input type="text" name="phone" />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
//read data from form
$lName = filter_input(INPUT_POST, "lName");
$fName = filter_input(INPUT_POST, "fName");
$email = filter_input(INPUT_POST, "email");
$phone = filter_input(INPUT_POST, "phone");
//generate output for text file
$output = $fName . "t";
$output .= $lName . "t";
$output .= $email . "t";
$output .= $phone . "n";
//here "t" means tab
//here "n" newline
//open file for output with append mode "a"
$fp = fopen("contacts.csv", "a");
//write to the file
fwrite($fp, $output);
fclose($fp);
}
else
{
echo "empty submit";
}
?>
我希望用户以html格式输入详细信息,但以csv格式接收输出。您只需创建html表单@jayantkaushik。我现在唯一的问题是表单输入位于正在生成的csv中的单个单元格中。救命!!使用“\t”;而不是“t”;及“\n”;代替“n”@JayantKaushiki希望用户以html格式输入详细信息,但接收csv格式的输出。我希望用户以html格式输入详细信息,但接收csv格式的输出。