gpt4 book ai didi

javascript - 使用Ajax和PHP插入数据库(mysql)

转载 作者:行者123 更新时间:2023-11-29 06:10:08 24 4
gpt4 key购买 nike

我试图将表单中的数据插入到mysql数据库中。我认为问题在于,我在哪里使用HTML中的按钮,因此我复制了所有的按钮。任何帮助都将不胜感激!
当我按下submit按钮时,页面会闪烁,数据库中没有任何内容。它应该显示一个绿色的框,表示记录已经在html页面上提交。
因为有些人更担心我建立一个认证系统,然后什么错了。这不是一个身份验证系统,它只是一个如何插入mysql数据库的示例。
Index.html索引

<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example with Ajax</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="js/insert.js"></script>

<style>
.custom{
margin-left:200px;
}
</style>
</head>
<body>

<div class="container">
<h2 class="text-center">Insert Data Using Ajax</h2>

<br/>
<p id="alert" style="display:none;" class="alert alert-success text-center"><i class="glyphicon glyphicon-ok"></i><span> id="show"</span></p>
<br/>
<hr/>
<form class="form-horizontal" role="form" method="POST">
<div class="form-group">
<label class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input class="form-control" id="name" type="text" placeholder="Enter you name">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input class="form-control" id="email" type="text" placeholder="Your Email...">
</div>
</div>
<fieldset >
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<input class="form-control" id="password" type="text" placeholder="Your Password...">
</div>
</div>
<div class="form-group">
<label for="gender" class="col-sm-2 control-label">Gender</label>
<div class="col-sm-10">
<select id="gender" class="form-control">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
</div>
</body>
</html>



插入.php
<?php
//Create connection
$connection = mysqli_connect('localhost','username','passwd','dbName');

if($_REQUEST['name']){
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$password= $_REQUEST['password'];
$gender = $_REQUEST['gender'];

$q = "INSERT INTO user VALUES ('','$name', '$email', '$password', '$gender')";

$query = mysqli_query($connection,$q);
if($query){
echo ' Data Inserted Successfully'
mysql_close($connection);
}
}
?>

js/插入.js
$(document).ready(function(e) {
$('#submit').click(function(){
var name = $('#name').val();
var email = $('#email').val();
var password = $('#password').val();
var gender = $('#gender').val();

$ajax({
type:'POST',
data:{name:name,email:email,password:password,gender:gender},
url:"insert.php", //php page URL where we post this data to save in databse
success: function(result){

$('#alert').show();

$('#show').html(result);


}
})
});
});

最佳答案

不管怎样,这个特殊的代码允许插入到数据库中,尽管在某些地方仍然有一些问题我无法发现。
index.html索引

<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example with Ajax</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script>
$(function () {
$('button').click(function () {
var name2 = $('#name').val();
var email2 = $('#email').val();
var password2 = $('#password').val();
var gender2 = $('#gender').val();
console.log('starting ajax');
$.ajax({
url: "./insert.php",
type: "post",
data: { name: name2, email: email2, password: password2, gender: gender2 },
success: function (data) {
var dataParsed = JSON.parse(data);
console.log(dataParsed);
}
});

});
});

</script>

<style>
.custom{
margin-left:200px;
}
</style>
</head>
<body>

<div class="container">
<h2 class="text-center">Insert Data Using Ajax</h2>

<form class="form-horizontal" >
<div class="form-group">
<label class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input class="form-control" name="name" id="name" type="text" placeholder="Enter you name">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input class="form-control" name="email" id="email" type="text" placeholder="Your Email...">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<input class="form-control" name="password" id="password" type="text" placeholder="Your Password...">
</div>
</div>
<div class="form-group">
<label for="gender" class="col-sm-2 control-label">Gender</label>
<div class="col-sm-10">
<select id="gender" class="form-control">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
</div>
</body>
</html>

插入.php
<?php

//Create connection
$connection = mysqli_connect('localhost', 'root', '', 'dbase');
if($_POST['name']){
$name = $_POST['name'];
$email = $_POST['email'];
$password= $_POST['password'];
$gender = $_POST['gender'];

$q = "INSERT INTO user (name, email, password, gender) VALUES ('$name', '$email', '$password', '$gender')";

$query = mysqli_query($connection, $q);

if($query){
echo json_encode("Data Inserted Successfully");
}
else {
echo json_encode('problem');
}
}

?>

关于javascript - 使用Ajax和PHP插入数据库(mysql),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38794652/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com