AJAX:JSON数据未返回正确的值

时间:2019-02-01 11:54:09

标签: jquery html css json ajax

我正在使用登录表单。我有一些有关Json数据返回值的问题,我也不知道为什么:/

如果我尝试我的代码,则数据已正确保存在数据库中(用户名/密码/电子邮件),并且“ if”控件似乎工作正常(用户已注册,重复发送电子邮件等) 顺便说一句: 在我的周期

if(data === "1"){...}

else if(data==2){...}  

该函数始终向我返回错误的值。 我也尝试过使用

的布尔形式
if(data.success){
...
}else{
...
}

但它似乎还是不起作用:( 总是把我带回“其他”

这是我的HTML:

<div class="form">
        <form method="post" class="register-form">
            <fieldset>
            <b>Nome utente:</b>
            <input type="text" id="username_r" placeholder="inserisci nome utente"  maxlength="16"/>
            <b>Password:</b>
            <input type="password" id="password_r" placeholder="inserisci password" maxlength="30"/>
            <b>Conferma password:</b>
            <input type="password" id="conferma_password" placeholder="inserisci password" maxlength="30"/>
            <b>Indirizzo email:</b>
            <input type="text" id="email" placeholder="email" maxlength="35"/>
            <p id="error"></p>
            <input id="register_input" type="submit" value="registrati">
            <p class="message">Già registrato? <a href="#">Esegui il login</a></p>
            </fieldset>
        </form>
</div>

[Login.js]

$("#register_input").click(function(event){
		event.preventDefault();
		
		var username1 = $('#username_r').val();
		var email = $('#email').val();
		var password1 = $('#password_r').val();
		var password2 = $('#conferma_password').val();
		
		username1 = username1.replace(/[" "]/g, "");
		email = email.replace(/[" "]/g, "");
		password1 = password1.replace(/[" "]/g, "");
		password2 = password2.replace(/[" "]/g, "");
		
		var myRegEx = /^[A-z0-9\.\+_-]+@[A-z0-9\._-]+\.[A-z]{2,6}$/;
		
		if(username1 === "" || email === "" || password1 === "" || password2 === ""){
			$("form #error").html("Devi riempire tutti i campi per registrarti.");
		} else if(password1.length<6){
			$("form #error").html("La password deve avere almeno 6 caratteri.");
		} else if(password1 !== "" && password2 !== "" && password1 !== password2){
			$("form #error").html("Le password inserite non sono uguali.");
		}else if(!myRegEx.test(email)){
			$("form #error").html("Indirizzo email non valido.");
		} else {
			$.ajax({
				url:"php/loginFunction.php",
				method:"POST",
				data:{username:username1, email:email, password:password1},
				success:function(data){
					data = JSON.parse(data);
					data = data.esito;
                    //*** Here the problem ***
					if(data === "1"){
                                                $("form #error").html("Registrazione eseguita correttamente!");
						window.location.replace("http://localhost/progetto/home.php");
					} else if(data === "0"){
						$("form #error").html("Username già esistente.");
					} else if(data === "2"){
						$("form #error").html("Email già in uso.");
					}
				},
				error: function(){
					alert("Si è verificato un errore, riprovare più tardi.");
				}
			});
		}	
	});

[LoginFunction.php]

function registrazione($username, $password, $email){
	try{
		$db = db_connect();
		$username = $db->quote($username);
		$password = $db->quote($password);
		$email = $db->quote($email);
		
		$query = "INSERT INTO utente(username, password, email)
				VALUES(".$username.", ".$password.", ".$email.")";
		
		$res = $db->exec($query);
		if($res){
			$_SESSION["username"] = $username;
			$query = "SELECT id_utente
				FROM utente
				WHERE username=".$username;
			$rows = $db->query($query);
			foreach($rows as $row){
				$_SESSION["id_utente"] = $row["id_utente"];
			}
			return 1; // registrazione effettuata con successo
		} else {
			$query = "SELECT COUNT(*) AS email_presente
				FROM utente
				WHERE email=".$email;
			$rows = $db->query($query);
			foreach($rows as $row){
				if($row["email_presente"]=="1"){
					return 2; //registrazione non effettuata, l'emai inserita è già presente nel db
				} else return 0;
			}
		}
		return 0; // registrazione non effettuata
		
	}catch (PDOException $ex){
		return -2; // errore
	}	
}

*编辑*

这是我的[loginFunction.php]的第一部分

<?php include_once("db.php");?>
<?php
session_start();

if(isset($_POST["username"]) && isset($_POST["email"])){
	$username = htmlspecialchars($_POST["username"]);
	$password = md5(htmlspecialchars($_POST["password"]));
	$email = htmlspecialchars($_POST["email"]);
	$regReturn = registrazione($username, $password, $email);
	$array = array("esito"=>$regReturn);
	$json = json_encode($array);
	print $json;
} else if(isset($_POST["username"])){
	$username = htmlspecialchars($_POST["username"]);
	$password = md5(htmlspecialchars($_POST["password"]));
	$loginReturn = login($username, $password);	
	$array = array("esito"=>$loginReturn);
	$json = json_encode($array);
	print $json;
}

0 个答案:

没有答案