Saturday, August 4, 2012

Multiple Submits in Single Form php



Multiple Submits in Single Form php 



Name
Designation
Home Town

You often have seen one form with one submit button.But by using PHP scripts we can create multiple submission of single form.This example,I have created to submit one for printing by using print.php and other for entering the form content into database by using enter.php which enters data into a table called employee_details created using following .txt file in my_database.
CREATE TABLE IF NOT EXISTS `employee_details` (
  `id` int(20) NOT NULL AUTO_INCREMENT,
  `First_Name` varchar(40) NOT NULL,
  `Designation` varchar(40) NOT NULL,
  `Home_Town` varchar(40) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

These two submission Print and Enter are deviated by a file called div.php which contains include and if, else if and else conditions,used to deviate print  button submission to print .php and enter button submission to enter.php
<html>
<head>
</head>
<body>
<table bgcolor=#f7d9c7 width=100 height=100 align="center">
<form action="div.php" method="post" target="foo" onSubmit="window.open('', 'foo', 'width=800,height=500,status=yes,resizable=yes,scrollbars=yes')">
<tr><td>Name</td><td><input type="text" name="First_Name"/></td></tr>
<tr><td>Designation</td><td><input type="text" name="Designation"/></td></td></tr>
<tr><td>Home Town</td><td><input type="text" name="Home_Town"/></td></tr>
<tr><td align="right"><input type="submit" value="Print" name="prnt" id="prnBtn"></td><td align="right"><input type="submit" value="Enter" name="save" id="subBtn"></td></tr>
</form>
</table>
</body>
</html>

Web pages and web application development use HTML,PHP,Java Scripts and CSS in combination. This example also,I have used java scripts to open print.php and enter.php in new window without menu bars and use given width and height values to view real software window like appearance.and It also has used  CSS styles to prevent print button printing with the document.
div.php:-

<?php

if ($_POST['prnt']=='Print')
{
include "print.php";
}
else if ($_POST['save']=='Enter')
{
include "enter.php";
}
else
{
 echo "error!";
}
?>

print.php:- 

<head>
<style type='text/css' media='print'>
#prnBtn {display : none}
</style>

</head>
<body>
<?php
$fname=$_POST['fname'];
$desig=$_POST['desig'];
$htown=$_POST['htown'];
?>
<table>
<tr><td><img src="pn.png"></td><td>Employers detail of web design company</td></tr>
<tr><td>Name:</td><td><?php echo $fname;?></td></tr>
<tr><td>Designation:</td><td><?php echo $desig;?></td></tr>
<tr><td>Home Town:</td><td><?php echo $htown;?></td></tr>
<tr><td>&nbsp;</td><td>--------------------</td></tr>
<tr><td>&nbsp;</td><td>Authorized officer's Signature</td></tr>
<tr><td><input type="button" value="PRINT" onClick="window.print()" id="prnBtn"></td></tr>
</table>
</body>

enter.php

<?php
$username="root";
$password="";
$database="my_database";

$First_Name=$_POST['First_Name'];
$Designation=$_POST['Designation'];
$Home_Town=$_POST['Home_Town'];

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO employee_details VALUES ('','$First_Name','$Designation','$Home_Town')";
mysql_query($query);
mysql_close();
echo
"<font color='red'>You have sucessfully entered data </font>"
?>

No comments:

Post a Comment