Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9271217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:33:57+00:00 2026-06-18T15:33:57+00:00

What I am trying to do is validate whether an email address is in

  • 0

What I am trying to do is validate whether an email address is in the database or not. My CheckEmailAddress checks if the email is in the database and it seems to work(rewrite it if needed). The problem is when I post my JSON email data from my JQuery function it always returns true when there should be a false if the email is entered in the database.

JQUERY:

$('#checkemail').click(function() {
    $.post('http://' +  location.host + '/buyme/include/getemailaddress.php', 
        {'email':'test012@yahoo.co.nz'},  function(res){
        var obj = JSON.parse(res);
        alert(obj)
    });
});

PHP

<?PHP
    require_once("membership.php");
    $membership = new Membership();
    $val = $_POST["email"];;
    $result = $membership->CheckEmailAddress(trim($val));
    echo json_encode($result); //output test@test.com   
?>

function CheckEmailAddress($email_address) {
    $connection = mysql_connect('localhost:3306','root','')or die('Error connecting');
    mysql_select_db('buyme') or die('Connection not working properly');     
    $query = "SELECT email_address from users where email_address='$email_address'";
    $result = mysql_query($query, $connection);
    $row = mysql_fetch_assoc($result);

            $emailInUse = 'false';
    if(!$row || $row["email_address"] == '') {
        $emailInUse = 'true';
    }else{
        $emailInUse = 'false';  
    }

    return $emailInUse;
}

if anything needs correction please update my code so i can test thanks

UPDATE:

    try {
    $pdo = new PDO('mysql:dbname=buyme;host=localhost:3306', 'root', '', 
        $options = array (
        PDO::ATTR_ERRMODE,  PDO::ERRMODE_EXCEPTION
    ));
    $email_address = 'kirkdm021@yahoo.co.nz';

    $stmt = $pdo->prepare("SELECT email_address
            FROM users 
            WHERE email_address=:email_address");
    $stmt->execute(array('email_address'=>$email_address));
    $rows = $stmt->fetchObject();

    echo empty($rows->email_address) ? "" : $rows->email_address; // $row["email_address"];

}catch (PDOException $e) {
    die('error: ' . $e->getMessage());      
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:33:59+00:00Added an answer on June 18, 2026 at 3:33 pm

    The issue is your TRUE & FALSE assignments are returning strings & not TRUE or FALSE values. Meaning they will always essentially be seen as TRUE because they are strings that contain values. A FALSE will always contain nothing. Also, you are doing an OR by doing || when it should be an AND by doing &&. So this snippet of code:

    if(!$row || $row["email_address"] == '') {
        $emailInUse = 'true';
    }else{
        $emailInUse = 'false';  
    }
    

    Should be rewritten like this:

    if(!$row && $row["email_address"] == '') {
        $emailInUse = TRUE;
    }else{
        $emailInUse = FALSE;  
    }
    

    And the final function would look like this:

    function CheckEmailAddress($email_address) {
        $connection = mysql_connect('localhost:3306','root','')or die('Error connecting');
        mysql_select_db('buyme') or die('Connection not working properly');     
        $query = "SELECT email_address from users where email_address='$email_address'";
        $result = mysql_query($query, $connection);
        $row = mysql_fetch_assoc($result);
    
        if(!$row && $row["email_address"] == '') {
            $emailInUse = TRUE;
        }else{
            $emailInUse = FALSE;  
        }
    
        return $emailInUse;
    }
    

    That said, I would recommend avoiding an else statement by doing this:

        $emailInUse = FALSE;        
        if(!$row || $row["email_address"] == '') {
            $emailInUse = TRUE;
        }
    
        return $emailInUse;
    

    Might not seem like a big difference, but from my experience, initing values like this with default values saves you headaches in the long run. Might not mean much in this script, but if you are a beginning programmer it’s a habit I recommend you getting into early on.

    EDIT WITH ADDITIONAL PERSPECTIVE: Also, I just re-read your post. You seem to want to return the words “true” and “false”? Bad habit. Let the function return a logically TRUE or FALSE as I explained & then act on it in your main code. So this coding:

    <?PHP
        require_once("membership.php");
        $membership = new Membership();
        $val = $_POST["email"];;
        $result = $membership->CheckEmailAddress(trim($val));
        echo json_encode($result); //output test@test.com   
    ?>
    

    Would now look like this:

    <?PHP
        require_once("membership.php");
        $membership = new Membership();
        $val = $_POST["email"];;
        $result = $membership->CheckEmailAddress(trim($val));
        $result_value = !empty($result) ? 'true' : 'false';
        echo json_encode($result_value); //output test@test.com   
    ?>
    

    See that line that reads $result_value = !empty($result) ? 'true' : 'false';? That is an inline bit of code that is an alternative to if/else stuff for values. I have genuine forgotten the formal name for that method, but it is useful for cases like this. The log flow is basically

    [value] = [test] ? [test value] : [default value];
    

    That basically means if test passes, make the value what comes after the ?. If the test does not pass? then use the default value to the right of the :.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to validate an e-mail address using javascript. The problem is the
I was trying to validate username whether exist or not with the following code.
Just trying to get some opinions on whether or not CommandHandlers can/should communicate with
I'm trying to validate a field if a file fields is not empty. So
I'm trying to make my contact form submission process work equally well whether the
I'm trying to write a method that checks whether a date is valid. It
I have a textarea that I am trying to validate whether someone has filled
I am trying to figure out how to check a String to validate whether
Im trying to validate the input to see if it a valid IP address(could
I'm trying to validate some POST data. One of the validations I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.