Create search engine for your web site

Create search engine for your web site, you can do it using Google api or Google search engine. Here is simple code which search data from Google.

 


<script>
(function() {
var cx = '014156408698225973323:bqmlsazztmc';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script>
<gcse:search></gcse:search>

put this code on your web site . then you will see output.

Please give a comment if you face any problem.

Simple ajax post with done function

Sometime you need to check value without refresh a page. You can do it using ajax method. Here is simple code. First create a simple html file from where ajax request send.Create simple php file name test.php


<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>
<script>
function test_ajax()
{
var x = new Date();
var h = x.getHours();
var m = x.getMinutes();
var s = x.getSeconds();
time = h+":"+m+":"+s;
$.post( "http://localhost/ajax_file.php", { name: "shafiq", time: time })
.done(function( data ) {
alert( "Result: " + data );
});
}
</script>
</head>
<body>
<a href="javascript:void('0')" onclick="test_ajax()" style="cursor: pointer"> Click Here For Check Ajax</a>
</body>
</html>

 

then create your request file. here we add ajax_file.php so create ajax_file.php. Here simple code

 


<?php
echo $_POST['name']."\n";
echo $_POST['time'];
?>

when you click Click Here For Check Ajax then you will see the name and time without refresh a page.

 

if you face any problem then give a comment.

How can I load jQuery if it is not already loaded?

Sometimes need to check that in a page jquery already loaded then no need extra jquery or not loaded then need to load jquery, you can do it using simple jquery code. Here is the code

<script type="text/javascript">
 var baseurl="shafiqul.wordpress.com";
 var doc=document.getElementsByTagName("head").item(0);
 var jQueryScriptOutputted = false;
 var jQueryScriptOutputtedplaceholder = false;
 window.onload=function()
 {
 initJQuery();
 }
 function initJQuery() {
 if (typeof(jQuery) == "undefined") {
 if (! jQueryScriptOutputted) {
 jQueryScriptOutputted = true;
 includeJs("/js/jquery-1.4.4.min.js", true);
| }
 }
 if (! jQueryScriptOutputtedplaceholder) {
 jQueryScriptOutputtedplaceholder = true;
 includeJs("/scripts/html5_placeholder.js", true);
 }
 setTimeout("initJQuery()", 100);
 }
 function includeJs(fileName, jQ, head){
 var js=document.createElement("script");
 js.setAttribute("language", "javascript");
 js.setAttribute("type", "text/javascript");
 js.setAttribute("src", baseurl+fileName);
 if(jQ===true){
 doc.insertBefore(js, doc.firstChild);
 return false;
 }
 if(head===true){
 doc.appendChild(js);
 return false;
 }
 document.getElementsByTagName("body").item(0).appendChild(js);
 return false;
 }
 </script>

if you find any problem then please give a comment.

Auto Reload And Clear Cache Using Javascript

Hi, sometime you need to do auto reload page after few time and auto clear cache . You can do it easily javascript setInterval function. Here is the simple code which auto reload after 3 second and clear cache.

 


<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>
<script>
$(document).ready(function(){
setInterval(function(){cache_clear()},3000);
});
function cache_clear()
{
window.location.reload(true);
}
</script>
</head>

<body>
<p>Auto reload and clear cache</p>
</body>
</html>

&nbsp;

if you face any problem then please give a comment.

Checked checkbox is checked or not using PHP

Sometime need to check that checkbox ix checked or not from a checkbox array. You can do it using simple php code.
Here is the total code


<?php
if(isset($_POST['submit']))
{
  if(isset($_POST['submit']))
    {
       // $x = cout($_POST['checkbox']);
        for($x = 0;$x <= 4;$x++)
        {
            if(strlen($_POST['checkbox'][$x])>1)
            {
                echo "Checkbox is cheked<br>";
            }
            else{
                echo "Checkbox not checked<br>";
            }
        }
    }


}


?>

<html>
<head>

</head>
<body>
<form enctype="multipart/form-data" action="" method="POST">
    <div id="text">
        <input type="checkbox" name="checkbox[0]"  />Checkbox1<br>
        <input type="checkbox" name="checkbox[1]"  />Checkbox2<br>
        <input type="checkbox" name="checkbox[2]"  />Checkbox3<br>
        <input type="checkbox" name="checkbox[3]"  />Checkbox4<br>
        <input type="checkbox" name="checkbox[4]"  />Checkbox5<br>

    </div>
    <input type="submit" value="Submit" name="submit" id="submit" />
</form>
</body>
</html>

Hope this is helpful. If you find any problem then give a comment. Thanks

The max height from a set of elements

To determine the maximum height from a set of elements, you can do it easily using normal jquery map function. Here is simple code
Html code

<div>
    <div class="testdiv">
      Line 1<br>
      Line 2
    </div>
    <div class="testdiv">
      Line 1<br>
      Line 2<br>
      Line 3<br>
      Line 4
    </div>
    <div class="testdiv">
      Line 1
    </div>
    <div class="testdiv">
      Line 1<br>
      Line 2
    </div>
</div>
var heights = $("div.testdiv").map(function ()
    {
        return $(this).height();
    }).get(),

    maxHeight = Math.max.apply(null, heights);

or

var highest = null;
var hi = 0;
$(".testdiv").each(function(){
  var h = $(this).height();
  if(h > hi){
     hi = h;
     highest = $(this);  
  }    
});

highest.css("background-color", "red");

if you face any problem then please give a comment.

Check Current URL Using Jquery

Sometime need to check your web site current url. Yon can do it using jquery . Here is simple code


var url = window.location.href;
var host = window.location.host;
if(url.indexOf('http://' + host + '/contact') != -1) {
   //match url
}

also php you can do it

   $filename = basename($_SERVER['REQUEST_URI']);

$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

If you face any problem then give a comment

skrwebdesigner

Designer Tutorial

php tutorial

Code and examples of php,javascript,jquerymobiles,yii,codeigniter

Follow

Get every new post delivered to your Inbox.