Saturday, March 5, 2016

Display Other Texts rather then English using PHP MySQL


When we want to display and store the data other than ENGLISH using PHP.

Below is the code used to save and retrieve the data in PHP.

mysql_set_charset('utf8',$db_connect);

where $db_connect is the "MySQL Host" connection handler. You can see the example below for the connection handler.


<?php
$db_connect = mysql_connect(HOST_NAME,USERNAME,PASSWORD);
mysql_set_charset('utf8',$db_connect);
?>


By using the above, we can store and retrieve the data in different languages like Telugu, French, Hindi, German and many.

Note: And also a major note we need to be keep in mind, when we try to retrieve the same UTF-8 converted data using AJAX. We should not use "mysql_set_charset" as AJAX by default sets the content as "UTF-8" 

Monday, February 29, 2016

Disable Back Button using Javascript




User tries to use back button for most of the applications and it is valid, but few of the cases is required to disable the back button and also restrict the user.

It would be very useful when the user is on payment page. Below is the code to disable the back button in Browser.

var currentURL = window.location.pathname + window.location.search;
 history.pushState(null, null, currentURL);
 window.addEventListener('popstate', function(event) {
  history.pushState(null, null, currentURL);
 });

It works in all major browsers

Tuesday, July 7, 2015

Trigger onchange event manually using Javascript


Below is the code to trigger onchange event for an element using javascript.

Usage of the method can be, when we want to call the onchange event on page loads or on by changing the text box value for other textbox.


if(document.fireEvent) {
    document.getElementById("myElement").fireEvent('onchange');
} else {
    var event = document.createEvent("HTMLEvents");
    event.initEvent("change",true,false);

    document.getElementById("myElement").dispatchEvent(event);
}

Whereas in jQuery, we can simply call the below method.

jQuery('myElement').change();

Thursday, July 24, 2014

Javascript: trim function support in all browsers



In Javascript, when we want to remove spaces in a string, we would be using trim function which is supported in all major browsers. See below code to use trim function in Javascript.

var str = "     message to trim    ";
document.write(str.trim());  // this code will prints as message to trim in the body.

But the above trim() function doesnot support in IE. To support the same functionality in IE too, add the below code to work as expected.

if (typeof String.prototype.trim !== "function") {
    String.prototype.trim = function () {
        return this.replace(/^\s+|\s+$/g, "")
    }
}