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());
}
The issue is your
TRUE&FALSEassignments are returning strings & notTRUEorFALSEvalues. Meaning they will always essentially be seen asTRUEbecause they are strings that contain values. AFALSEwill always contain nothing. Also, you are doing anORby doing||when it should be anANDby doing&&. So this snippet of code:Should be rewritten like this:
And the final function would look like this:
That said, I would recommend avoiding an
elsestatement by doing this: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
TRUEorFALSEas I explained & then act on it in your main code. So this coding:Would now look like this:
See that line that reads
$result_value = !empty($result) ? 'true' : 'false';? That is an inline bit of code that is an alternative toif/elsestuff for values. I have genuine forgotten the formal name for that method, but it is useful for cases like this. The log flow is basicallyThat basically means if
testpasses, make the value what comes after the?. If the test does not pass? then use the default value to the right of the:.