Welcome to PHP Lessons

PHP is an extremely popular, Open Source scripting language, most commonly used on webservers to produce dynamic pages. The name "PHP" is a recursive acronym for "PHP: Hypertext Preprocessor" and was initially created by Rasmus Lerdorf in 1994.

This blog has moved here: lab.yogeshsaroya.com | FOLLOW ME ON TWITTER @yogeshsaroya

Thursday, September 1, 2011

How to send PHP Simple E-Mail


The simplest way to send an email with PHP is to send a text email.
In the example below we first declare the variables ($to, $subject, $message, $from, $headers), then we use the variables in the mail() function to send an e-mail:
<?php
$to = "someone@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>


PHP Mail Form

With PHP, you can create a feedback-form on your website. The example below sends a text message to a specified e-mail address:
<html>
<body>

<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
  {
  //send email
  $email = $_REQUEST['email'] ;
  $subject = $_REQUEST['subject'] ;
  $message = $_REQUEST['message'] ;
  mail("someone@example.com", "$subject",
  $message, "From:" . $email);
  echo "Thank you for using our mail form";
  }
else
//if "email" is not filled out, display the form
  {
  echo "<form method='post' action='mailform.php'>
  Email: <input name='email' type='text' /><br />
  Subject: <input name='subject' type='text' /><br />
  Message:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  <input type='submit' />
  </form>";
  }
?>

</body>
</html>


Saturday, August 20, 2011

How to Open popup window with open.window method


To open a new window by clicking on the link, you will need to use window.open method of javascript.




Sample of popup window

<a href="javascript: void(0)" 
   onclick="window.open('popup.html', 
  'windowname1', 
  'width=200, height=77'); 
   return false;">Click here for simple popup window</a>


Now you can open a simple window, also this function can have different features of that window to appear.

Syntax

window.open([URL], [Window Name], [Feature List], [Replace]);
Feature List:
PropertyDefault valueDescription
widthautospecifies width of the new window in pixels
heightautoheight of the window in pixels
topautospecifies window position
leftautospecifies window position
directoriesnoshould the directories bar be shown? (Links bar)
locationnospecifies the presence of the location bar
resizablenospecifies whether the window can be resized.
menubarnospecifies the presence of the menu bar
toolbarnospecifies the presence of the toolbar
scrollbarsnospecifies the presence of the scrollbars
statusnospecifies the presence of the statusbar


<a href="javascript: void(0)" 
   onclick="window.open('popup.html', 
  'windowname2', 
  'width=200, \
   height=77, \
   directories=no, \
   location=no, \
   menubar=no, \
   resizable=no, \
   scrollbars=1, \
   status=no, \
   toolbar=no'); 
  return false;">Click here for specific popup window</a>

Fullscreen popup window

The fullscreen parameter is supported only by Internet Exploer browser.
If you need to open fullscreen popup window in all browsers you need to use this modified function:
<script type="text/javascript">
<!--
function popup(url) 
{
 params  = 'width='+screen.width;
 params += ', height='+screen.height;
 params += ', top=0, left=0'
 params += ', fullscreen=yes';

 newwin=window.open(url,'windowname4', params);
 if (window.focus) {newwin.focus()}
 return false;
}
// -->
</script>

<a href="javascript: void(0)" 
   onclick="popup('popup.html')">Fullscreen popup window</a>

Centered popup window

To open new popup window in the middle of the screen we should know the size of a window and resolution of the screen. On the basis of these data, we will calculate values for the top-left corner.
<script type="text/javascript">
<!--
function popup(url) 
{
 var width  = 300;
 var height = 200;
 var left   = (screen.width  - width)/2;
 var top    = (screen.height - height)/2;
 var params = 'width='+width+', height='+height;
 params += ', top='+top+', left='+left;
 params += ', directories=no';
 params += ', location=no';
 params += ', menubar=no';
 params += ', resizable=no';
 params += ', scrollbars=no';
 params += ', status=no';
 params += ', toolbar=no';
 newwin=window.open(url,'windowname5', params);
 if (window.focus) {newwin.focus()}
 return false;
}
// -->
</script>

<a href="javascript: void(0)" 
   onclick="popup('popup.html')">Centered popup window</a>

How to Select All Text by Clicking on Text Field or Textarea Box




This script highlight all text in textarea or text field (input tag) by clicking on it. It allows users to reduce their manual work. For instance when users need to copy to the clipboard all text quickly or quickly delete the text from the field. For example: to copy a piece of code or to delete a username and password on login web page.


Sample

Textarea:

Input TextBox:

Code
The script is really very simple. Text field must have unique identifier, this indentifier will be passed to the SelectAll() function. The function call only two methods: focus() and select().

<script type="text/javascript">
function SelectAll(id)
{
    document.getElementById(id).focus();
    document.getElementById(id).select();
}
</script>

Textarea:<br>
<textarea rows="3" id="txtarea" onClick="SelectAll('txtarea');" style="width:200px" >This text you can select all by clicking here </textarea>

Input TextBox:<br>
<input type="text" id="txtfld" onClick="SelectAll('txtfld');" style="width:200px" value = "This text you can select all" />

