0

I just want to whenever after I type the email it will automatically display whether email can be used(not existed in db) or not(existed on db). When I type there is no output that displays.As of now I have this code. How should i fix this

Here's my main page code's

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Jquery-ajax Practice</title>
</head>

<body>
<section>
    <form>
        <label for="text_email">E-mail:</label>
        <input id="text_email" type="email" >

        <script type="text/javascript" charset="utf-8" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js">
            $(document).ready(function(){
                $('#text_email').keyup(function(){
                    email($(this).val());
                });
            });
            function email(str){
                $.post('checkmail.php',{ email: str},
                function(data){
                    $('#check_email').html(data.returnValue);
                }, 'json');
            }
        </script>

        <label for="text_email" id="check_email"></label>   
    </form>
</section>
</body>
</html>

Here's my php code

<?php
if(isset($_POST['email']))
{
    try 
    {
        $pdo=new PDO('mysql:host=localhost;dbname=class;charset=utf-8', 'root');
    }
    catch(PDOException $e)
    {
        echo 'Failed: '.$e->getMessage();
    }

    $stmt=$pdo->prepare('SELECT email FROM class where email=:email LIMIT 1');
    $stmt->execute(array(':email'=>$_POST['email']));
    if($stmt->rowCount()>0)
    {
        $check='E-mail cannot be use.';
    }
    else
    {
        $check='E-mail can be use.';
    }

    echo json_encode(array('returnValue'=>$check));
}
?>
8
  • 1
    Your code looks fine with a quick glance. Are you getting errors? Is it not working as expected? Commented Aug 27, 2012 at 17:26
  • 3
    You need to ask a specific, answerable question about a problem you're having. "Here's my code, how can I improve it?" is not a question by Stack Overflow's definition. Commented Aug 27, 2012 at 17:26
  • 4
    Code Review may be more appropriate Commented Aug 27, 2012 at 17:28
  • 2
    Rather than overloading your server by sending an ajax call everytime the user releases a key.You should probably provide a button that has a click event handled by your function email. You should use either firebug for firefox or fiddler to watch the request/response from the ajax call to see what you are sending and what the js is receiving back. Make certain you echo a header from the PHP before passing back any data. You should also use the jquery ajax function at first and define error and success functions to aid your debugging. Commented Aug 27, 2012 at 17:32
  • 2
    I think you mustn't use keyup function to check the user input as it's going to overload your server for no reason. I would rather use on blur function to trigger the AJAX email check or even better to check email on key up only if the email appears to be valid. Commented Aug 27, 2012 at 17:51

1 Answer 1

1

One problem I can see, is your handling of the database errors.

You should wrap all PDO operations in a try - catch block (not just the connection part) and when you catch an error, you would need to wrap that in your json_encode statement at the end as well and not just echo it out as that would invalidate the received json at the client side.

I don't know your database setup, but you are not providing a password and I think your connection string should have charset=utf8.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm just using wamp sir,and I didn't change or touch anything on it :)
@Sui Go I don't use wamp, but I would think that the root user has a password set. You should use firebug or something similar and check the output from your ajax call in the Net tab. Or just post directly to your php script and see what errors it gives you.
I think my connection string works fine sir because I already use it for many times. I think the problem is on my ajax, it my first time. OK sir I' try it. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.