Showing posts with label Tips & Tricks. Show all posts
Showing posts with label Tips & Tricks. Show all posts
How to disable the browser back button navigation using javascript

How to disable the browser back button navigation using javascript

Hi, Today you will learn about "How to disable the browser back button navigation using javascript", In general, we don't use this type of requirement in the project.But is very useful in so many situations.

For example, we are doing in the online cart i.e an e-commerce platform so here after the placing the order and once the user checks out the order in his cart and payment completed it will redirect and shown the order success page after that user doesn't go back to the previous pages. So here we want to block the browser back button navigation.

So here in this situation we will use this code. if you searched on the internet you will find the code i.e
<script type="text/javascript">  
     window.history.forward();  
     function noBack()  
     {  
       window.history.forward();  
     }  
 </script>  
 <body onLoad="noBack();" onpageshow="if (event.persisted) noBack();" onUnload="">   
This code is not working properly. so I have found the solution to this problem. Just add the below script code on the file where the user doesn't go back from that file.
<script type="text/javascript">  
 (function (global) {   
   if(typeof (global) === "undefined") {  
     throw new Error("window is undefined");  
   }  
   var _hash = "!";  
   var noBackPlease = function () {  
     global.location.href += "#";  
     // making sure we have the fruit available for juice (^__^)  
     global.setTimeout(function () {  
       global.location.href += "!";  
     }, 50);  
   };  
   global.onhashchange = function () {  
     if (global.location.hash !== _hash) {  
       global.location.hash = _hash;  
     }  
   };  
   global.onload = function () {        
     noBackPlease();  
     // disables backspace on page except on input fields and textarea..  
     document.body.onkeydown = function (e) {  
       var elm = e.target.nodeName.toLowerCase();  
       if (e.which === 8 && (elm !== 'input' && elm !== 'textarea')) {  
         e.preventDefault();  
       }  
       // stopping event bubbling up the DOM tree..  
       e.stopPropagation();  
     };       
   }  
 })(window);  
 </script>  
Here you can check the demo.
Demo
* If you like this post please don't forget to subscribe TechiesBadi - programming blog for more useful stuff
How to read the .docx files in PHP

How to read the .docx files in PHP

Hi, In This tutorial you will learn about "How to read the .docx files in PHP". Generally .docx files will be opened in the MS-OFFICE, But we are able to open the .docx file in PHP we have to convert the .docx file into text then we can easily display the content in the web browser.

<?php  
 function read_file_docx($filename){  
      $striped_content = '';  
      $content = '';  
      if(!$filename || !file_exists($filename)) return false;  
      $zip = zip_open($filename);  
      if (!$zip || is_numeric($zip)) return false;  
      while ($zip_entry = zip_read($zip)) {  
      if (zip_entry_open($zip, $zip_entry) == FALSE) continue;  
      if (zip_entry_name($zip_entry) != "word/document.xml") continue;  
      $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));  
      zip_entry_close($zip_entry);  
      }// end while  
      zip_close($zip);  
      $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);  
      $content = str_replace('</w:r></w:p>', "\r\n", $content);  
      $striped_content = strip_tags($content);  
      return $striped_content;  
 }  
 $filename = "sample.docx";// or /var/www/html/file.docx  
 $content = read_file_docx($filename);  
 if($content !== false) {  
      echo nl2br($content);  
 }  
  else {  
      echo 'Couldn\'t the file. Please check that file.';  
           }  
 ?>  

* If you like this post please don't forget to subscribe TechiesBadi - programming blog for more useful stuff
How to Refresh a page after specific time automatically in PHP

How to Refresh a page after specific time automatically in PHP

Hi, In This tutorial you will know about How to Refresh a page after specific time automatically in PHP. Generally, we did this type of implementation in the javascript or jquery but sometimes javascript is disabled in the user's side (i.e users disabled the javascript in their browsers).

At this situation, we need to implement that code in PHP
it is very simple first we need to fix the specific time for the refresh the website.
In general, the header is used to redirect the web page in PHP by using the location. But here we will use the header in the Refresh tag to set the time and web page URL to refresh the web page.

 //getting the current web page url
 $page = $_SERVER['PHP_SELF'];
 //set the time in seconds
 $sec = "10";
 header("Refresh: $sec; url=$page");
* If you like this post please don't forget to subscribe TechiesBadi - programming blog for more useful stuff
How to Export and Download the Mysql Table data to CSV File in PHP

