gpt4 book ai didi

JavaScript 函数 :signup() not sumbitting to ajax

转载 作者:行者123 更新时间:2023-11-30 14:45:26 26 4
gpt4 key购买 nike

Ajax 新手,开始使用 Javascript 和 jQuery。

我有一个以提交按钮结尾的表单,JavaScript 通过一组 function signup(){ 将验证字段和其他要求。到这里为止一切都很好。然而,一旦所有过程都正确完成并且我到了最后,Javascript 应该将信息发送到 Ajax 进行验证并插入到数据库中:此时链接不起作用,我得到一个错误告诉我 AjaxObj 未定义。我知道这属于某个我似乎找不到的 ajax js 库。如果我没记错的话,这即使不是相同的 Xttp 处理也是相似的 - 但我不确定这将如何适用于此。

在我的 js 方面,我在注册函数的最后一个 else 语句中有以下内容:

$("#signupbtn").css("display","none");
document.getElementById("status").innerHTML = 'Processing...'; // up to here all OK //

var xhttp = new XMLHttpRequest();
var ajax = xhttp.open("POST", "FLClubRegisterAction.php", true);
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
if(ajax.responseText != "signup_success"){
document.getElementById("status").innerHTML.innerHTML = ajax.responseText;
$("#signupbtn").css("display","block");
} else {
window.scrollTo(0,0);
document.getElementById("signupform").innerHTML = "Congratulations "+fname+" "+lname+", you are one step closer to activating your account. We have sent you an email containing an activation link. It is required that you follow this link in order to enable your online access. Thank you!";
}
}
};
ajax.send("username="+username+"&email="+email+"&password="+password+"&prefix="+prefix+"&fname="+fname+"&mname="+mname+"&lname="+lname+"&gender="+gender+"&dob="+dob+"&address1="+address1+"&address2="+address2+"&city="+city+"&state="+state+"&zip="+zip+"&secquest1="+secquest1+"&secansw1="+secansw1+"&emailpromo="+emailpromo);
}

PHP代码是:

if(isset($_POST["username"])){
// CONNECT TO THE DATABASE
include_once("/US/en/local/ressources/php/connection.php");
// GATHER THE POSTED DATA INTO LOCAL VARIABLES
$username = preg_replace('#[^a-z0-9]#i', '', $_POST['username']);
$email = mysqli_real_escape_string($db_conx, $_POST['email']);
$password = $_POST['password'];
$prefix = mysqli_real_escape_string($db_conx, $_POST['prefix']);
$fname = mysqli_real_escape_string($db_conx, $_POST['fname']);
$mname = mysqli_real_escape_string($db_conx, $_POST['mname']);
$lname = mysqli_real_escape_string($db_conx, $_POST['lname']);
$gender = mysqli_real_escape_string($db_conx, $_POST['gender']);
$dob = mysqli_real_escape_string($db_conx, $_POST['dob']);
$address1 = mysqli_real_escape_string($db_conx, $_POST['address1']);
$address2 = mysqli_real_escape_string($db_conx, $_POST['address2']);
$city = mysqli_real_escape_string($db_conx, $_POST['city']);
$state = mysqli_real_escape_string($db_conx, $_POST['state']);
$zip = mysqli_real_escape_string($db_conx, $_POST['zip']);
$secquest1 = mysqli_real_escape_string($db_conx, $_POST['secquest1']);
$secansw1 = mysqli_real_escape_string($db_conx, $_POST['secansw1']);
$emailpromo = mysqli_real_escape_string($db_conx, $_POST['emailpromo']);
$accountactive = mysqli_real_escape_string($db_conx, $_POST['accountactive']);

// GET USER IP ADDRESS
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
// DUPLICATE DATA CHECKS FOR USERNAME AND EMAIL
$sql = "SELECT accnum FROM FLCUsers WHERE username='$username' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$username_check = mysqli_num_rows($query);
// -------------------------------------------
$sql = "SELECT accnum FROM FLCUsers WHERE email='$email' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$email_check = mysqli_num_rows($query);
// FORM DATA ERROR HANDLING
if($username == "" || $email == "" || $confemail == "" || $password == "" || $confpassword == "" || $fname == "" || $lname == "" || $gender == "" || $address1 == "" || $city == "" || $state == "" || $zip == "" || $secquest1 == "" || $secansw1 == ""){
echo "All required fields have not been completed";
exit();
} else if ($username_check > 0){
echo "The username selected is already taken.";
exit();
} else if ($email_check > 0){
echo "That email address is already in use in the system";
exit();
} else if (strlen($username) < 3 || strlen($username) > 16) {
echo "Your username must contain 5 to 40 caracters";
exit();
} else if (is_numeric($username[0])) {
echo 'Username cannot begin with a number';
exit();
} else {
// END FORM DATA ERROR HANDLING
// Begin Insertion of data into the database
// Hash the password and apply your own mysterious unique salt

$res = explode("/", $dob);
$dobok = $res[2]."-".$res[0]."-".$res[1];

$cryptpass = crypt($password);
include_once ("/US/en/local/ressources/php/randStrGen.php");
$password_hash = randStrGen(20)."$cryptpass".randStrGen(20);
// Add user info into the database table for the main site table
$sql = "INSERT INTO FLCUsers (username, email, password, prefix, fname, mname, lname, gender, dob, address1, address2, city, state, zip, secquest1, secansw1, creationip, signupdate, laslogin, accountactive)
VALUES('$username','$email','$password_hash','$prefix','$fname','$mname','$lname','$gender', '$dobok', $address1','$address2','$city','$state','$zip','$mname','$secansw1','$secansw1','$ip',now(),now(), 0)";
$query = mysqli_query($db_conx, $sql);
$accnum = mysqli_insert_id($db_conx);
// Establish their row in the useroptions table
$sql = "INSERT INTO useroptions (accnum, username, background) VALUES ('$accnum','$username','original')";
$query = mysqli_query($db_conx, $sql);
// Create directory(folder) to hold each user's files(pics, MP3s, etc.)
if (!file_exists("FLCUsers/$username")) {
mkdir("FLCUsers/$username", 0755);
}
// Email the user their activation link
exit();
}

感谢任何人愿意分享的任何提示!

最佳答案

你确实想使用这个 jQuery:

$.ajax({
url: "/US/en/local/flclub/account/action/FLClubRegisterAction.php",
data: dataToSend,
type: 'POST',
dataType: 'json',
success: function(data, status) {
console.log(data);
console.log(status);
if(data.code == 400 || data.code == 401 || data.code == 402 || data.code ==403){
//alert('error');
$('#status').html('Error...' + data.message);
$("#signupbtn").css("display","block")
}

对于错误处理,您可能希望使用类似于以下内容的内容:

if ($username_check > 0){ 

echo json_encode(["message"=>"The username selected is already taken.", "code"=>400]);
exit();

这会创建将在 jQuery 中处理并显示的错误。

关于JavaScript 函数 :signup() not sumbitting to ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48999156/

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