php - 需要帮助以使用 php 从 .csv 获取数据到 html 页面
问题描述
我在 PHP 中创建了一个表单,我在其中插入数据并保存在 .csv 文件中,现在我想使用 page 将 .csv 数据显示到 HTML 页面中,但我不知道该怎么做。这是我在 PHP 中的表单代码。
<?php
$error = '';
$name = '';
$email = '';
$number = '';
$gender = '';
$country = '';
$birthdate='';
$address = '';
$education = '';
function clean_text($string)
{
$string = trim($string);
$string = stripslashes($string);
$string = htmlspecialchars($string);
return $string;
}
if(isset($_POST["submit"]))
{
if(empty($_POST["name"]))
{
$error .= '<p><label class="text-danger">Please Enter your Name</label></p>';
}
else
{
$name = clean_text($_POST["name"]);
if(!preg_match("/^[a-zA-Z ]*$/",$name))
{
$error .= '<p><label class="text-danger">Only letters and white space allowed</label></p>';
}
}
if(empty($_POST["email"]))
{
$error .= '<p><label class="text-danger">Please Enter your Email</label></p>';
}
else
{
$email = clean_text($_POST["email"]);
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$error .= '<p><label class="text-danger">Invalid email format</label></p>';
}
}
if(empty($_POST["number"]))
{
$error .= '<p><label class="text-danger">Number is required</label></p>';
}
else
{
$number = clean_text($_POST["number"]);
}
if(empty($_POST["gender"]))
{
$error .= '<p><label class="text-danger">Gender is required</label></p>';
}
else
{
$gender = clean_text($_POST["gender"]);
}
if(empty($_POST["country"]))
{
$error .= '<p><label class="text-danger">Country is required</label></p>';
}
else
{
$country = clean_text($_POST["country"]);
}
if(empty($_POST["birthdate"]))
{
$error .= '<p><label class="text-danger">Date of birth is required</label></p>';
}
else
{
$birthdate = clean_text($_POST["birthdate"]);
}
if(empty($_POST["address"]))
{
$error .= '<p><label class="text-danger">Address is required</label></p>';
}
else
{
$address = clean_text($_POST["address"]);
}
if(empty($_POST["education"]))
{
$error .= '<p><label class="text-danger">Educational Background is required</label></p>';
}
else
{
$education = implode(' ',$_POST['education'] );
// $education = clean_text($_POST["education"]);
}
if($error == '')
{
$file_open = fopen("test.csv", "a");
$no_rows = count(file("test.csv"));
if($no_rows > 1)
{
$no_rows = ($no_rows - 1) + 1;
}
$form_data = array(
'sr_no' => $no_rows,
'name' => $name,
'email' => $email,
'number' => $number,
'gender' => $gender,
'country' => $country,
'birthdate' => $birthdate,
'address' => $address,
'education' => $education
);
fputcsv($file_open, $form_data);
header('location:index_load.php');
}
}
?>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<br />
<div class="container">
<br />
<div class="col-md-6" style="margin:0 auto; float:none;">
<form method="post">
<h3 align="center">Please fill the Form</h3>
<br />
<?php echo $error; ?>
<div class="form-group">
<label>Enter Name:</label>
<input type="text" name="name" placeholder="Enter Name" class="form-control" value="<?php echo $name; ?>" />
</div>
<div class="form-group">
<label>Enter Email:</label>
<input type="text" name="email" class="form-control" placeholder="Enter Email" value="<?php echo $email; ?>" />
</div>
<div class="form-group">
<label>Mobile Number:</label>
<input type="text" name="number" class="form-control" placeholder="Enter number" maxlength="255" value="<?php echo $number ?>"/><br>
<div class="form-group">
<label>Gender:</label>  
<span>
<input type="radio" name="gender" value="male"/>
<label class="choice">Male</label> 
<input type="radio" name="gender" value="female"/>
<label class="choice">Female</label>
</span>
<div class="form-group">
<label for="country">Country:</label>
<select class="form-control" id="country" name="country">
<option value="" selected="selected"></option>
<option value="Canada">Canada</option>
<option value="France">France</option>
<option value="Germany">Germany</option>
<option value="India">India</option>
</select>
</div>
<div class="form-group">
<label>Date of birth:</label>
<input type="date" id="birthdate" name="birthdate" value="<?php echo $birthdate ?>">
</div>
</div>
<div class="form-group">
<label>Enter Address:</label>
<textarea name="address" class="form-control" placeholder="Enter address"><?php echo $address; ?></textarea>
</div>
<div class="form-group">
<label>Education Background:</label><br>
<input type="checkbox" name="education[]" value="10th">10th<br>
<input type="checkbox" name="education[]" value="12th">12th<br>
<input type="checkbox" name="education[]" value="B.COM">B.COM<br>
<input type="checkbox" name="education[]" value="B.A">B.A<br>
<input type="checkbox" name="education[]" value="BSC">BSC
</div>
<div class="form-group" align="center">
<input type="submit" name="submit" class="btn btn-info" value="Submit" />
<a href="index.php" class="btn btn-default">Cancel</a>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
上面的代码以 .csv 格式存储数据,现在我想使用 php 将 .csv 文件数据显示到 html 页面中。谁能帮我怎么办?
解决方案
推荐阅读
- c# - ExchangeService.FindItems()
- sql-server - 在 SQLServer 中将特定日期格式转换为字符串
- jenkins - 将声纳结果输出到不同的服务器位置
- cloud-foundry - 如何在 CloudFoundry 中向 php-buildpack 添加其他依赖项
- python - 使用 re.findall() 查找从 A 到 Z 的所有字母时出错
- swift - 如何在 RxAlamofire 中使用 Array 作为参数而不是 Dictionary 发布请求?
- vba - 如何重复执行 CommandButton1 直到满足条件?
- lotus-notes - 调度代理未在 Domino 服务器上启动
- gulp - 管理文件包括在 gulp 中针对不同环境的不同
- amazon-web-services - 如何从端点 URL 中找到特定的 API 网关/lambda