Rss Feed
  1. Page reload using onclick event

    Sunday, 14 October 2012

    Onclick page reload using js

     onclick="document.location.reload(true)"

  2. Simple mail sending in PHP, AJAX

    Saturday, 13 October 2012

     HTML Form

    <form action="sendmail.php" method="post">
    <div style="margin:10px 0;"> Name.</div>
     <input name="username" id="username" type="text" style="width:300px; height:35px;" />         
    <span id="userspan"></span>

    <div style="margin:10px 0;">Contact No.</div>
    <input name="contact" id="contact" type="text" style="width:300px; height:35px;" />
    <span id="contactspan"></span>

    <div style="margin:10px 0;">Company Name</div>
    <input name="company" id="company" type="text" style="width:300px; height:35px;" />
    <span id="companyspan"></span>

    <div style="margin:10px 0;">Email</div>
    <input name="email" id="email" type="text" style="width:300px; height:35px;" />
    <span id="emainspan"></span>

    <div style="float:left; margin:15px 0 0 110px; padding:0;"><a onclick="return sendcontact()"><img src="img/submitbutton.png" width="82" height="28" alt="submit" /></a></div>
    </div>
    </form>



    Javascript Ajax Call

    <script type="text/javascript">

      function sendcontact()
      {
          document.getElementById('userspan').innerHTML = '';
          document.getElementById('contactspan').innerHTML = '';
          document.getElementById('companyspan').innerHTML = '';
          document.getElementById('emailspan').innerHTML = '';
         
          var name = document.getElementById("name").value;
          var comp = document.getElementById("company").value;
          var con = document.getElementById("contact").value;
          var email = document.getElementById("email").value;

          if((name == "" || name == null))
          {
              document.getElementById('name').focus();
              document.getElementById('userspan').innerHTML = 'Please Provide your Name';
              return false;
          }
         
          else if((con == "" || con == null))
          {
              document.getElementById('contact').focus();
              document.getElementById('contactspan').innerHTML = 'Please Provide your Contact No';
              return false;
          }
         
          else if((comp == "" || comp == null))
          {
              document.getElementById('company').focus();
              document.getElementById('companyspan').innerHTML = 'Please Provide your Company name';
              return false;
          }


          else if((email == "" || email == null))
          {
              document.getElementById('email').focus();
              document.getElementById('emailspan').innerHTML = 'Please Provide your Company name';
              return false;
          }
       
          if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
          }
          else
          {
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
         
          xmlhttp.onreadystatechange=function()
          {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
              {
                  document.getElementById("contactres").innerHTML=xmlhttp.responseText;
              }
          }
         
          var str = name + '&con=' + con + '&comp=' + comp + '&email=' + email;
          xmlhttp.open("GET","sendmail.php?name="+str,true);
          xmlhttp.send();
      }
      </script>




    PHP Script

    <?php

    $name = $_GET['name'];
    $contact = $_GET['con'];
    $company = $_GET['comp'];
    $email = $_GET['email'];

    $to = 'example1@example.com, example2@example.com';
    $message = "Hi, You have got a contact from Your Web" . "\n" . "\n";
    $message .= "Name : " . $name . "\n" . "Company : " . $company . "\n" . "Contact Number : " . $contact . "\n";
    $subject = "Mail from your website";
    $from = $email;
    $headers .=  "From: ". $from;

    if(mail($to,$subject,$message,$headers))
    {
        echo "Thank you for your interest. We will get in touch with you shortly.";
    }
    else
    {
        echo "Sorry, there exist some problem. Please try again later.";
    }
    ?>

  3. To hash password in model

    Security::hash($password, 'sha1', true) ;
    AuthComponent::password($password) ;

     To check password with confirm password and to reset password

    public $validate = array(
        'name' => array(
                'notEmpty' => array(
                    'rule' => 'notEmpty',
                    'message' => 'This field cannot be left blank.',
                    'last' => true,
                ),
                'validName' => array(
                    'rule' => '_validName',
                    'message' => 'This field must be alphanumeric',
                    'last' => true,
                ),
            ),
            'username' => array(
                'isUnique' => array(
                    'rule' => 'isUnique',
                    'message' => 'The username has already been taken.',
                    'last' => true,
                ),
                'notEmpty' => array(
                    'rule' => 'notEmpty',
                    'message' => 'This field cannot be left blank.',
                    'last' => true,
                ),
                'validAlias' => array(
                    'rule' => '_validAlias',
                    'message' => 'This field must be alphanumeric',
                    'last' => true,
                ),
            ),
           
            'pwd' => array(                                                                           // password field
                'rule' => array('minLength', 6),
                'message' => 'Passwords must be at least 6 characters long.',
            ),
            'password2'  => array('rule' =>'checkpasswords','message' => 'Passwords dont match'), //confirm password field
            'oldpwd'  => array('rule' =>'checkoldnewpasswords','message' => 'Old and New Passwords dont match'), // oldpassword that to be changed
            'email' => array(
                'email' => array(
                    'rule' => 'email',
                    'message' => 'Please provide a valid email address.',
                    'last' => true,
                ),
                'isUnique' => array(
                    'rule' => 'isUnique',
                    'message' => 'Email address already in use.',
                    'last' => true,
                ),
            ),
               
           
           
        );
       

        function checkpasswords()     // to check pasword and confirm password
        {  //print_r($this->data['User']['id']);
        if(strcmp($this->data['User']['pwd'],$this->data['User']['password2']) == 0 )
        {
            return true;
        }
        return false;
        }
       
        function checkoldnewpasswords()   // to check password and old password
        {
          
            $this->id = $this->data['User']['id'];
            $user_data = $this->field('password');      
            print_r(Security::hash($this->data['User']['oldpwd'], 'sha1', true));
           if ($user_data == (Security::hash($this->data['User']['oldpwd'], 'sha1', true)))
           {
         return true;
         }else{
         return false;
         }
            }
             


    To assign password to correct database field name
    Here using   pwd i getting password in view so assigning this to database password field.

    function beforeSave() {
        parent::beforeSave();
        //print_r(($this->data['User']['newPassword']));
        //if (isset($this->data['User']['pwd']) && !empty($this->data['User']['pwd'])){
           // $this->data['User']['password'] = Security::hash($this->data['User']['pwd'], 'sha1', true);
          
            if (!empty($this->data[$this->alias]['pwd'])) {                 // hashing password with sha1 and cake salt
            $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['pwd']);
       
            }
        return true;
        }




    In view

    <?php echo $this->Form->input('oldpwd', array('type' => 'password','label' => '', 'class' => 'my-text-field-gen-11')); ?>

    <?php echo $this->Form->input('pwd', array('type' => 'password','label' => '', 'class' => 'my-text-field-gen-11')); ?>

    <?php echo $this->Form->input('password2', array('type' => 'password', 'label' => '','class' => 'my-text-field-gen-11')); ?> 


    Refer://http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp/


  4. Php functions

    Thursday, 11 October 2012

    To delete the empty key in array

    array_filter()

    Finds whether a variable is a number or a numeric string

    is_numeric

  5. Cakephp to get base url

    <?php echo $this->webroot; ?>

    <?php $this->base ?>

  6. Java script to reload a page in Html and Cakephp

    onclick="document.location.reload(true)"

    JS onclick event in html

    <a href="#" onclick="document.location.reload(true)"></a>

    Js onClick event in cakephp

    <?php echo $this->Html->image('image.png',array('onClick' => "document.location.reload(true)"),array('escape' => false)); ?>

    Reload function in javascript

     location.reload();
     window.setTimeout('location.reload()', 3000);