How to show server side error using jquery and ajax.
I am new to jquery and php. i have one query that i have mentioned below. plese help me to fix this query.
1.) I want that when i enter email in signup form for signup who already registered in database then a warning message show for account already existense.
I use jquery validation plugin for client side validation and use ajax for form submit.
when i signed up successfully, successfull-message show properly in a div which class is ".signup-success".
but when i enter email that already registered then same div which class is ".signup-success" is appear with no message from server.
i want that email aready exits message show in different div. not in ".signup-success" div.
plzzzzzz help me....
My Code are below:
Validate.js
- submitHandler: function(form) {
- var fname = $("#fname").val();
- var lname = $("#lname").val();
- var email = $("#email").val();
- var mobile = $("#mobile").val();
- var password = $("#password").val();
-
- var form_data = {
- fname: fname,
- lname: lname,
- email: email,
- mobile: mobile,
- password: password
- }
- $.ajax({
- url: "./script/send.php",
- data: form_data,
- type: "POST",
- beforeSend: function () {
- $(".signup-process").fadeIn(300);
- },
- success: function(data) {
- $(".signup-process").fadeOut(100);
- $(".signup-success").fadeIn(500);
- $("#signup-success-msg").html(data);
- }
- });
- return false;
}
Index.php
- <section id="signup_sec">
- <div class="container">
- <div class="row">
- <div class="col-md-12">
- <div class="wrap-signup">
- <div class="row">
- <div class="col-md-5">
- <div class="signup_icon">
- <img src="assets/img/login.png" class="img-responsive" alt="Image">
- </div>
- </div>
- <div class="col-md-7 signup-form-wrap">
- <h3 class="text-center login-form-title">Create Your Account</h3>
- <form action="script/send.php" method="POST" role="form" id="signupForm">
- <div class="row">
- <div class="col-md-6">
- <div class="form-group">
- <input type="text" class="form-control" placeholder="*Firstname" id="fname" name="fname">
- </div>
- </div>
- <div class="col-md-6">
- <div class="form-group">
- <input type="text" class="form-control" placeholder="*Lastname" id="lname" name="lname">
- </div>
- </div>
- </div>
- <div class="row">
- <div class="col-md-6">
- <div class="form-group">
- <input type="text" class="form-control" placeholder="*Email" id="email" name="email">
- </div>
- </div>
- <div class="col-md-6">
- <div class="form-group">
- <input type="text" class="form-control" placeholder="*Mobile" id="mobile" name="mobile">
- </div>
- </div>
- </div>
- <div class="form-group">
- <input type="password" class="form-control" placeholder="*Password" id="password" name="password">
- </div>
- <button type="submit" class="form-control btn btn-success btn-block" id="send" name="send" value="Submit">SUBMIT</button>
- </form>
- </div>
- </div>
- <div class="jumbotron signup-success">
- <div class="container">
- <div class="row">
- <div class="col-md-6 col-md-offset-3">
- <div class="signup-success-box">
- <div class="signup-success-box-top">
- <img src="assets/img/right.svg" class="img-responsive" alt="Image">
- </div>
- <div class="signup-success-box-bottom">
- <h2 class="text-center">Success</h2>
- <p class="text-center" id="signup-success-msg"></p>
- <a href="" type="button" id="close_success_msg" class="btn btn-danger">Close</a>
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- <div class="signup-process">
- <div class="jumbotron signup-process-overlay"></div>
- <div class="container">
- <img src="assets/img/loding.svg" class="img-responsive loading-icon" alt="Image">
- </div>
- </div>
- <p class="refer-to-login-signup">Already Registered? <a href="user/login_form.php"> Login</a></p>
- </div>
- </div>
- </div>
-
- </div>
</section>
Send.php
- <?php
- // Import PHPMailer classes into the global namespace
- // These must be at the top of your script, not inside a function
- use PHPMailer\PHPMailer\PHPMailer;
- use PHPMailer\PHPMailer\Exception;
- ?>
- <?php include("../config/db.php"); ?>
- <?php
- global $email_err;
- global $success_msg;
-
-
- $fname = $_POST["fname"];
- $lname = $_POST["lname"];
- $email = $_POST["email"];
- $mobile = $_POST["mobile"];
- $password = $_POST["password"];
- $email_check = "SELECT email FROM signup WHERE email = '{$email}' ";
- $email_query = mysqli_query($connection, $email_check);
- $count = mysqli_num_rows($email_query);
- if ($count>0) {
- $email_err = "This email was already registered.";
- }
- else{
- $user_activation_key = md5(rand().time());
- $sql = "INSERT INTO signup (fname, lname, email, mobile, password, activation_key, is_active, date_time ) VALUES ('{$fname}','{$lname}','{$email}','{$mobile}','{$password}','{$user_activation_key}','0',now())";
- $query = mysqli_query($connection, $sql);
- if (!$query) {
- die("Faield to send data".mysqli_error($connection));
- }
- if ($query) {
- $activation_msg = "Plese Activate Your Account Using This Link "."<a href='http://localhost/ajax/user/user_activation.php?key=".$user_activation_key."'>http://localhost/ajax/user/user_activation.php?key=".$user_activation_key."</a>";
-
- //Load composer's autoloader
- require ('../mailer/vendor/autoload.php');
- $mail = new PHPMailer(true); // Passing `true` enables exceptions
- try {
- //Server settings
- //$mail->SMTPDebug = 2; // Enable verbose debug output
- $mail->isSMTP(); // Set mailer to use SMTP
- $mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
- $mail->SMTPAuth = true; // Enable SMTP authentication
- $mail->Username = 'mj981220@gmail.com'; // SMTP username
- $mail->Password = '33661212'; // SMTP password
- $mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
- $mail->Port = 465; // TCP port to connect to
- //Recipients
- $mail->setFrom('mj981220@gmail.com', 'Ajax CRUD');
- $mail->addAddress($email, $fname." ".$lname); // Add a recipient
- //Content
- $mail->isHTML(true); // Set email format to HTML
- $mail->Subject = 'Varification Email From PHP CRUD APPLICATION';
- $mail->Body = $activation_msg;
-
- if ($mail->send()) {
- $success_msg = "Check your email. A verification email has been sent to your email Address.";
- echo $success_msg;
- }
- } catch (Exception $e) {
-
- $success_msg = "Email has been not sent";
- echo $success_msg;
- }
- }
- }
-
-
-
-
- ?>