Form
Posted: Tue Apr 05, 2011 12:45 am
I have this form that submits to itself and the data inserted into the fields get displayed on the same page below the form. but I am having problems-
When the input boxes are filled with correct data and I click the Save button, the data gets inserted in MySQL, but the form input fields do not get emptied automatically. I have to clear each record from the input fields manually and even the Reset button doesnot work. But the reset button works well only in case the page loads in localhost for the very first time, i.e before submitting any data.
I don't want to use JavaScript for validation. But I am not able to do everything in PHP either. Is there no way to do this in PHP?
When the input boxes are filled with correct data and I click the Save button, the data gets inserted in MySQL, but the form input fields do not get emptied automatically. I have to clear each record from the input fields manually and even the Reset button doesnot work. But the reset button works well only in case the page loads in localhost for the very first time, i.e before submitting any data.
I don't want to use JavaScript for validation. But I am not able to do everything in PHP either. Is there no way to do this in PHP?
Code: Select all
<body>
<div id="header">
<h2>BOOK MASTER</h2>
</div>
<?php
$result = mysql_pconnect("localhost","root","");
if(!$result)
{
echo "Could not connect to database server";
exit;
}
mysql_select_db("bookmaster");
$result = mysql_query("SELECT * FROM bookmastertable");
$count=mysql_num_rows($result);
// Check if delete button active, start this
if(isset($_POST['delete']))
{
$checkboxes=$_REQUEST["checkbox"];
for($i=0; $i<count($checkboxes); $i++)
{
if(isset($checkboxes[$i]))
{
$del_id = $checkboxes[$i];
$sql = "DELETE FROM bookmastertable WHERE BookID='$del_id'";
$result = mysql_query($sql);
}
}
}
?>
<?php
$result = mysql_pconnect("localhost","root","");
if(!$result)
{
echo "Could not connect to database server";
exit;
}
mysql_select_db("bookmaster");
$result = mysql_query("SELECT * FROM bookmastertable");
if(isset($_POST['save']))
{
$allFieldRecords=TRUE;
$BookName = $_POST['BookName'];
$Synopsis = $_POST['Synopsis'];
$AuthorName = $_POST['AuthorName'];
$PublisherName = $_POST['PublisherName'];
if(strlen($_POST['BookName'])==0)
{
$allFieldRecords=FALSE;
}
if(strlen($_POST['Synopsis'])==0)
{
$allFieldRecords=FALSE;
}
if(strlen($_POST['AuthorName'])==0)
{
$allFieldRecords=FALSE;
}
if(strlen($_POST['PublisherName'])==0)
{
$allFieldRecords=FALSE;
}
if($allFieldRecords==TRUE)
{
$result = mysql_query("INSERT into bookmastertable(BookID, BookName, Synopsis, AuthorName, PublisherName) values('', '$_POST[BookName]', '$_POST[Synopsis]', '$_POST[AuthorName]', '$_POST[PublisherName]')");
}
}
?>
<form action="BookMasterform.php" method="post">
<div id="left">
<p>Book Name:</p>
<p>Synopsis:</p>
<p>Author name:</p>
<p>Publisher name:</p>
</div>
<div id="right" style="left: 0px; top: 0px">
<p><input type="text" name="BookName" value="<?php if(isset($_POST['BookName'])) echo $_POST['BookName']; ?>"><?php if(isset($_POST['save']) && strlen($_POST['BookName'])==0) echo "<font color=red>Book Name cannot be empty.</font>"; ?></p>
<p><input type="text" name="Synopsis" value="<?php if(isset($_POST['Synopsis'])) echo $_POST['Synopsis']; ?>"><?php if(isset($_POST['save']) && strlen($_POST['Synopsis'])==0) echo "<font color=red>Synopsis cannot be empty.</font>"; ?></p>
<p><input type="text" name="AuthorName" value="<?php if(isset($_POST['AuthorName'])) echo $_POST['AuthorName']; ?>"><?php if(isset($_POST['save']) && strlen($_POST['AuthorName'])==0) echo "<font color=red>Author Name cannot be empty.</font>"; ?></p>
<p><input type="text" name="PublisherName" value="<?php if(isset($_POST['PublisherName'])) echo $_POST['PublisherName']; ?>"><?php if(isset($_POST['save']) && strlen($_POST['PublisherName'])==0) echo "<font color=red>Publisher Name cannot be empty</font>"; ?></p>
<p><input type="submit" name="save" value="Save"> <input name="reset" type="reset" value="reset"></p>
</div>
</form>
<form method="post" action="BookMasterform.php">
<div id="list">
<table>
<tr>
<td style="width: 80px" bgcolor="black">
<input name="delete" type="submit" id="delete" value="Delete" style="width: 54px"></td>
<td class="style1"><b>BOOK NAME</b></td>
<td class="style1"><b>SYNOPSIS</b></td>
<td class="style1"><b>AUTHOR NAME</b></td>
<td class="style1"><b>PUBLISHER NAME</b></td>
</tr>
</table>
</div>
<?php
$result = mysql_query("SELECT * FROM bookmastertable ORDER BY BookID");
while($row = mysql_fetch_array($result))
{
$BookName = $row['BookName'];
$Synopsis = $row['Synopsis'];
$AuthorName = $row['AuthorName'];
$PublisherName = $row['PublisherName'];
?>
<div id="list">
<table>
<tr><td width="70px" bgcolor="black"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['BookID']; ?>"></td>
<td class="style2"><?php echo $BookName; ?></td>
<td class="style2"><?php echo $Synopsis; ?></td>
<td class="style2"><?php echo $AuthorName; ?></td>
<td class="style2"><?php echo $PublisherName; ?></td></tr>
</table>
</div>
<?php
}
?>
</form>
</body>