html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
<div>
<form action="baoming.php" method="post">
<!-- <form action="baoming.php" method="post"> -->
firstname: <input type="text" name="firstname" id="firstname">
lastname:<input type="text" name="lastname" id="lastname">
email: <input type="text" name="email" id="email">
<input type="submit" value="提交">
</form>
</div>
</body>
<script src="./ajax.js"></script>
</html>
<?php
$servername = "localhost";
$username = "yiban";
$password = "123456";
$dbname = "localhost";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('$firstname','$lastname','$email')";
if (mysqli_query($conn, $sql)) {
echo "新记录插入成功";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
var firstname = document.getElementById('firstname').value;
var lastname = document.getElementById('lastname').value;
var email = document.getElementById('email').value;
$.ajax({
type: "POST",
url: './new/baoming.php',
data: : "key = name&key2 = age&key3 = email",
success: function(data){alert(data);}
});
我用ajax进行了前后端的交互,页面显示数据插入成功,但是打开数据库后数据库表格内容为空值。。。。。。
?
|