1

i am trying to get the user details into database and data is stored..i want a success message to fade in i have tried out some code but sadly its not working...plzz help me out of this..beg u pardon if am wrong.. here gose my register.php code

               <?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)) {
            // 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 {
                // 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 {
            // invalid email address
            $response["error"] = true;
            $response["error_msg"] = "invalid email address";
            echo json_encode($response);
        }
    } else {
        $response["error"] = true;
        $response["error_msg"] = "Required parameters are missing!";
        echo json_encode($response);
    }
?>

and here gose the .html file with jquery..

              <html>
    <head>
<title>jQuery Test</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script type="text/javascript" src = "register.js"></script>
 </head>

 <body>

    <!--html body-->
    <form name = "register" id = "register" method = "POST">
        <label>First name:</label>
        <input type = text name = "fname" id = "fname" required>
        <label>Last name:</label>
        <input type = "text" name = "lname" id = "lname" required>
        <label>E-mail:</label>
        <input type = "email" name = "email" id = "email" required>
        <label>Password</label>
        <input type = "password" name = "password" id = "password" required>
        <label>Mobile no:</label>
        <input type = "number" name = "mobile" id = "mobile" required>
        <input type="submit" value="Insert" name="submit" id = "submit">
    </form>
    <div id = "result" align = "right"></div>
</body>
</html>

here gose me /.js/ file

        $(document).ready(function(){

    $("#submit").click(function(e){

        e.preventDefault();

        $.ajax({
            url: "register.php",
            type: "POST",
            data: {
                fname: $("#fname").val(),
                lname: $("#lname").val(),
                email: $("#email").val(),
                password: $("#password").val(),
                mobile: $("#mobile").val()
            },
            dataType: "JSON",
            success: function (json) {
                $("#result").html(json.user.email);  // like that you can display anything inside #result div
                $("#result").fadeOut(1500);

            },
            error: function(jqXHR, textStatus, errorThrown){
                alert(errorThrown);
            }
        });
    });

}); 

1 Answers1

0

There's no need to use JSON.stringify(jsonStr) because jQuery has already parsed the response object for you. jQuery will look at the Content-Type of the response and, if it's application/json, it will parse it, and provide the parsed result to your success handler.

Your jQuery should be like this:

$(document).ready(function(){

    $("#submit").click(function(e){

        e.preventDefault();

        $.ajax({
            url: "register.php",
            type: "POST",
            data: {
                fname: $("#fname").val(),
                lname: $("#lname").val(),
                email: $("#email").val(),
                password: $("#password").val(),
                mobile: $("#mobile").val()
            },
            dataType: "JSON",
            success: function (json){
                if(json.error){
                    $("#result").html(json.error_msg);  // display error message
                }else{
                    $("#result").html(json.user.email);  // like that you can display anything inside #result div
                }
                $("#result").fadeOut(1500);
            },
            error: function(jqXHR, textStatus, errorThrown){
                alert(errorThrown);
            }
        });
    });

}); 
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • sir..!! it seems that even empty feilds are taken in to database..i have used ||....!==") but is seems to be not working –  Dec 07 '15 at 04:29
  • @krishna You mean to say in `storeUser` method some empty fields are getting inserted into the `users` table, right? – Rajdeep Paul Dec 07 '15 at 07:20
  • sir...I have solved it sir...thanks for reply...but i am now facing problem wit email validation –  Dec 07 '15 at 07:26
  • i have updated it sir..!!now problem is email validation in it i want to use filter_var($email, FILTER_VALIDATE_EMAIL) in this code but confused –  Dec 07 '15 at 07:29
  • i have asked a new question..!! plz help me out sir..!!@ http://stackoverflow.com/questions/34128372/how-efficently-may-i-use-filter-varemail-filter-validate-email –  Dec 07 '15 at 07:40
  • @krishna let me take a look at it. – Rajdeep Paul Dec 07 '15 at 08:36
  • @krishna Please see my answer there. – Rajdeep Paul Dec 07 '15 at 08:53
  • sir..the solution which u gave me for the question above is only working for success message..!! when ever their is an error as you specified its not giving a alert on error i.e textStatus..sir i have updated the whole question plz if you could assist me on this one..!! –  Dec 08 '15 at 04:04
  • 1
    @krishna `error: function(jqXHR, textStatus, errorThrown){ ... }` is called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides `null`) are `"timeout"`, `"error"`, `"abort"`, and `"parsererror"`. When an HTTP error occurs, `errorThrown` receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error". – Rajdeep Paul Dec 08 '15 at 05:55
  • soo sir..is their any way i could display alert on empty fields/say so invalid email in back-end side and display alert in front-end –  Dec 08 '15 at 06:01
  • 1
    @krishna I've updated my answer. Please test your application with the updated code. – Rajdeep Paul Dec 08 '15 at 06:12
  • 1
    @krishna No, `error_msg` comes from your `register.php`. Look at these statements, `$response["error"] = true; $response["error_msg"] = "Unknown error occurred in registration!";`. There's a key field `error_msg` in your `$response` array. – Rajdeep Paul Dec 08 '15 at 06:19
  • ur...superb sir...!! accurate..!! –  Dec 08 '15 at 06:30
  • sir..may i get your assistance on this question..!! http://stackoverflow.com/questions/34197062/back-end-error-msg-is-not-giving-a-alert –  Dec 10 '15 at 09:27
  • no..sir i have to display out the error messages as you ansered in your previous...and on success message then perform action in form tag...if one happens other is not executing..e.preventDefault(); is not working out as its not stopping the page to move to login.html –  Dec 10 '15 at 10:17
  • 1
    @krishna So after displaying *Registeration successful!* you want to submit the form and redirect it to *login.html*, right? – Rajdeep Paul Dec 10 '15 at 10:20
  • exactly sir...i have tried out lots of hour resolving it..sadly not unable to do so..!! –  Dec 10 '15 at 10:21