您好,我在收到ajax请求的回复时遇到了问题,我已多次这样做但我不知道现在的问题是什么,我一直在拉我的头发现在在这一天,我曾尝试使用完整,错误或.done,没有任何作用。我正在做的是通过ajax插入数据,然后在插入数据时得到响应,现在数据插入正常,但我没有得到响应,请你帮我,没有控制台错误。
insert_ajax.php
<?php
?>
<html>
<head>
<title>Basic Ajax Insert</title>
<link rel="stylesheet" href="style/style.css" type="text/css"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" type="text/css"/>
<script src="jquery-1.3.1.min.js"></script>
<script src="custom.js"></script>
</head>
<body>
<div class="container">
<div id="succ"></div>
<h2 class="text-warning">Basic Information</h2>
<form id="form" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" autocomplete="off">
<div class="form-group col-md-6">
<label class="text-warning">First Name: </label>
<input class="form-control first_name" type="text" name="fname" />
</div>
<div class="form-group col-md-6">
<label class="text-warning">Last Name: </label>
<input class="form-control last_name" type="text" name="lname" />
</div>
<div class="form-group col-md-6">
<label class="text-warning">Email: </label>
<input class="form-control email" type="text" name="email" />
</div>
<div class="form-group col-md-6">
<label class="text-warning">Password: </label>
<input class="form-control pwd" type="password" name="pass" />
</div>
<button class="btn btn-warning" name="btn_sub">Send</button>
</form>
</div>
</body>
</html>
ajax.php
<?php
$con=mysqli_connect("localhost","root","","practices");
if(isset($_REQUEST['em'])){
echo'email received..: '.$_REQUEST['em'];
}
$fname=$_REQUEST["fn"];
$lname=$_REQUEST["ln"];
$email=$_REQUEST["em"];
$pass=$_REQUEST["pwd"];
//echo "returned";
$ins="insert into `user` (`first_name`,`last_name`,`email`,`password`)
values('$fname','$lname','$email','$pass')";
$query=mysqli_query($con,$ins);
if($query){
echo "Data is entered Love";
}
?>
custom.js
$(document).ready(function(){
$(".btn").click(function(){
var fname=$(".first_name").val();
var lname=$(".last_name").val();
var email=$(".email").val();
var pass=$(".pwd").val();
$.ajax({
method: "POST",
url : "ajax.php",
data: {fn:fname,ln:lname,em:email,pwd:pass},
success: function(r){
alert(r);
}
});
});
});
答案 0 :(得分:1)
更改为这些行后尝试
$(".btn").on("click",function(e){
e.preventDefault();
var fname=$(".first_name").val();
var lname=$(".last_name").val();
var email=$(".email").val();
var pass=$(".pwd").val();
$.ajax({
method: "POST",
url : "ajax.php",
data: {fn:fname,ln:lname,em:email,pwd:pass},
success: function(ind,val){
console.log(ind);
console.log(val);
}
});
答案 1 :(得分:0)
相反如何:
$.post("aqjax.php",{fn:fname,ln:lname,em:email,pwd:pass})
.done(function(data)
{
//Do stuff here
})