Showing posts with label tutorials. Show all posts
Showing posts with label tutorials. Show all posts
  • Tuesday, May 29, 2012
  • | Kathir 0
  • Handy newsletter signup in javascript with ajax and PHP



    Ajax is a popular technology in web development with the combination of Javascript and DOM, Ajax will do wonders. Here I’m not talking about ajax technology, just explaining how newsletter signup can be done with ajax and core javascript along with PHP. In this example I have used onclick javascript event to call an Ajax request.

    Create an HTML page, say for example newsletter.html which is the user communication page and ajax.js javascript page for handling client side scripting and finally ajax.php for communicating with server.

    Add the below HTML Code in newsletter.html

    <form name="formNewsletter" id="formNewsletter" action="" method="post"><input type="text" name="email" id="email" value="" /><input type="button" name="btnSubmit" id="btnSubmit" value="Signup" onclick="ajaxRequest('newsletter_signup','divSignupMessage');"/></form><div id="divSignupMessage">Submit your email address to receive the latest updates</div>; 

    Here "newsletter_signup" is an unique process name, which i have used for handling the request based on the process name. And div id divSignupMessage used for displaying the output received from server.

    Now create another file called  ajax.js and add the below code into it.

    //Ajax Request Handler
    function xmlhttpPost(url,method,divname,qstr){
    var 
    xmlHttpReq = false;
    var 
    self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest){
    self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject){
    self.xmlHttpReq = new ActiveXObject('Microsoft.XMLHTTP');
    }
    self.xmlHttpReq.open(method, url, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function(){
    if (
    self.xmlHttpReq.readyState == 4){
    updateDiv(self.xmlHttpReq.responseText,divname);//div update function call
    }
    }
    self.xmlHttpReq.send(qstr);
     }

    //Updating output
    function updateDiv(txtvalue,divname){
    if(
    divname){
    document.getElementById(divname).innerHTML='';
    document.getElementById(divname).innerHTML=txtvalue;
    }
    }


    function 
    ajaxRequest(process,divname){
    if(
    process =='newsletter_signup'){
    email    = document.getElementById('email').value;
    qstr     = 'p=' + escape(process) + '&email=' + escape(email);
    xmlhttpPost('ajax.php','POST',divname,qstr);
    }
    }

    Finally add below PHP Code in ajax.php file.

    $process = trim($_REQUEST["p"]);
    $output = "";

    if(
    $process == "newsletter_signup"){$email = mysql_real_escape_string(trim($_REQUEST["email"]));
    $email_validation = ereg("^[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[@]{1}[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[.]{1}[A-Za-z]{2,5}$", $email);

    if($email_validation<1){//Email validation error message
    $output = "Please enter valid email address";
    }elseif(
    $email_validation>=1){ //Already exist email error message
    $sql = "SELECT `email` FROM `newsletter` WHERE `email`=".$email." LIMIT 1";
    $res = mysql_query($sql);
    $tot = mysql_num_rows($res);

    if(
    $tot>0){
    $output = "Email address already exist";
    }else{

    $output = "TRUE";
    }
    }
    echo 
    $output;
    exit;
    }
    //End 

    Check out the online example and download the working example.


    [ Read More ]
  • Friday, May 25, 2012
  • | Kathir 0
  • Add minutes or hours to current time mysql


    Writing a SQL query is always interesting, and for me it’s never boring task. Recently I was developing a CRM for my company, In that I need to send mail to the users who have scheduled their task as a reminder so I taught of using cron job for the same.Well now I need to fetch the users to schedule the mail, here is the challenge; I have to set the cron job to run the file every 15 minutes, so I have to take out the users from current time to next 15 minutes.

    We can achieve this by PHP, but I want to do it via mysql itself. After quick googling, I found a function called DATE_ADD().


    Syntax:

    DATE_ADD(date_time,INTERVAL expr type)

    My Query:

    SELECT * FROM table_task WHERE field_remainder BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 15 MINUTE)

    So adding 15 minutes to the current time becomes easier now by using the DATE_ADD() function. Likewise you can also add days to the date. Check here for more detailed information on DATE_ADD() function.

    [ Read More ]
  • Saturday, May 05, 2012
  • | Kathir 2
  • Sorting Alphanumeric values in MySQL


    Yesterday I got query from a visitor of my blog, he wants to do a sorting were the data stored in the field is alphanumeric. Normally this type of sorting we call it as "Natural Sort". He also gave me the structure of the data.



    Data
    Here is an example how the column looks.

    trip_id
    TRPAN1000
    TRPAN999
    TRPAN1002
    TRPAN998

    Now he wants to sort the above column in following order.

    trip_id
    TRPAN1002
    TRPAN1000
    TRPAN999
    TRPAN998

    Problem

     SELECT trip_id FROM test_table ORDER BY trip_id DESC 

    trip_id
    TRPAN1000
    TRPAN1002
    TRPAN998
    TRPAN999

    Sorting by alphanumeric column with normal ORDER BY query will end in unexpected result.

    Solution
    Since first 5 characters are common in trip_id field, I have used SUBSTR function to remove those characters and then used CAST function to sort the integer values which is in the form of varchar datatype.

     SELECT trip_id FROM test_table ORDER BY CAST(SUBSTR(trip_id,6) AS SIGNED) DESC 

    Here is our expected result.

    trip_id
    TRPAN1002
    TRPAN1000
    TRPAN999
    TRPAN998


    [ Read More ]
  • Saturday, March 10, 2012
  • | Kathir 0
  • Instant Character Count like Twitter with jQuery



    In this post I would like to explain how to do character count in jQuery. I have did the same thing in many of my projects to enrich my web applications. This will be very help full for you in comment section to restruct the user to limited character.

    [ Read More ]
     
    Copyright (c) 2011 - 2021 techispeaks.com. All rights reserved the content is copyrighted to Kathirason Asokan
    Creative Commons License