Я использую jquery, чтобы выполнить файл .php, но моя главная проблема заключается в том, когда когда-либо ошибка выводится из исходного кода, я использовал предупреждение, чтобы отобразить этот error_msg..but когда-либо я отправляю с ошибкой намеренно … его просто перемещение на странице, указанной в действии … нет предупреждения об ошибке всплыло … plz помогите мне из этого. !! простите меня, если я ошибаюсь
здесь есть DB_Function.php
<?php class DB_Functions { private $db; // constructor for database connection function __construct() { try { $hostname = "localhost"; $dbname = "miisky"; $dbuser = "root"; $dbpass = ""; $this->db = new PDO("mysql:host=$hostname;dbname=$dbname", $dbuser, $dbpass); } catch(PDOException $e) { die('Error in database requirments:' . $e->getMessage()); } } /** * Storing new user * returns user details of user */ public function storeUser($fname, $lname, $email, $password, $mobile) { try { $hash = md5($password); $sql = "INSERT INTO users(fname, lname, email, password, mobile, created_at) VALUES ('$fname', '$lname', '$email', '$hash', '$mobile', NOW())"; $dbh = $this->db->prepare($sql); if($dbh->execute()){ // get user details $sql = "SELECT * FROM users WHERE email = '$email' LIMIT 1"; $dbh = $this->db->prepare($sql); $result = $dbh->execute(); $rows = $dbh->fetch(); $n = count($rows); if($n){ return $rows; } } } catch (Exception $e) { die('Error accessing database: ' . $e->getMessage()); } return false; } /*to check if user is already registered*/ public function isUserExisted($email) { try{ $sql = "SELECT email FROM users WHERE email = '$email' LIMIT 1"; $dbh = $this->db->prepare($sql); $result = $dbh->execute(); if($dbh->fetch()){ return true; }else{ return false; } }catch (Exception $e) { die('Error accessing database: ' . $e->getMessage()); } } /*to check if user exist's by mobile number*/ public function isMobileNumberExisted($mobile){ try{ $sql = "SELECT mobile FROM users WHERE mobile = '$mobile' LIMIT 1"; $dbh = $this->db->prepare($sql); $result = $dbh->execute(); if($dbh->fetch()){ return true; }else{ return false; } }catch(Exception $e){ die('Error accessing database: ' . $e->getMessage()); } } //DB_Functions.php under construction //more functions to be added } ?>
здесь gose .php- файл, чтобы быть понятным, что делаю .. !!
<?php require_once 'DB_Functions.php'; $db = new DB_Functions(); // json response array $response = array("error" => false); if (!empty($_POST['fname']) && !empty($_POST['lname']) && !empty($_POST['email']) && !empty($_POST['password']) && !empty($_POST['mobile'])){ // receiving the post params $fname = trim($_POST['fname']); $lname = trim($_POST['lname']); $email = trim($_POST['email']); $password = $_POST['password']; $mobile = trim($_POST['mobile']); // validate your email address if(filter_var($email, FILTER_VALIDATE_EMAIL)) { //validate your password if(strlen($password) > 6){ //validate your mobile if(strlen($mobile) == 12){ //Check for valid email address if ($db->isUserExisted($email)) { // user already existed $response["error"] = true; $response["error_msg"] = "User already existed with " . $email; echo json_encode($response); } else { if($db->isMobileNumberExisted($mobile)) { //user already existed $response["error"] = true; $response["error_msg"] = "user already existed with" . $mobile; echo json_encode($response); } else { // create a new user $user = $db->storeUser($fname, $lname, $email, $password, $mobile); if ($user) { // user stored successfully $response["error"] = false; $response["uid"] = $user["id"]; $response["user"]["fname"] = $user["fname"]; $response["user"]["lname"] = $user["lname"]; $response["user"]["email"] = $user["email"]; $response["user"]["created_at"] = $user["created_at"]; $response["user"]["updated_at"] = $user["updated_at"]; echo json_encode($response); } else { // user failed to store $response["error"] = true; $response["error_msg"] = "Unknown error occurred in registration!"; echo json_encode($response); } } } } else { $response["error"] = true; $response["error_msg"] = "Mobile number is invalid!"; echo json_encode($response); } } else { //min of 6-charecters $response["error"] = true; $response["error_msg"] = "password must be of atleast 6-characters!"; echo json_encode($response); } } else { // invalid email address $response["error"] = true; $response["error_msg"] = "invalid email address"; echo json_encode($response); } } else { $response["error"] = true; $response["error_msg"] = "Please fill all the required parameters!"; echo json_encode($response); } ?>
по<?php require_once 'DB_Functions.php'; $db = new DB_Functions(); // json response array $response = array("error" => false); if (!empty($_POST['fname']) && !empty($_POST['lname']) && !empty($_POST['email']) && !empty($_POST['password']) && !empty($_POST['mobile'])){ // receiving the post params $fname = trim($_POST['fname']); $lname = trim($_POST['lname']); $email = trim($_POST['email']); $password = $_POST['password']; $mobile = trim($_POST['mobile']); // validate your email address if(filter_var($email, FILTER_VALIDATE_EMAIL)) { //validate your password if(strlen($password) > 6){ //validate your mobile if(strlen($mobile) == 12){ //Check for valid email address if ($db->isUserExisted($email)) { // user already existed $response["error"] = true; $response["error_msg"] = "User already existed with " . $email; echo json_encode($response); } else { if($db->isMobileNumberExisted($mobile)) { //user already existed $response["error"] = true; $response["error_msg"] = "user already existed with" . $mobile; echo json_encode($response); } else { // create a new user $user = $db->storeUser($fname, $lname, $email, $password, $mobile); if ($user) { // user stored successfully $response["error"] = false; $response["uid"] = $user["id"]; $response["user"]["fname"] = $user["fname"]; $response["user"]["lname"] = $user["lname"]; $response["user"]["email"] = $user["email"]; $response["user"]["created_at"] = $user["created_at"]; $response["user"]["updated_at"] = $user["updated_at"]; echo json_encode($response); } else { // user failed to store $response["error"] = true; $response["error_msg"] = "Unknown error occurred in registration!"; echo json_encode($response); } } } } else { $response["error"] = true; $response["error_msg"] = "Mobile number is invalid!"; echo json_encode($response); } } else { //min of 6-charecters $response["error"] = true; $response["error_msg"] = "password must be of atleast 6-characters!"; echo json_encode($response); } } else { // invalid email address $response["error"] = true; $response["error_msg"] = "invalid email address"; echo json_encode($response); } } else { $response["error"] = true; $response["error_msg"] = "Please fill all the required parameters!"; echo json_encode($response); } ?>
и вот главный файл .js
$(document).ready(function(){ //execute's the function on click $("#submit").click(function(e){ /*jquery to call the url requested and parse the data in json*/ $.ajax({ url: "register.php", type: "POST", data: { fname: $("#fname").val(), lname: $("#lname").val(), email: $("#email").val(), password: $("#password").val(), mobile: $("#mobile").val() }, dataType: "JSON", /*Give out the alert box to display the results*/ success: function (json){ if(json.error){ alert(json.error_msg); e.preventDefault(); }else{ alert("Registeration successful!",json.user.email); } }, error: function(jqXHR, textStatus, errorThrown){ alert(errorThrown); e.preventDefault(); } }); }); });
и здесь gose соответствующий .html– файл
<form method = "POST" name = "register" id = "register" class="mt" role="form" action="login.html"> <div class="form-group"> <input type="text" name = "fname" id = "fname" class="form-control" placeholder="First Name" required=""> </div> <div class="form-group"> <input type="text" name = "lname" id = "lname" class="form-control" placeholder="Last Name" required=""> </div> <div class="form-group"> <input type="email" name = "email" id = "email" class="form-control" placeholder="Email" required=""> </div> <div class="form-group"> <input type="password" name = "password" id = "password" class="form-control" placeholder="Password" required=""> </div> <div class="form-group"> <input type="mobile" name = "mobile" id = "mobile" class="form-control" placeholder="Mobile No" required=""> </div> <div class="form-group" id="recaptcha_widget"> <div class="required"> <div class="g-recaptcha" data-sitekey="6Lc4vP4SAAAAABjh8AG"></div> <!-- End Thumbnail--> </div> <?php include("js/captcha.php");?> </div> <div class="form-group"> <div cle the terms and policy </label></div> </div>ass="checkbox i-checks"><label> <input type="checkbox"><i></i> Agre <button type="submit" name = "submit" id = "submit" class="btn btn-primary block full-width mb">Register</button> <p class="text-muted text-center"><small>Already have an account?</small></p> <a class="btn btn-sm btn-white btn-block" href="login.html">Login</a> < /form>