Jump to content

Recommended Posts

Hi all, I have a PHP page where I have included two submit buttons. Before processing with the code associated with any of the two submit buttons I have to check as to which one gets clicked. How do I determine as to which one of the two is clicked?Hi all, I have a PHP page where I have included two submit buttons. One is for inserting data into MySQL and one is for deleting data displayed one that same page using checkboxes. With the code that I have the insertion and display of all the data along with checkboxes is working well. But the thing that I am not able to do is the deletion part. I have included everything in a single php page that posts to itself.

<form action="BookMasterform.php" method="post"><table style="width: 100%; height: 151px" class="style1">	<tr>		<td style="width: 210px; height: 37px" class="style2">Book Name:</td>		<td style="height: 37px">		<input type="text" name="BookName" style="width: 210px"></td>	</tr>	<tr>		<td style="width: 210px; height: 38px" class="style2">Synopsis:</td>		<td style="height: 38px">		<input type="text" name="Synopsis" style="width: 300px"></td>	</tr>	<tr>		<td style="width: 210px; height: 38px" class="style2">Author Name:</td>		<td style="height: 38px">		<input type="text" name="AuthorName" style="width: 297px"></td>	</tr>	<tr>		<td style="width: 210px; height: 38px" class="style2">Publisher Name:</td>		<td style="height: 38px"><input type="text" name="PublisherName"></td>	</tr>	<tr>		<td style="width: 210px; height: 22px" class="style2"></td>		<td style="height: 22px" align="right"><input type="submit" name="save" value="Save">  <input type="reset" value="Clear"></td>	</tr>	</table></form></div><form method="post" action="BookMasterform.php"><div id="list"><table><tr>	<td class="style3" style="width: 147px">	  <input name="delete" type="submit" id="delete" value="Delete" style="width: 54px"></td>	<td class="style3" style="width: 353px">Book Name</td>	<td class="style3" style="width: 403px">Synopsis</td>	<td class="style3" style="width: 467px">Author Name</td>	<td class="style3" style="width: 416px">Publisher Name</td></tr></table></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");// if ok put in db   if(isset($_POST['save']))   {  	 $result = mysql_query("INSERT into bookmastertable(BookID, BookName, Synopsis, AuthorName, PublisherName) values('', '$_POST[BookName]', '$_POST[Synopsis]', '$_POST[AuthorName]', '$_POST[PublisherName]')");   }  	 $result = mysql_query("SELECT * FROM bookmastertable");	 while($row = mysql_fetch_array($result))	  {	   $BookName = $row['BookName'];	   $Synopsis = $row['Synopsis'];	   $AuthorName = $row['AuthorName'];	   $PublisherName = $row['PublisherName'];	  ?>	   <div id="list">	   <table>	   <tr><td class="style4" style="width: 100px"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['BookID']; ?>"></td>	   <td class="style4" style="width: 353px"><?php echo $BookName; ?></td>	   <td class="style4" style="width: 403px"><?php echo $Synopsis; ?></td>	   <td class="style4" style="width: 467px"><?php echo $AuthorName; ?></td>	   <td class="style4" style="width: 416px"><?php echo $PublisherName; ?></td></tr>	   </table>	   </div>	   <?php	   } 	   ?><?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 thisif(isset($_POST['delete'])){  for($i=0;$i<$count;$i++)   {	 $del_id = $checkbox[$i];	 $sql = "DELETE FROM bookmaster WHERE BookID='$del_id'";	 $result = mysql_query($sql);   }}?>	 </form></body></html>

Edited by pritam79
Link to post
Share on other sites

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 for the very first time.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, and use JavaScript?

<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 thisif(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>

Link to post
Share on other sites

you had told php to do so. its by default reset the fields.

value="<?php if(isset($_POST['BookName'])) echo $_POST['BookName']; ?>"

this line is telling that if $_POST['BookName'] is set then echo it up. when you post the data its set the value='somevalue'reset will set the field to its initiate value

Link to post
Share on other sites

you can set a button to act as reset button using js. create a button in a onclick event bound a function which will reset the value of the field to a empty string.

Link to post
Share on other sites

Instead of printing the values in $_POST directly into the form fields, print a local variable. Initialize your variables to be empty, set them to the values from $_POST if they are given, and after you process them and insert into the database set them all back to empty. Once you get down to the fields where you print them, they will be empty. If there was an error during submission, don't reset the variables so that when you print them to the fields they still have the values from $_POST.

Link to post
Share on other sites
Instead......... $_POST.
I am done with my insertion error handling. Now I am trying to include error handling for the delete button. When I select the checkboxes corresponding to the records that I need to delete, they get deleted properly from the database as well as from the page where the records are displayed. But in case I donot select any of the checkboxes and click on the delete button, I get an error message which says- Notice: Undefined index: checkbox in C:\wamp\www\BookMasterform.php on line 140.I want an error message which says- "No records selected"..The following is the code for the delete button.<?php	 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);			}		}	  }	 $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	  }?>

Link to post
Share on other sites
You can use isset to check if $_POST['checkbox'] or $_REQUEST['checkbox'] is set, and if it's not set then they didn't check anything.
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.
<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 thisif(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">  <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>

Link to post
Share on other sites
if(isset($_POST['delete']))
here the if block will be executed when $_POST['delete'] is set. it wont care about checkbox isset or now. so
 $checkboxes=$_REQUEST["checkbox"];

here php will throw a noticeyou may do something like this. so the if block will be executed only when both delete and checkbox had been set.

if(isset($_POST['delete'],$_POST['checkbox']))

Edited by birbal
Link to post
Share on other sites

Hi again I have now included an ‘UPDATE’ link at the end of each displayed record on the ‘BookMasterform.php’ page like <a href="edit.php?id=<?php echo $row['BookID'];?>">EDIT</a>, and when I click any record I am also directed to a different page ‘edit.php’, which provides all the corresponding fields in individual text boxes, so that I can ‘modify’ the records and update them. But how do I proceed? I am ‘passing variables through a URL’ but I am unable to further use this option in the page where I need to modify the records. Which method would be the best, pass the variables in a URL, through a session, via a cookie, or with an HTML form?This is edit.php

<body><div id="header">	 <h2>BOOK MASTER</h2></div><?php require("db-connect.php");$result = db_connect();if(isset($_POST['Update']))	  {   echo $_GET['id'];   // do updation here  }elseif(!isset($_POST['Update']))  {   $sql = mysql_query("SELECT * FROM bookmastertable WHERE BookID = '$_GET[id]'");   while($row = mysql_fetch_array($sql))	 {	  $BookID = $row['BookID'];	  $BookName = $row['BookName'];	  $Synopsis = $row['Synopsis'];	  $AuthorName = $row['AuthorName'];	  $PublisherName = $row['PublisherName'];	 }?> <form action="edit.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 echo $BookName; ?>"></p>	  <p><input type="text" name="Synopsis" value="<?php echo $Synopsis; ?>"></p>	  <p><input type="text" name="AuthorName" value="<?php echo $AuthorName; ?>"></p>	  <p><input type="text" name="PublisherName" value="<?php echo $PublisherName; ?>"></p>	  <p><input type="submit" name="Update" value="Update">  <input name="reset" type="reset" value="reset"></p>	</div>  </form><?php}?>  </body>

Link to post
Share on other sites

you need to process the form if the form has been submited. You can submit the form to itslef (edit.php) then validate your inputs if all are ok update in database.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...