0

I am using this below code for check customer exist in database or not. it's working fine but it reload the page. can anyone suggest some ajax for reloading

<script type="text/javascript">
function emailcheck(email){
document.getElementById("myform").submit();
}

</script>

<?php //print_r($_POST['email']);

 echo $email=$_POST['email'];
$con = mysql_connect("localhost","root","password");

   if (!$con)
  {
     die('Could not connect: ' . mysql_error());
  }
       mysql_select_db("magento", $con);

        $sql = "SELECT `email` FROM `customer_entity` WHERE `email`='".$email."'" or die(mysql_error());
       $select = mysql_query($sql,$con);

       $result=mysql_fetch_array($select);


      echo $row = mysql_num_rows($select);

            if ($row > 0)  
          {
            echo 'This email Id is already exixst';  
          }
             else 
          {
             echo 'unavailable';
          } 
?>
        <form id="myform" name ="myform" method="POST">
                <input STYLE="WIDTH: 68%;MARGIN-LEFT: 12%;background-color: white;" placeholder="Email Address" type="email" name="email" id= "email" class="input-text " required />   

                <button class="button" onclick="emailcheck();"/> <span><span><?php echo $this->__('Submit') ?></span></span> </button>
        </form>
2
  • 1
    Why are you tryin to include an e-mail check? Is that intended to be used by your customers in the frontend? That is actually bad as I can use it to retrieve information about your registered customers and then brute-force their passwords. Sep 4, 2015 at 14:24
  • 1
    No need to bruteforce. The above code can be sql injected to get anything you could possibly want. Sep 5, 2015 at 1:27

3 Answers 3

2

Let me stop you there, you don't connect directly in your database like this, this is very bad practice, and can potentially be dangerous for your store if you expose database details.

Follow this guide, it covers in details what you are after: http://www.atwix.com/magento/ajax-validation/

1

Despite the fact that this is very bad for security if this is intended to be used by your customers in the frontend: It can be used to retrieve information about your registered customers and then brute-force their passwords.

Apart from that you need the following to create such a AJAX check:

  1. Create a custom controller where you send the request to
  2. Call that custom controller via AJAX request
0

Since you are using magento framework, you need to do it the 'correct' way

To check if email customer email exist

$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());

$customer->loadByEmail($email);

if ($customer->getId()) {
    //true
}
else{
  //false
}

Since magento uses prototype JS and the validation library, you could create you own validation class see Magento form field AJAX validation

1
  • how to get value of textbox and check by submit button
    – Magento 2
    Sep 7, 2015 at 5:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.