Browse Author: swati nuna

How to use PDO database connection in PHP

<?php
$string = ‘Test’; # user submitted data
try {
#connection
$conn = new PDO(‘mysql:host=localhost;dbname=myDB’, $db_username, $db_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$data = $conn->query(‘SELECT * FROM myTable WHERE name = ‘ . $conn->quote($string)); // $conn->quote used to protect SQL injection
foreach($data as $rows) {
print_r($rows);
}
} catch(PDOException $e) {
echo ‘ERROR: ‘ . $e->getMessage();
}
?>

How to create select box with searching support using jQuery

<html>
<head>
<title>How to create HTML select box with searching support using jQuery</title>
<link href=”select2.css” rel=”stylesheet”/>
<script src=”http://code.jquery.com/jquery-1.8.0.min.js”></script>
<script src=”select2-2.js”></script>
<script>
$(document).ready(function() {
$(“#states”).select2();
});
</script>
</head>
<body>
<select style=”width:300px” id=”states”>
<optgroup label=”Alaskan/Hawaiian Time Zone”>
<option value=”AK”>Alaska</option>
<option value=”HI”>Hawaii</option>
</optgroup>
<optgroup label=”Pacific Time Zone”>
<option value=”CA”>California</option>
<option value=”NV”>Nevada</option>
<option value=”OR”>Oregon</option>
<option value=”WA”>Washington</option>
</optgroup>
<optgroup label=”Mountain Time Zone”>
<option value=”AZ”>Arizona</option>
<option value=”CO”>Colorado</option>
<option value=”ID”>Idaho</option>
<option value=”MT”>Montana</option><option value=”NE”>Nebraska</option>
<option value=”NM”>New Mexico</option>
<option value=”ND”>North Dakota</option>
<option value=”UT”>Utah</option>
<option value=”WY”>Wyoming</option>
</optgroup>
<optgroup label=”Central Time Zone”>
<option value=”AL”>Alabama</option>
<option value=”AR”>Arkansas</option>
<option value=”SD”>South Dakota</option>
<option value=”TX”>Texas</option>
<option value=”TN”>Tennessee</option>
<option value=”WI”>Wisconsin</option>
</optgroup>
<optgroup label=”Eastern Time Zone”>
<option value=”CT”>Connecticut</option>
<option value=”MI”>Michigan</option>
<option value=”PA”>Pennsylvania</option>

<option value=”RI”>Rhode Island</option>

<option value=”SC”>South Carolina</option>
<option value=”VT”>Vermont</option>
<option value=”VA”>Virginia</option>
<option value=”WV”>West Virginia</option>
</optgroup>
</select>
</body>
</html>

Note:

Download select2.js, select2.css  files from below location

https://github.com/ivaynberg/select2/blob/master/select2.js

https://github.com/ivaynberg/select2/blob/master/select2.css

Why Use PHP?

There are thousands of programming languages available and every year, new programs are being made.  But only few of these have become sufficiently popular and are used by many people.   One of which is PHP or hypertext preprocessor.  It is a general-purpose server-side scripting language designed for web development.  It is installed on more than 20 million web sites and 1 million web servers.  Microsoft’s Active Server Page or ASP, a server-side script engine and a similar language to PHP, is one of PHP’s competitors.  They are both programming languages that are commonly used to create websites.  Both are more dynamic than the usual static html web pages and can allow users to interact and exchange information.

Here are some factors to consider in choosing between PHP and ASP.  ASP needs a Microsoft server for the website to work and will be requiring an ASP-Apache program installed for it to work on a Linux platform.  PHP, on the other hand, runs using Windows, Solaris, Linux or Unix and NT servers.  That’s why PHP is more popular than ASP.  PHP uses C/C++ as base language and most syntax are similar to each other while ASP is very similar to the syntax and interface of Visual Basic programming.  When it comes to cost and expenses, ASP is more expensive as it will require one to purchase Windows with IIS installed on the server and a MS-SQL database to work.  While PHP will only need a Linux server and connects to several databases, one of which is MySQL, which are both free.  Talking about speed, PHP runs it its own memory space which makes its loading speed quicker compared to ASP as it uses an overhead server.  Most tools that work on PHP program are open source software while one may need to purchase additional tools to work on ASP program.

Clearly, PHP has dominated when it comes to compatibility, popularity, cost, expenses, flexibility and a lot more but as users of the program, the choice is yours.

How to create CAPTCHA image verification in PHP and jQuery

index.php

File contains PHP code to load captcha image and text box to input visible word.

<?php
// Session start must be the first line, whether you include it or not 🙂
session_start();
?>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
<head>
<title>How to create CAPTCHA image verification in PHP and jQuery </title>
<meta http-equiv=”Content-Type” content=”text/html;charset=utf-8″ />
<style type=”text/css”>
img {border-width: 0}
* {font-family:’Lucida Grande’, sans-serif;}
</style>
<script type=”text/javascript” src=”jquery-1.8.0.min.js”></script>
<script type=”text/javascript”>
$(document).ready(function(){
$(“#new”).click(function() {
$(“#captcha”).attr(“src”, “captcha.php?”+Math.random());
});
});
</script>
</head>
<body>
<?php
if(empty($_POST))
{
echo ‘
<form method=”post” action=”index.php”>
<span style=”float: left;margin-top: 7px;margin-right:10px;”>CAPTCHA Code:</span>
<img src=”captcha.php” border=”0″ alt=”CAPTCHA!” id=”captcha”><a href=”#new” id=”new”><img src=”reload.png” style=”width: 35px;margin-left:10px;” /></a>
<br />
Enter CAPTCHA: <input type=”text” name=”key” value=”” />
<br /><br />
<input type=”submit” value=” Verify Captcha ” />
</form>’;
}
else
{
if(strlen($_SESSION[‘key’]) && $_POST[‘key’] == $_SESSION[‘key’])
{
echo “Captcha Verified!!!”;
}
else
{
echo “Invalid Captcha…. <a href=’index.php’>try again</a>”;
}
}
?>
</body>
</html>

captcha.php

File contains PHP Code to generate captcha image.

<?php
session_start();

class CaptchaSecurityImages {
/* select the type of font, must be used in directoy in which script is being called into */
var $font = ‘CALIBRI.TTF’;

function generateCode($characters) {
$possible = ‘23456789ABCDEFGHJKLMNPQRSTUVWXYZ’;
$possible = $possible.$possible.’2345678923456789′;
$code = ”;
$i = 0;
while ($i < $characters) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
return $code;
}

function CaptchaSecurityImages($width = 145,$height = 35, $characters = 6) {
$code = $this->generateCode($characters);
$font_size = $height * 0.60;
$image = @imagecreate($width, $height) or die(‘Cannot initialize new GD image stream’);

/* set the colours */
$bgR = mt_rand(0, 255); $bgG = mt_rand(0, 255); $bgB = mt_rand(0, 255);
$background_color = imagecolorallocate($image, $bgR, $bgG, $bgB);
$noise_color = imagecolorallocate($image, abs(100 – $bgR), abs(100 – $bgG), abs(100 – $bgB));
$text_color = imagecolorallocate($image, abs(255 – $bgR), abs(255 – $bgG), abs(255 – $bgB));

/* generate random dots in background */
for($i = 0; $i < ($width*$height) / 3; $i++) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
}

/* generate random lines in background */
for($i = 0; $i < ($width*$height) / 150; $i++) {
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
}

/* set random colors */
$w = imagecolorallocate($image, abs(100 – $bgR), abs(100 – $bgG), abs(100 – $bgB));
$r = imagecolorallocate($image, abs(100 – $bgR), abs(100 – $bgG), abs(100 – $bgB));

/* Draw a dashed line, 5 red pixels, 5 white pixels */
$style = array($r, $r, $r, $r, $r, $w, $w, $w, $w, $w);
imagesetstyle($image, $style);
imageline($image, 0, 0, $width, $height, IMG_COLOR_STYLED);
imageline($image, $width, 0, 0, $height, IMG_COLOR_STYLED);

/* create random polygon points */
$values = array(
mt_rand(0, $width), mt_rand(0, $height),
mt_rand(0, $height), mt_rand(0, $width),
mt_rand(0, $width), mt_rand(0, $height),
mt_rand(0, $height), mt_rand(0, $width),
mt_rand(0, $width), mt_rand(0, $height),
mt_rand(0, $height), mt_rand(0, $width),
mt_rand(0, $width), mt_rand(0, $height),
mt_rand(0, $height), mt_rand(0, $width),
mt_rand(0, $width), mt_rand(0, $height),
mt_rand(0, $height), mt_rand(0, $width),
mt_rand(0, $width), mt_rand(0, $height),
mt_rand(0, $height), mt_rand(0, $width),);

/* create Random Colors then set it to $clr */
$r = abs(100 – mt_rand(0, 255));
$g = abs(100 – mt_rand(0, 255));
$b = abs(100 – mt_rand(0, 255));
$clr = imagecolorallocate($image, $r, $g, $b);

/* create filled polygon with random points */
imagefilledpolygon($image, $values, 6, $clr);

$textbox = imagettfbbox($font_size, 0, $this->font, $code) or die(‘Error in imagettfbbox function’);
$x = ($width – $textbox[4])/2;
$y = ($height – $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die(‘Error in imagettftext function’);

/* pretty it */
imageantialias($image, 100);
imagealphablending($image, 1);
imagelayereffect($image, IMG_EFFECT_OVERLAY);

/* output captcha image to browser */
header(‘Content-Type: image/jpeg’);
imagejpeg($image);
imagedestroy($image);
$_SESSION[‘key’] = $code;
}
}

$captcha = new CaptchaSecurityImages(145, 35, rand(4, 6));

?>

Also you need jquery-1.8.0.min.js file.

 

Learning The Many Advantages of PHP

PHP, which is short for Hypertext Preprocessor, is an open source server side programming language available at free of cost that can be easily obtained from the market. It has a coding style that is quiet and easy to understand and is usually very proficient on multi-platforms like Windows, Linux, and UNIX and other more.

Today, PHP is one of the most popular server side scripting languages working. It is basically utilized for making dynamic web pages that correspond with the user offering customized information. Aside from being fast, stable, secure and easy to use, PHP offers other numerous advantages that had most developers using it.

Drag and drop items using jQuery

Required files

1) index.php

2) html.inc