Thursday, August 18, 2011

Introduction to MySQL with PHP

A database is your best choice for storing data in your web application, and the MySQL database server has always been the most popular choice among PHP developers. It's supported by almost any hosting company offering PHP, which makes it easy to get started with, and you can even download and install it on your own computer, for testing purposes.

MySQL uses the SQL (Structured Query Language) programming language to work with the data, and PHP interacts with MySQL by simply passing SQL code through a set of MySQL functions to the MySQL server, which then returns a result that PHP can interpret. It can seem a bit scary to have to learn a second language to interact with databases, but fortunately SQL is a fairly simple language, which looks a lot like the English language and we will provide you with some good SQL examples, allowing you to do the most common tasks.

In the following chapters we will work with the MySQL database and make it do various things for us. To do it properly, we need some common test data, which you will need to add to a database for which you have access to. The easiest way to do this is to use one of the many MySQL tools, with the most popular one being phpMyAdmin, which is installed on most servers offering PHP and MySQL. If you don't have access to phpMyAdmin, you can install it, use one of the many downloadable applications or use the MySQL prompt. Whatever you choose, you should execute the following SQL code against your database. In phpMyAdmin, this is done by clicking the button labelled "SQL":


CREATE TABLE `test_users` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(100) NOT NULL default '',
  `country` varchar(100) NOT NULL default '',
  PRIMARY KEY  (`id`)
); 

INSERT INTO `test_users` VALUES (1,'David','USA');
INSERT INTO `test_users` VALUES (2,'Sammy','Canada');
INSERT INTO `test_users` VALUES (3,'Heidi','Germany');
INSERT INTO `test_users` VALUES (4,'Pierre','France');
INSERT INTO `test_users` VALUES (5,'Carlos','Spain');
Source: php5-tutorial.com

Variables in PHP


Variables are one of the most important aspects of every programming language. They allow you to store a value within a named container, for later use or manipulation. Storing and retrieving something from memory is a complicated task, but fortunately, PHP hides all this low level stuff, and makes it super easy to declare and use variables.

In PHP, variables can be recognized from the $ in front of them. The dollar sign is used to inform PHP that the following piece of text refers to a variable. If you have used other programming languages, you may be used to telling the compiler/interpreter which type of variable you wish to declare. However, PHP is a so-called loosely typed language. It means that you don't have to declare a variable as a specific type - it's type will be determined by what you put into it. We will talk more about this in the next chapter. For now, let's work with some variables.


In many other languages, you have to declare a variable before using it, usually with a specific keyword like "var", "dim" or at least the type you wish to use for the variable. But not so with PHP. Just enter the name of it and assign a value, and you're good to go. Here is an example:

<?php
$myVar = "Hello world!";
?>
Now that's pretty simple! You simply write the name of the variable with a dollar sign in front of it, an equal sign, and then you tell PHP what to put inside of the variable. Now, the variable called myVar holds the value of a text string - "Hello world!". Using the variable is even simpler. In this example, we use it with the echo function (it's actually just a language construct and not a function, but that doesn't matter now), which simply outputs it.
<?php
$myVar = "Hello world!";
echo $myVar;
?>
In this example, we used a text string, but we may as well use e.g. a number, like this:
<?php
$myVar = 42;
echo $myVar;
?>
On the other hand, why not just use a text string with the number 42 in it? That's no problem. Try putting quotes around the number in the above example, and you will see that you get the exact same output. But is it in fact the same? No, of course not. While PHP may not seem to care, internally it does, keeping track of the data types you put into your variables. You can read more about that in the chapters about data types. 

What is PHP?

PHP is an extremely popular, Open Source scripting language, most commonly used on webservers to produce dynamic pages. The name "PHP" is a recursive acronym for "PHP: Hypertext Preprocessor" and was initially created by Rasmus Lerdorf in 1994. As of today, the current version of PHP is version 5, with version 6 in the making.



What is PHP?

  • PHP stands for PHP: Hypertext Preprocessor
  • PHP is a server-side scripting language, like ASP
  • PHP scripts are executed on the server
  • PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
  • PHP is an open source software
  • PHP is free to download and use



What is a PHP File?

  • PHP files can contain text, HTML tags and scripts
  • PHP files are returned to the browser as plain HTML 
  • PHP files have a file extension of ".php", ".php3", or ".phtml"

What is MySQL?

  • MySQL is a database server
  • MySQL is ideal for both small and large applications
  • MySQL supports standard SQL
  • MySQL compiles on a number of platforms
  • MySQL is free to download and use

PHP + MySQL

  • PHP combined with MySQL are cross-platform (you can develop in Windows and serve on a Unix platform)

Why PHP?

  • PHP runs on different platforms (Windows, Linux, Unix, etc.)
  • PHP is compatible with almost all servers used today (Apache, IIS, etc.)
  • PHP is FREE to download from the official PHP resource: www.php.net
  • PHP is easy to learn and runs efficiently on the server side
Source : w3schools.com

Related Posts Plugin for WordPress, Blogger...