Presto-X Posted April 12, 2012 Share Posted April 12, 2012 Hello everyone, I'm working on a custom Joomla user registration form, and I need to make sure that both the username and email address are unique. I have the username working great, but I'm having a problem with the email address, the script keeps removing the AT @ symbol from the search so it returns that the address is available when it's not. This is driving me crazy... I'm not sure if it's the JavaScript or the PHP. On to the code: window.addEvent("domready",function(){ var box = $('emailStatus'); var cun = document.id('email1'); $(cun).addEvent("blur",function(){ if ( $(cun).value.length > 0 ){ var url="index.php?option=com_conferencesystem&format=raw&view=checkavailability&email="+$(cun).value; box.style.display="block"; box.set('html','Checking email availability...'); var a=new Request.JSON({ url:url, onComplete: function(response){ box.set('html',response.html + '</span>'); } }); a.get(); } }); }); function chkEmail(){ if($email = JRequest::getVar('email','','get','cmd')){ $db =& JFactory::getDBO(); $query = 'SELECT id FROM #__users WHERE email = ' . $db->Quote($email); $db->setQuery($query); $result=$db->loadObject(); if ($result){ $response['html'] = '<span class="unavailable">' . $email . ' Is Not Available!</span>'; $response['msg'] = 'false'; }else{ $response['html'] = '<span class="available">' . $email . ' Is Available!</span>'; $response['msg'] = 'true'; } }else{ $response['html'] = '<span class="invalid">' . $email . ' Is An Invalid Email...</span>'; $response['msg'] = 'false'; } echo json_encode($response); return true; } if(JRequest::getVar('email','','get','cmd') != ''){ chkEmail(); } Quote Link to comment https://forums.phpfreaks.com/topic/260779-check-email-availability-make-sure-email-is-unique-in-the-database/ Share on other sites More sharing options...
freelance84 Posted April 14, 2012 Share Posted April 14, 2012 encodeURIComponent() maybe the thing that is missing there... This function encodes special characters and encodes the following characters: , / ? : @ & = + $ # try var emailToCheck = encodeURIComponent( $(cun).value ); You don't need to decode it on your server either, that's done automatically... Hope thats all it is Quote Link to comment https://forums.phpfreaks.com/topic/260779-check-email-availability-make-sure-email-is-unique-in-the-database/#findComment-1337223 Share on other sites More sharing options...
Presto-X Posted April 14, 2012 Author Share Posted April 14, 2012 Hello John, Thanks for the reply, I ended up getting it to work late last night, I found that was the problem, and that the PHP on the Joomla side was also removing any symbols from the var, I found if I removed CMD from the getVar it started working. So I had JavaScript encode the URL and then PHP decode it. Below is the working code, maybe it will help others down the road. window.addEvent("domready",function(){ var box = $('emailStatus'); var cun = document.id('email1'); $(cun).addEvent("blur",function(){ if ( $(cun).value.length > 0 ){ var email = encodeURIComponent( $(cun).value ); var url = "index.php?option=com_conferencesystem&format=raw&view=checkavailability&email=" + email; box.style.display="block"; box.set('html','Checking email availability...'); var a=new Request.JSON({ url:url, onComplete: function(response){ if(response.msg == "true"){ box.set('html', '<span class="available">' + response.html + '</span>'); cun.setAttribute("class", "inputbox textfieldValidState"); }else{ box.set('html', '<span class="unavailable">' + response.html + '</span>'); cun.setAttribute("class", "inputbox textfieldRequiredState"); } } }); a.get(); } }); }); function chkEmail($email){ $db =& JFactory::getDBO(); $query = "SELECT id FROM #__users WHERE email = '" . $email . "'"; $db->setQuery($query); $result=$db->loadObject(); if ($result){ $response['html'] = $email . ' Is Not Available!'; $response['msg'] = 'false'; }else{ $response['html'] = $email . ' Is Available!'; $response['msg'] = 'true'; } echo json_encode($response); return true; } if($email = rawurldecode(JRequest::getVar('email','','get'))){ chkEmail($email); } Quote Link to comment https://forums.phpfreaks.com/topic/260779-check-email-availability-make-sure-email-is-unique-in-the-database/#findComment-1337256 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.