I have a basic signup/ login page that submits the data to the SQL database with php. However, I would like the page not to redirect on submission with help of jQuery AJAX (either successful or not).
我有一个基本的注册/登录页面,用PHP将数据提交到SQL数据库。但是,我希望页面不要在jQuery AJAX的帮助下重定向(无论是否成功)。
This is what I currently have and it is not working. It doesn't show any error messages.
这就是我目前所拥有的,它无法正常工作。它不显示任何错误消息。
HTML - signup.html
HTML - signup.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Signup</title>
<meta charset="utf-8">
</head>
<body>
<form>
<table>
<tbody>
<tr>
<td>
<input type="text" name="first" placeholder="First Name" id="first">
</td>
</tr>
<tr>
<td>
<input type="text" name="last" placeholder="Last Name" id="last">
</td>
</tr>
<tr>
<td>
<input type="submit" value="Signup" id="signup">
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
JavaScript - signup.js
JavaScript - signup.js
function submit() {
$("form").submit(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'signup.php',
data: $('form').serialize(),
success: function() {
console.log("Signup was successful");
},
error: function() {
console.log("Signup was unsuccessful");
}
});
});
}
$(document).ready(function() {
submit();
});
PHP - signup.php
PHP - signup.php
<?php
include_once "db_connect.php";
$post_FirstName = $_POST['first'];
$post_LastName = $_POST['last'];
$addData = "INSERT INTO details (firstname, lastname) VALUES ('$post_FirstName', '$post_LastName')";
if ($conn->query($addData) === TRUE) {
echo "Working";
} else {
echo "Not working";
}
?>
Here is the JSFiddle.
这是JSFiddle。
I hope you guys can help. Thanks in advance :)
我希望你们能提供帮助。提前致谢 :)
3 个解决方案
#1
2
If you are using ajax no need to use input type as submit
use button
.
如果您使用的是ajax,则无需使用输入类型作为提交使用按钮。
$(document).ready(function() {
$("#signup").click(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'signup.php',
data: $('form').serialize()
success: function() {
console.log("Signup was successful");
}
error: function() {
console.log("Signup was unsuccessful");
}
});
});
Also change here
也在这里改变
$post_FirstName = $_POST['first']; // name is `first` not `firstname`
#2
3
You have some brakes and parentheses not properly closed
你有一些刹车和括号没有正确关闭
function submit() {
$("form").submit(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'signup.php',
data: $('form').serialize(),
success: function() {
console.log("Signup was successful");
},//here
error: function() {
console.log("Signup was unsuccessful");
}
});});//here
}
$(document).ready(function() {
submit();
});
#3
2
No need to call submit
function. Just this will do, (you missed comma and closing tag):
无需调用提交功能。只是这样做,(你错过了逗号和结束标记):
<script>
$("form").submit(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'signup.php',
data: $('form').serialize(),
success: function() {
console.log("Signup was successful");
}, //You missed this
error: function() {
console.log("Signup was unsuccessful");
}
});
}); //You missed this
</script>
#1
2
If you are using ajax no need to use input type as submit
use button
.
如果您使用的是ajax,则无需使用输入类型作为提交使用按钮。
$(document).ready(function() {
$("#signup").click(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'signup.php',
data: $('form').serialize()
success: function() {
console.log("Signup was successful");
}
error: function() {
console.log("Signup was unsuccessful");
}
});
});
Also change here
也在这里改变
$post_FirstName = $_POST['first']; // name is `first` not `firstname`
#2
3
You have some brakes and parentheses not properly closed
你有一些刹车和括号没有正确关闭
function submit() {
$("form").submit(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'signup.php',
data: $('form').serialize(),
success: function() {
console.log("Signup was successful");
},//here
error: function() {
console.log("Signup was unsuccessful");
}
});});//here
}
$(document).ready(function() {
submit();
});
#3
2
No need to call submit
function. Just this will do, (you missed comma and closing tag):
无需调用提交功能。只是这样做,(你错过了逗号和结束标记):
<script>
$("form").submit(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'signup.php',
data: $('form').serialize(),
success: function() {
console.log("Signup was successful");
}, //You missed this
error: function() {
console.log("Signup was unsuccessful");
}
});
}); //You missed this
</script>