我希望在ajax函数完成后调用另一个函数。成功注册用户后,我想要一个回调函数,但是当我尝试时,我的注册功能停止工作:
<?php
include('connection.php');
$firstname = "";
$lastname = "";
$email = "";
$contact = "";
$message = "";
$sql = "";
$table = "comments";
if (isset($_REQUEST['firstname']) && isset($_REQUEST['lastname']) && isset($_REQUEST['email']) && isset($_REQUEST['contact']) && isset($_REQUEST['message'])) {
//echo "Hi";
$firstname = "'" . $_REQUEST['firstname'] . "'";
$lastname = "'" . $_REQUEST['lastname'] . "'";
$email = "'" . $_REQUEST['email'] . "'";
$contact = $_REQUEST['contact'];
$message = "'" . $_REQUEST['message'] . "'";
$sql = "INSERT INTO {$table} VALUES (NULL,{$firstname},{$lastname},{$email},{$contact},{$message})";
if (mysqli_query($conn, $sql)) {
echo "<div class='alert alert-success' ><h4>Your message has successfully been sent.</h4></div>
<div class=\"text-right\">
<button class=\"btn btn-danger\" data-dismiss=\"modal\">Close</button>
</div>";
} else {
echo "<div class='alert alert-danger' ><h4> Message sending failed.</h4></div>
<div class=\"text-right\">
<button class=\"btn btn-danger\" data-dismiss=\"modal\">Close</button>
</div>";
}
} else {
echo "<div class='alert alert-danger' ><h4>Message sending failed.</h4></div>
<div class=\"text-right\">
<button class=\"btn btn-danger\" data-dismiss=\"modal\">Close</button>
</div>";
}
答案 0 :(得分:0)
编辑你的$(document).ready(function()..
不应该在另一个函数中,因为这样只会在调用该函数时调用它,并且可能不是你想要的那样,因为我不知道你如何使用signUp()函数,我无法真正说出来。
您可以使用这种方式制作ajax请求
function signUp(){
$(document).ready(function() {
$("#register").click(function() {
var name = $("#name").val();
var email = $("#email").val();
var password = $("#password").val();
var cpassword = $("#cpassword").val();
if (name == '' || email == '' || password == '' || cpassword == '') {
alert("Please fill all fields...!!!!!!");
} else if ((password.length) < 8) {
alert("Password should atleast 8 character in length...!!!!!!");
} else if (!(password).match(cpassword)) {
alert("Your passwords don't match. Try again?");
} else {
$.ajax({
type: "POST",
url: "http://localhost:5000/your/endpoint",
data: {
name1: name,
email1: email,
password1: password
},
success: function (data, status, xhr) {
// function called when it has succeeded
// Do what you want here in case of success
// The best way to do this should be used with statusCode because basically you
// only want to know that your post was OK.
},
statusCode: {
// Add more status code if you want to do something for each
200: function(){
$("form")[0].reset();
alert('You have Successfully Registered.....');
},
500: function(){
// In case of server error you should do something also
}
}
error: function(xhr, msg, err){
// Function called when error happens
}
})
}
});
});
}
您甚至可以定义在给定状态代码作为响应时调用的函数,您可以在http://api.jquery.com/jquery.ajax/上找到完整的参考