Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Sunday, 30 November 2014

Calculating age in years in php

Age calculation in php



Declare a function =>

  1. function getAge( $dob, $date )
  2. {
  3.     $dob=strtotime($dob);
  4.     $date=strtotime($date);
  5.         $age = 0;
  6.         while( $date > $dob = strtotime('+1 year', $dob))
  7.         {
  8.                 ++$age;
  9.         }
  10.         return $age." years";
  11. }

Sunday, 18 May 2014

Processing Multiple Forms on One Page with PHP

I know that the way most people treat multiple forms on one page is to have each form post to another PHP file where the form is validated, its information is entered into a database or an email is sent off. So you usually have something like this:

<form name="contact-form" method="post" action="contact.php"> blah blah blah </form> <form name="maillist" method="post" action="mailer.php"> blah blah blah </form>
That work great, but why would you create all those extra files when you can just have the form post to the same file and create multiple functions to process your multiple forms.

The solution is very simple and super efficient.
First, lets create some forms.

<form name="mailinglist" method="post"> <input type="text" name="email" /> <input type="submit" name="mailing-submit" value="Join Our Mailing List" /> </form> <form name="contactus" method="post"> <input type="text" name="email" /> <input type="text" name="subjet" /> <textarea name="message"></textarea> <input type="submit" name="contact-submit" value="Send Email" /> </form>

Now lets put some PHP code before the <head> tag to have different processes for each form.

<?php if (!empty($_POST['mailing-submit'])) { //do something here; } if (!empty($_POST['contact-submit'])) { //do something here; } ?>
Now all you need to do is create your processes within those two “if” statements and each form will be dealt with accordingly when it it filled and submitted.

PHP mail Function


PHP provides a convenient way to send email with the mail() function.

Syntax of mail function:

mail(to,subject,message,headers,parameters)

  • to --  Required. Specifies the recipient's email address or addresses.
  • subject -- Required. Specifies the email's subject line. 
  • message -- Required. Specifies the actual email body . Each line should be separated with  \n.
  • headers -- Optional. Specifies additional headers such as "From", "Cc", "Bcc", etc. The additional headers should be separated with  (\r\n).
  • parameters -- Specifies any additional parameters.It is optional.

How to use PHP Sessions


Session variable is used to store information.Session variables hold information about one single user, and are available to all pages in one application.



Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID.


Starting a PHP Session

Before using PHP session variable, you must first start the session.
Note: The session_start() function must appear BEFORE the <html> tag:

<?php session_start(); ?>