Form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pritam79
Forum Commoner
Posts: 65
Joined: Wed Mar 26, 2008 9:28 am

Form

Post by pritam79 »

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?

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">&nbsp;&nbsp;<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>
chunnilal
Forum Newbie
Posts: 3
Joined: Tue Apr 05, 2011 3:46 am

Re: Form

Post by chunnilal »

which browser?
pritam79
Forum Commoner
Posts: 65
Joined: Wed Mar 26, 2008 9:28 am

Re: Form

Post by pritam79 »

chunnilal wrote:which browser?
Firefox 3.6 on Win 7.
pritam79
Forum Commoner
Posts: 65
Joined: Wed Mar 26, 2008 9:28 am

Re: Form

Post by pritam79 »

I have been able to modify my script, and now i just have one problem. The insertion of records, display of records and error detection for erroneous input works well. Even when I select the checkboxes and press delete button the records get deleted.
The problem I am having is that in case I do not select any checkboxes and then press the ‘delete’ button, I get the error message which says “Select record(s) to delete” as expected, but I also get this error message at the top of the page which says- “Undefined index: checkbox in C:\wamp\www\MasterPG442\BookMasterform.php on line 135.

This is the code..please help

Code: Select all

<html>
<head>
<title>bookmaster form</title>
<style type="text/css">
body {
	background: #fff;
	font-family: Arial;
	margin: 0 0 0;
	padding: 0;
}
div {
	margin: 0;
	padding: 0;
}
#header {
        	margin-left: 225px; margin-right: 225px; text-align: center;  background-color: black; font-weight: bold; font-family: Arial; color: white; height: 40px;
     	}
#left {
	width: 150px;
	height: 195px;
	position: absolute;
	background-color:silver;
	margin-left: 225px;
	margin-top:-35px;
	
}
#right {
	width: 680px;
	height: 195px;
	position: absolute;
	margin-left: 375px;
	background-color:silver;
	margin-top:-35px;
	
}
#left p {
	text-align: right;
	margin-right: 10px;
	padding-top: 3px;
}
#right p {
	text-align: left;
	margin-left: 20px;
	padding-top: 0px;
}
#list-header {
               width:830px;
               position: relative;
               margin-top: 160px;
               margin-left: 225px;
               height: 40px;
               background-color:black;
               
}
#book-list {
       	    width:830px;
            position: relative;
            margin-top: 0px;
            margin-left: 225px;
            height: 30px;
            background-color: silver;
	
}

.style1 {
	color: #FFFFFF;
	text-align:center;
	width:410px;
	height:40px;
}
.style2 {
	width: 350px;
	text-align:center;
	height: 10px;
}
#delmsg {
	position:absolute;
	margin-left:30px;
	margin-top:150px;
}
#delmsg p {
	color:red;
} 
</style>
</head>
<body>
<div id="header">
     <h2>BOOK MASTER</h2>
</div>
<?php 
require("db-connect.php");

     $result = db_connect();
     $count=mysql_num_rows($result);
     global $insert, $delresult;
      
// Check if delete button active, start this


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]')");
        if($result)
        {
         $insert = TRUE;
        }
      }
    }
if(isset($_POST['delete']))
       {
      $checkboxes=$_REQUEST["checkbox"];
      for($i=0; $i<count($checkboxes); $i++)
        {
          if(!(isset($checkboxes[$i])))
            {
              $checked == FALSE;
            }
          elseif(isset($checkboxes[$i]))
            {
               $del_id = $checkboxes[$i];
               $sql = "DELETE FROM bookmastertable WHERE BookID='$del_id'";
               $delresult = mysql_query($sql);
            }
         }
       
       }
?>
<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"> 
      <p><input type="text" name="BookName" value="<?php if($insert) echo ''; elseif(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($insert) echo ''; elseif(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($insert) echo ''; elseif(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($insert) echo ''; elseif(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">&nbsp;&nbsp;<input name="reset" type="reset" value="reset"></p>
    </div>
  </form>
<div id="delmsg">
<?php if(isset($_POST['delete']) && $delresult==FALSE) echo "<p>Select record(s) to delete</p>"; ?>
</div>  
<form method="post" action="BookMasterform.php">
  <div id="list-header">
    <table width="830px" align="center" height="40px">
    <tr>
     <td style="width: 80px" align="left">
  	 <input name="delete" type="submit" id="delete" value="Delete" style="width: 50px"></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>
	 <td style="width: 80px" align="center">
  	 <input name="edit" type="submit" id="edit" value="Edit" style="width: 50px"></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="book-list">
       <table width="830px" align="center" bgcolor="silver">
       <tr><td width="70px" bgcolor="black" align="center"><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>
	   <td width="70px" bgcolor="black"  align="center"><input name="Radio" type="radio" checked="checked" value="<?php echo $row['BookID']; ?>"></td></tr>
       </table>
    </div>
       
<?php
     }
?>
</form>
</body>
</html>
Post Reply