Sei sulla pagina 1di 5

Write about Form Handling in PHP?

When you login into a website or into your mail box, you are interacting with a form.

Forms are used to get input from the user and submit it to the web server for processing.

The diagram below illustrates the form handling process.

The code below creates a simple registration form

<html>
<head>
<title>Registration Form</title>

</head>
<body>

<h2>Registration Form</h2>

<form action="post.php" method="POST">


First name:

<input type="text" name="firstname"> <br>


Last name:

<input type="text" name="lastname">

<input type="hidden" name="form_submitted" value="1" />

Submt: <input type="submit" value="Submit">

</form>
</body>
</html>
Out put :-

PHP POST method

 This is the built in PHP super global array variable that is used to get values submitted
via HTTP POST method.
 The array variable can be accessed from any script in the program; it has a global scope.
 This method is ideal when you do not want to display the form post values in the URL.
 A good example of using post method is when submitting login details to the server.

It has the following syntax.

<?php
$_POST['variable_name'];
?>

PHP GET method

 This is the built in PHP super global array variable that is used to get values submitted
via HTTP GET method.
 The array variable can be accessed from any script in the program; it has a global scope.
 This method displays the form values in the URL.
 It’s ideal for search engine forms as it allows the users to book mark the results.

It has the following syntax.

<?php
$_GET['variable_name'];
?>

.
POST.PHP
html>
<body>

Welcome <?php echo $_POST["firstname"]; ?><br>


Your email address is: <?php echo $_POST["lastname"]; ?>

</body>
</html>

The code below creates a simple registration form

<html>
<head>
<title>Registration Form</title>

</head>
<body>

<h2>Registration Form</h2>

<form action="get.php" method="GET">


First name:

<input type="text" name="firstname"> <br>


Last name:

<input type="text" name="lastname">

<input type="hidden" name="form_submitted" value="1" />

Submt: <input type="submit" value="Submit">

</form>
</body>
</html>

Out put :-
GET.PHP

<html>
<body>

Welcome <?php echo $_GET["firstname"]; ?><br>


Your lastname is: <?php echo $_GET["lastname"]; ?>

</body>
</html>

Write a PHP program for Connecting database, inserting and accessing data
from a database.?
<?php

// Create connection
$conn = mysql_connect(“localhost:3306”,”root”,” “);
// Check connection
if (!$conn) {
die("Connection failed: " . mysql_error());
}
// sql to create database
if(mysql_query(“create database:mydb”,$Conn)
{
Echo “Database is Created”;
}
else
{
Echo “Database not created”.mysql_error());
}
// sql to create table
$sql = "CREATE TABLE Persons (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
email VARCHAR(50),
)";

if (mysql_query($conn, $sql)) {
echo "Table Persons created successfully";
} else {
echo "Error creating table: " . mysql_error($conn);
}
// sql to inserting data
$sql = "INSERT INTO Persons (name, email)
VALUES ('503', 'Ranjith', 'rk@gmail.com')";
if (mysql_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysql_error($conn);
}
// sql to accessing data

$sql = "SELECT id, name,email FROM Persons";


$result = mysql_query($conn, $sql);

if (mysql_num_rows($result) > 0) {
// output data of each row
while($row = mysql_fetch_assoc($result)) {
echo "Id: " . $row["id"]. " - Name: " . $row["name"]. " " . $row["email"]. "<br>";
}
} else {
echo "0 results";
}
mysql_close($conn);
?>

Potrebbero piacerti anche