How to Export and Download the Mysql Table data to CSV File in PHP

Hi, You will learn in this tutorial How to Export and Download the Mysql Table data to CSV File in PHP. Sometimes you need to Export the user's data to CSV file because if a client wants to send the emails from the Autoresponders like Aweber, GetResponse, Mailchimp, etc..

In this case, you need to provide the user's contact information in the form of CSV file. It is a comma separated values so the human can read easily and exported CSV file can be imported on the Auto responders easily.

Related to Read : How to Upload CSV File into Database Using PHP and MYSQL

MySql Users Table
CREATE TABLE `users` (
  `user_id` int(11) NOT NULL,
  `user_first_name` varchar(100) NOT NULL,
  `user_last_name` varchar(100) NOT NULL,
  `user_email` varchar(255) NOT NULL,
  `user_country` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `users`
  ADD PRIMARY KEY (`user_id`);

ALTER TABLE `users`
  MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT;
Exporting the MySql table data to CSV file follow the step by step process

Step #1: Connect to the MySql Database in PHP
First let's connect to the database using the mysqli() object.
 <?php  
 $servername = "localhost";  
 $username = "root";  
 $password = "";  
 $dbname = "leads";  
 // Create connection  
 $con = new mysqli($servername,$username, $password, $dbname);  
 // Check connection  
 if ($con->connect_error) {  
 die("Connection failed: " . $con->connect_error);  
 }  
 ?>  
Step #2: Select the table and its columns from the MySql database
After connecting to the database, we need to fetch the records from the table.
If you want to export the entire table then execute this SQL Query "SELECT * FROM `users`"
otherwise, you need the custom columns then specify the column names in the Query "SELECT `user_first_name`,`user_email` FROM `users`".  Here I am taking the entire table 'users'.
 <?php  
 // Fetch Records From Table  
 $sql = "SELECT * FROM `users`";  
 $result = $con->query($sql);  
 ?>  
Step #3: Converting the MySql Results to CSV file format
Now we have to convert the fetched records into CSV file one by one. First, we need to write the table headings after that write the each row of data into the CSV file.
 <?php  
 $output = "";  
 // Get The Field Names from the table  
 while ($fieldinfo=$result->fetch_field())  
 {  
 $output .= '"'.$fieldinfo->name.'",';  
 }  
 $output .="\n";  
 // Get Records from the table  
 while ($row = $result->fetch_array()) {  
 for ($i = 0; $i < $columns_total; $i++) {  
 $output .='"'.$row["$i"].'",';  
 }  
 $output .="\n";  
 }  
 ?>  
Here each and every table records are saved in the form of the comma separated value format in the $output.

Step #4: Download the Exported CSV file.
Here we need to specify the CSV file name and add the PHP headers application/csv and then add the attachment header this will force to downloaded the CSV file in the Browser.
 <?php   
 // Download the CSV file  
 $filename = "myFile.csv";  
 header('Content-type: application/csv');  
 header('Content-Disposition: attachment; filename='.$filename);  
 echo $output;  
 ?>  
Complete PHP code to Export and Download the MySql Table data to CSV File
 <?php  
 $servername = "localhost";  
 $username = "root";  
 $password = "";  
 $dbname = "leads";  
 // Create connection  
 $con = new mysqli($servername,$username, $password, $dbname);  
 // Check connection  
 if ($con->connect_error) {  
 die("Connection failed: " . $con->connect_error);  
 }  
 // Fetch Records From Table  
 $output = "";  
 $sql = "SELECT * FROM `users`";  
 $result = $con->query($sql);  
 $columns_total = mysqli_num_fields($result);  
 // Get The Field Names from the table  
 while ($fieldinfo=$result->fetch_field())  
 {  
 $output .= '"'.$fieldinfo->name.'",';  
 }  
 $output .="\n";  
 // Get Records from the table  
 while ($row = $result->fetch_array()) {  
 for ($i = 0; $i < $columns_total; $i++) {  
 $output .='"'.$row["$i"].'",';  
 }  
 $output .="\n";  
 }  
 // Download the CSV file  
 $filename = "myFile.csv";  
 header('Content-type: application/csv');  
 header('Content-Disposition: attachment; filename='.$filename);  
 echo $output;  
 exit;  
 ?> 
Here you can download the Full Source code and check the demo.

Download Demo
* If you like this post please don’t forget to subscribe Techies Badi - programming blog for more useful stuff