У меня есть регистрационная форма, которая при заполнении и нажатии кнопки «Регистрация» проверяется js, для поиска пустых полей и для проверки доступности имени пользователя, или если электронная почта или мобильный номер уже используются, отправляя информацию через ajax на php и получая ответ , Но мои js не будут работать полностью. Это мой js-скрипт:
$("#reg_button").click(function(){ user = $("#usr").val(); pass = $("#psw").val(); fname = $("#first_name").val(); sname = $("#second_name").val(); dateb = $("#date_birth").val(); email = $("#email").val(); mobnum = $("#mob_num").val(); if(user == ""){ alert("First name must be filled out"); $('#usr').focus(); return false; }else if(pass == ""){ alert("Password must be filled out"); $('#psw').focus(); return false; }else if(fname == ""){ alert("First name must be filled out"); $('#first_name').focus(); return false; }else if(sname == ""){ alert("Second name must be filled out"); $('#second_name').focus(); return false; }else if(dateb == ""){ alert("Date of birth must be filled out"); $('#date_birth').focus(); return false; }else if(email == ""){ alert("Email must be filled out"); $('#email').focus(); return false; }else if(mobnum == ""){ alert("Mobile number must be filled out"); $('#mob_num').focus(); return false; }else{ ajaxCheck(); } function ajaxCheck(){ $.ajax({ type: "POST", url: "http://imes.*********.com/php/check_info_reg.php", data: "usr="+user+"&email="+email+"&mob_num="+mobnum, dataType: "json", success: function(json){ if(json.success){ var user_conf = json.data.usr; var email_conf = json.data.email; var mob_conf = json.data.mob; if(user_conf == "taken"){ alert("Username already taken. Choose another one."); $('#usr').focus(); return false; } if(email_conf == "taken"){ alert("Email already registered. If you lost your password, retrieve it on login page."); $('#email').focus(); return false; } if(mob_conf == "taken"){ alert("Mobile number already registered. If you lost your password, retrieve it on login page."); $('#mob_num').focus(); return false; } }else{ //Here could go another ajax, for actualy sending the //info into the php script which sends it to database. } }, beforeSend: function() { $.mobile.showPageLoadingMsg(); }, //Show spinner complete: function() { $.mobile.hidePageLoadingMsg() }, //Hide spinner }); return false; } });
И мой php:
<?php $username = mysql_real_escape_string($_POST['usr']); $email = mysql_real_escape_string($_POST['email']); $mob_num = mysql_real_escape_string($_POST['mob_num']); include('mysql_connection.php'); mysql_select_db("jzperson_imesUsers", $con); $sql1 = mysql_query("SELECT * FROM registered_user WHERE username='$username'"); $sql2 = mysql_query("SELECT * FROM registered_user WHERE email='$email'"); $sql3 = mysql_query("SELECT * FROM registered_user WHERE mobile_num='$mob_num'"); $res1 = mysql_num_rows($sql1); $res2 = mysql_num_rows($sql2); $res3 = mysql_num_rows($sql3); if(isset($username) && !empty($username){ if($res1 >= 1){ //JSON message: Username already taken. Choose different. echo json_encode(array('success'=>true, 'usr'=>"taken")); } } elseif(isset($email) && !empty($email)){ if($res2 >= 1){ //JSON message: Email already registered. Retrieve on "login"(login => link). echo json_encode(array('success'=>true, 'email'=>"taken"));; } } elseif(isset($mob_num) && !empty($mob_num)){ if($res3 >= 1){ //JSON message: Mobile number already registered. Retrieve on "login"(login => link). echo json_encode(array('success'=>true, 'mob'=>"taken")); } } else{ echo json_encode(array('success'=>false)); } ?>
в<?php $username = mysql_real_escape_string($_POST['usr']); $email = mysql_real_escape_string($_POST['email']); $mob_num = mysql_real_escape_string($_POST['mob_num']); include('mysql_connection.php'); mysql_select_db("jzperson_imesUsers", $con); $sql1 = mysql_query("SELECT * FROM registered_user WHERE username='$username'"); $sql2 = mysql_query("SELECT * FROM registered_user WHERE email='$email'"); $sql3 = mysql_query("SELECT * FROM registered_user WHERE mobile_num='$mob_num'"); $res1 = mysql_num_rows($sql1); $res2 = mysql_num_rows($sql2); $res3 = mysql_num_rows($sql3); if(isset($username) && !empty($username){ if($res1 >= 1){ //JSON message: Username already taken. Choose different. echo json_encode(array('success'=>true, 'usr'=>"taken")); } } elseif(isset($email) && !empty($email)){ if($res2 >= 1){ //JSON message: Email already registered. Retrieve on "login"(login => link). echo json_encode(array('success'=>true, 'email'=>"taken"));; } } elseif(isset($mob_num) && !empty($mob_num)){ if($res3 >= 1){ //JSON message: Mobile number already registered. Retrieve on "login"(login => link). echo json_encode(array('success'=>true, 'mob'=>"taken")); } } else{ echo json_encode(array('success'=>false)); } ?>
В вашем php-файле отсутствует скобка:
if(isset($username) && !empty($username){
Должно быть
if(isset($username) && !empty($username)){