Tuesday, May 29, 2012

different types of input php



Different types of input php

In HTML Form You may have seen different types of input fields like text fields,text areas,check boxes,options and radios etc.This post is to show you how to create above fields in HTML and how post the details of forms through a PHP file in to a MySql database.
                 By now you may have created the data base called "my_database" which we have used for our previous lessens. create table called "persanal_data" in your database using following fields and data type indicated.

CREATE TABLE IF NOT EXISTS `persanal_data` (
  `id` int(50) NOT NULL AUTO_INCREMENT,
  `Fname` varchar(50) NOT NULL,
  `Lname` varchar(50) NOT NULL,
  `gender` varchar(50) NOT NULL,
  `level` varchar(50) NOT NULL,
  `details` longtext NOT NULL,
  `pet` varchar(50) NOT NULL,
  `drink` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

Then I have created HTML file including different types of filed as follows.

   
First Name:-
Last Name:-
Gender:-Male:-
Female:-
Please Select your Level of Education:-Primery:-
Secondary:-
University:-
:
Select a your favorite pet:-:
Select type of drink:-:

Then following php file should be created to send the collected data from the form to mysql.

<?php
$username="root";
$password="";
$database="my_database";
$Fname = $_POST["Fname"];
$Lname = $_POST["Lname"];
$gender = $_POST["gender"];
$level = $_POST["level"];
$details = $_POST["details"];
$pet = $_POST["pet"];
$drink = $_POST["drink"];
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO persanal_data VALUES ('','$Fname','$Lname','$gender','$level ','$details','$pet','$drink')";
mysql_query($query);
mysql_close();
echo
"You have sucessfully entered data";
?>

No comments:

Post a Comment