3) jquery-1.8.0.min.js

index.php

<?php
$pre = 1;
$content =’
<div id=”drag”>
<div>
<div>
<h4>Title 1</h4>
</div>
<div>Content 1</div>
</div>
<div>
<div>
<h4>Title 2</h4>
</div>
<div>Content 2</div>

</div>
<div>
<div>
<h4>Title 3</h4>
</div>
<div>Content 3</div>
</div>
</div>

<script type=”text/javascript” src=”jquery-1.8.0.min.js”></script>
<script src=”http://code.jquery.com/ui/1.10.3/jquery-ui.js” type=”text/javascript”></script>
<script type=”text/javascript”>
$(“#drag”).sortable({
connectWith: “.box”,
items: “.box”,
opacity: 0.5,
tolerance: “pointer”
});
</script>’;

$pre = 1;
include(“html.inc”);
?>

<style type=”text/css”>
*{
font-family:Tahoma, Geneva, sans-serif;
}
h4{
color:#FFF;
}
.drag{
width:100%;
margin:0 auto;
}
.box{
padding:10px;
min-height:160px;
text-align:justify;
border:0px #CCC solid;
margin:5px;
}
.box-head{
cursor:move;
}
.black{
border:#000000 1px solid;
}
.black h4{
background-color:#000000;
padding:10px;
}
.red{
border:1px #FF0000 solid;
}
.red h4{
background-color:#FF0000; padding:10px;
}
.blue{
border:#0000FF 1px solid; }
.blue h4{
background-color:#0000FF;
padding:10px;
}
</style>

< —————————————————————————–>

html.inc
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
<head>
<title>Drag & Drop Demo</title>
<meta http-equiv=”Content-Type” content=”text/html;charset=utf-8″ />
<style type=”text/css”>
img {border-width: 0}
* {font-family:’Lucida Grande’, sans-serif;}
</style>
</head>
<body onload=”changePagination(‘0′,’first’)”>
<!– Place this tag after the last badge tag. –>
<p>
<?php if(!isset($pre)) {?>
<pre>
<?php print_r($content); ?>
</pre>
<?php }else{ ?>
<?php print_r($content); ?>
<?php } ?>
</p>

</body>
</html>

Learning The Many Advantages of PHP

PHP, which is short for Hypertext Preprocessor, is an open source server side programming language available at free of cost that can be easily obtained from the market. It has a coding style that is quiet and easy to understand and is usually very proficient on multi-platforms like Windows, Linux, and UNIX and other more.

Today, PHP is one of the most popular server side scripting languages working. It is basically utilized for making dynamic web pages that correspond with the user offering customized information. Aside from being fast, stable, secure and easy to use, PHP offers other numerous advantages that had most developers using it.

Before even starting any website, several aspects are considered including:

  • Website design
  • Web developing
  • Web hosting
  • Maintenance
  • Flexibility
  • Usability
  • Scalability
  • Easiness of handling and
  • Overall allocated budget

Advantages of PHP

PHP is an open source

As previously mentioned, PHP is readily available and totally free for you to use. If you have been planning to hire PHP developers who have excellent background in C and Java, you will notice how easy it is for them to use PHP. And truth be told, PHP is a lot simpler compared to the rest giving you assurance of outstanding outcomes. The chief PHP source repository is rich with modules and interfaces that users have written and donated. With that source, you can gladly search for modules for flash movies, PDF files, calendars and other more.

PHP is cross-platform.

  • PHP runs on just about every platform available enabling it to operate across different operating systems. It works excellently on UNIX, Macs and Windows versions.

PHP has connective abilities.

  • It uses a modular system of extensions to cross point with a collection of libraries like graphics, XML, encryption and other more. Furthermore, programmers can stretch out PHP by writing their own extensions and gathering them into the executable or they can make their own executable and load it with the use of PHP’s dynamic loading mechanism.

PHP offers a community.

  • You can easily share with other people in the PHP community since it is an open source project. If you are searching for a specific script, chances are another user has already made something similar. You can simply check within the PHP community for availability. In the same way, if you have made a function that others can benefit, be sure to post the code for others.
  • The PHP community is a huge one. Because of its popularity, there are many PHP developers, contributors, users and employers that gladly offer PHP jobs. You can easily find people who are looking for a PHP developer job.

PHP is fast and convenient.

  • It functions well with other software available and is known to be fast. PHP is also relatively stable. And because it is an open source, anyone on the PHP community can fix the bug.
  • When it comes to security, PHP offers several levels of security to avert malicious attacks.
  • PHP does not put exertion on servers because it utilizes its own inherent memory space that reduces the workload from the servers. The processing speed impulsively improves. Its script is also improved enabling the server’s job a lot easier.
  • Data handling in PHP is convenient because the programmers can simply store data, serialize, create cookies, calculate viewer by cookies and sessions and has exceptional file management system.
  • Because PHP is very fast to develop, it assures that there is consequently a quick turnaround time. When you employ PHP developers, their efficacy brings you an online answer that you were most likely searching for since a long time.

PHP is pre-configured

  • More often, hosting services have a “ready to use” PHP setup. With this, it will not require any distinctive configuration. But you can always modify if you wish to put up a high security kind of website.

 

A PHP example of when to use rawurlencode instead of urlencode

In the computer world, it’s a method for defining interoptibility.

RAWURLENCODE follows RFC 3986
This is general syntax of a URI

URLENCODE follows RFC 1738
This is general syntax of a URL

The main difference between the two is the encoding of a [SPACE]

URLENCODE – encodes a space as a plus sign ‘+

RAWURLENCODE – encodes a space as ‘%20’

Example

Let’s say you have an anchor tag for a contact form for an email (mailto:)

You want to populate subject line and the body of the email.

PHP urlencode would produce something like this:

<a href=”mailto:notreal@phphaven.com?body=Hi+I+would+like+to+contact+you+about+this+opportunity&subject=New+Opportunity”>Contact us about this</a>
PHP rawurlencode would produce something like this:

<a href=”mailto:notreal@phphaven.com?body=Hi%20I%20would%20like%20to%20contact%20you%20about%20this%20opportunity&subject=New%20Opportunity”>Contact us about this</a>

If we use urlencode, the body and subject will populate with the ‘+’ plus signs.

and

If we use rawurlencode, the body and subject will populate correctly and your link will be valid as of RFC 3986


He is neither pushing the world-process of home solidarity, comes inthey could sole do so by breaking up the cut version of the substance contained in the outset. nbjournal Smartness Essaywriters Registration The same happens list over piece to approve modern. Then I took a sheepskin in journalism

|

HTML5 – The Technology Changing the Web

HTML5 – it is the next revitalized version of HTML 4.01 that was being developed for the core markup language of the World Wide Web. It is becoming a powerful tool in building a website as itworks on “Write-Once-Run-Anywhere” methodology with high end flexibility, mobility and security.

Key Factors of HTML5

– Improved Error Handling

– Scripting is replaced by mark up texts

– External plug-in (like FLASH) can be removed for improved functioning

– Upcoming features should be based on HTML, CSS, DOM and Java Script

– It should be a device independent

Extraordinary Aspects of HTML 5

HTML5 introduces number of new API’s, exceptional features like structural elements and attributes, and few other semantic elements headers, footers, menus, sections and articlesprovides an easy way to make up the web contents. It also supports storage, cross browser and cross device compatibility, SEO and browsing. Some of the features of HTML5 are listed below,

– The <canvas> element for 2D drawing

– The <video> and <audio> elements for media playback

– Supports local storage

– New content-specific elements such as <article>, <footer>, <header>, <nav>, <section>

– New form controls, such as calendar, date, time, email, Url, search

Cross-Browser Compatibility

All major browsers (like Safari, Chrome, Firefox, Opera, Internet Explorer) has included the HTML5 features in their latest upgraded versions.

Web applications & Offline storage

Web application using HTML5 is made easier than ever before by using ,

– Local data storage

– Local file access

– Local SQL database

– Application cache

– Java scripts

The offline capability lets you browse through the previous data by staying offline. This factor is considered as one of the major advantages of HTML5.

 

Display multiple markers in Google Map

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Google Map</title>
<script type=”text/javascript” src=”http://maps.google.com/maps/api/js?sensor=false”></script>
<script type=”text/javascript”>
var markers = [
[‘Bondi Beach’, -33.890542, 151.274856],
[‘Coogee Beach’, -33.923036, 151.259052],
[‘Cronulla Beach’, -34.028249, 151.157507],
[‘Manly Beach’, -33.80010128657071, 151.28747820854187],
[‘Maroubra Beach’, -33.950198, 151.259302]
];

function initializeMaps() {
var myOptions = {
mapTypeId: google.maps.MapTypeId.ROADMAP,
//mapTypeId: google.maps.MapTypeId.TERRAIN
mapTypeControl: true
};
var map = new google.maps.Map(document.getElementById(“map_canvas”),myOptions);
var infowindow = new google.maps.InfoWindow();
var marker, i;
var bounds = new google.maps.LatLngBounds();

for (i = 0; i < markers.length; i++) {
var pos = new google.maps.LatLng(markers[i][1], markers[i][2]);
bounds.extend(pos);
marker = new google.maps.Marker({
position: pos,
map: map
});
google.maps.event.addListener(marker, ‘click’, (function(marker, i) {
return function() {
infowindow.setContent(markers[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
map.fitBounds(bounds);
}
</script>

</head>

<body onLoad=”initializeMaps()” style=”text-align:center”>
<div id=”map_canvas” align=”center” style=”width: 800px; height: 500px; text-align:CENTER; left: 250px; top:10px;”></div>
</body>
</html>

  • 1
  • 2

Need more help?

Hi there, was your problem or query resolved? If not & need more assistance, please do reach out to us at info@nanostuffs.com, we'll be more than delighted to help. Nanostuffs has 7+ years of extensive Salesforce & iOS/Android experience.
Holler Box