php - 发送数据按钮在 PHP 上不起作用
问题描述
为了测试将用户输入到 html 中的数据并将其放在屏幕上的代码,我制作了一个 HTML 代码来获取用户在某些字段中填写的数据,并在用户单击按钮后发送数据出现在屏幕上,数据不发送。也没有出现任何错误消息。
请问,谁能帮帮我?
index.php 类是主类,其中包含 html 和引导代码。model.php 类提供了将数据发送到屏幕上的代码。
index.php 的类
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
<title>Hello, world!</title>
</head>
<body>
<div class = "container">
<div class = "row">
<div class = "col-md-12 mt-5">
<h1 class = "text-center">PHP OOP CRUD TUTORIAL</h1>
<hr style = "height: 1px; color: black; background-color:black;">
</div>
</div>
<div class = "row">
<div class = "col-md-5 mx-auto">
<?php
include 'model.php';
$model = new Model();
$insert = $model->insert();
?>
<form action = "" method = "">
<div class = "form-group">
<label for = "">Name</label>
<input type = "text" name = "name" class = "form-control">
</div>
<div class = "form-group">
<label for = "">Email</label>
<input type = "email" name = "email" class = "form-control">
</div>
<div class = "form-group">
<label for = "">Mobile No.</label>
<input type = "text" name = "mobile" class = "form-control">
</div>
<div class = "form-group">
<label for = "">Address</label>
<textarea name ="address" id = "" cols = "" rows = "3" class = "form-control"></textarea>
<br />
</div>
<div class = "form-group">
<button type = "submit" name = "submit" class = "btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
</body>
</html>```
Class of model.php
```<?php
class Model {
private $server = "localhost";
private $username ="root";
private $password;
private $db = "crud";
private $conn;
public function __construct(){
try {
$this->conn = new mysqli($this->server, $this->username, $this->password, $this->db);
} catch (Exception $e){
echo "Connection failed". $e->getMessage();
}
}
public function insert() {
if(isset($_POST['submit'])) {
if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['mobile']) && isset($_POST['address'])) {
if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['mobile']) && !empty($_POST['address'])) {
echo $name = $_POST['name'];
echo $mobile = $_POST['mobile'];
echo $email = $_POST['email'];
echo $address= $_POST['address'];
} else {
echo "<script>alert('empty');</script>";
}
}
}
}
}
?>```
解决方案
推荐阅读
- javascript - 如何动态编辑影响另一个表格单元格的表格单元格?
- magento2 - 根据当前商店 magento 翻译商店视图名称
- python - 使用 openCV 从轮廓构建嵌套蒙版
- java - 我应该总是覆盖equals、hashcode和toString方法吗?
- react-native - 在按钮单击时加载组件并将数据传递给它
- c# - 如何使 MySql 和 EF Core 将 tinyint 或 bit 映射为布尔值?
- python - 比较 Python Selenium 中的类
- haproxy - HAProxy 在 404 上的不同后端重试请求
- r - R:查找具有最高值的列
- c# - 集成测试期间的 ASP.NET Core 3 模拟授权