Link to home
Start Free TrialLog in
Avatar of selhamawy
selhamawy

asked on

Notice: Undefined index: chkDel in .....

I get the following error and the record is not deleted

Notice: Undefined index: chkDel in \File_3.php on line 4

I have three files file the description of the purpose of the files as follows:

File_1 -       Search Parameters, Default all records Shown, and
                  Select record(s) to Delete by checkbox
File_2 -       Search Results and
                  Select record(s) to Delete by checkbox
File_3 -      Delete the record(s) in checked and
                  Commit the Transaction

Please can anyone assist here as I cannot seem to locate the problem.

Thank you all in advance

Code:                  
            


<script language="JavaScript"> // included in all three files 
	function onDelete()
	{ 	if(confirm('Do you want to delete these entries?')==true) {
			return true;
		}
		else { 
			return false;
		}
	}
</script>

 

<?PHP	//File_2.php   	

		   	
		ECHO '<form action="File_3.php" method="get">
			<input  style="color: #fff; background-color: #800000; width: 100px; float: left;" 
					type="submit" value="Delete" 
					name="delete" id="frm_delete" />
			</form>	';  // DELETE BUTTON



	
	echo ' <form name="frmMain" action="deleteEntryCommit.php" method="POST" OnSubmit="return onDelete();"> ';

	
	echo "<tbody>";
	echo "<tr>";	
	echo "<table border='0'>";	
	
	//---------------------//
	// Create Table Header //
	//---------------------//
	
	echo "<tr bgcolor='#228B22'>	
			<td class='rowTitleFormat' align='center' > <b> Delete 	</b> </td> 		
			<td class='rowTitleFormat' align='center' > <b> Last Name 	</b> </td> 		
			<td class='rowTitleFormat' align='center' > <b> First Name	</b> </td> 
			<td class='rowTitleFormat' align='center' > <b> Title		</b> </td> 
			<td class='rowTitleFormat' align='center' > <b> E-Mail		</b> </td> 
			<td class='rowTitleFormat' align='center' > <b> Phone		</b> </td>  
			<td class='rowTitleFormat' align='center' > <b> Department	</b> </td> 			
		  </tr> </font>";
		  
		
	echo '<br>';
	
	if($_POST["txtDepartment"] != '.All') {	
			$stid = oci_parse($conn, 	' SELECT  pidm, lname, fname, title, email, phone, ext , dept ' . 
										' FROM view_Name' .
										' WHERE LNAME is not null and' . 
										' LNAME like INITCAP (' . '\'' . $_POST["txtLastName"].'\'' . ')' .
													' and '.			
										' FNAME like INITCAP (' . '\'' . $_POST["txtFirstName"].'\''. ')' .
													' and '.
										' TITLE like ' . '\'' . $_POST["txtTitle"].'\''. 
													' and '.
										' EMAIL like ' . '\'' . $_POST["txtEmail"].'\''. 
													' and '.			
										' PHONE || EXT like ' . '\'' . $_POST["txtPhone"].'\''. ' ' .
													' and '. 
										' DEPT like INITCAP (' . '\'' . $_POST["txtDepartment"].'\''. ')' .
										' order by DEPT, LNAME '	);
	}
	else {	
			$stid = oci_parse($conn, 	' SELECT  pidm, lname, fname, title, email, phone, ext , dept ' . 
										' FROM view_Name' .
										' WHERE LNAME is not null and' . 
										' LNAME like INITCAP (' . '\'' . $_POST["txtLastName"].'\'' . ')' .
													' and '.			
										' FNAME like INITCAP (' . '\'' . $_POST["txtFirstName"].'\''. ')' .
													' and '.
										' TITLE like ' . '\'' . $_POST["txtTitle"].'\''. 
													' and '.
										' EMAIL like ' . '\'' . $_POST["txtEmail"].'\''. 
													' and '.			
										' PHONE || EXT like ' . '\'' . $_POST["txtPhone"].'\''. ' ' .
										' order by DEPT, LNAME '	);
	}
		
	oci_execute($stid); 
	
	echo "<tr bgcolor='#228B22'>";  		

	$counter = 1;
	
	//********************//	
	// Populate Directory //			
	//********************//
	while ($row = oci_fetch_array($stid, OCI_BOTH)) {			
		if(multipleof2($counter)){
			echo "<tr class='rowResultFormat' bgcolor='#FFFACD'> "; } 
		else {
			echo "<tr cclass='rowResultFormat' bgcolor='#c98d11'> "; } 
		echo '<td 	class="rowResultFormat" align="center"> 
					<input type="checkbox" name="chkDel[]" value="' . $row["PIDM"] . '"></td>	';
		echo "<td class='rowResultFormat' >" . ($row['LNAME']) . "</td> " ;			
		echo "<td class='rowResultFormat' >" . ($row['FNAME']) . "</td>";
		echo "<td class='rowResultFormat' >" . ($row['TITLE']) . "</td>" ; 
		echo "<td class='rowResultFormat' >" . ($row['EMAIL']) . "</td>";
		echo "<td class='rowResultFormat' >" . ($row['PHONE']) . ($row['EXT']) . "</td>";
		echo "<td class='rowResultFormat' >" . ($row['DEPT'])  . "</td>";				
		echo "</font> </tr>\n";	
		$counter++;	
	}

echo "</tbody>	";



echo "</table><br>";

?>


<?PHP 	//File_3.php
	for($i=0;$i<count($_POST["chkDel"]);$i++) {
		if($_POST["chkDel"][$i] != "") {
			$objParse = oci_parse($conn, "DELETE FROM table_Name WHERE pidm = '" . $_POST["chkDel"][$i] . "' ");
			$objExecute = oci_execute($objParse, OCI_DEFAULT);
		}
	}
	oci_commit($conn); //*** Commit Transaction ***//
	oci_close($conn);
?>

Open in new window

Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Have you checked if $_POST["chkDel"] is set?

Try this:

if(isset($_POST["chkDel"])) { your code here }
else echo "Not set"

HTH,
Dan
@Dan is on the money.  The reason you have to check if it is set or not is because the chkDel is an array of checkboxes.  When no checkboxes are selected and you click the delete button, nothing to do with the checkbox is sent to the server.  It's not like it sends that the checkbox is off, the browser just doesn't send anything.
Here is a demo http://phpfiddle.org/api/run/g7f-ds0

first check one or many of the checkboxes and see the result. You'll see the "mycheckbox" variable with the indexes of the ones checked

Then go back and uncheck all and submit.  You won't see the "mycheckbox" variable at all.
As an addition to what Dan says, you don't need to do a for() loop and check against an empty value. An empty check box doesn't get submitted, so if it exists in the array, it was checked. Here's the normal way of doing it:

if (isset($_POST['chkDel'])) {
   foreach ($_POST['chkDel'] as $chk) {
      $objParse = oci_parse($conn, "DELETE FROM table_Name WHERE pidm = '$chk'");
      $objExecute = oci_execute($objParse, OCI_DEFAULT);
   }
}

Open in new window

Here is an excerpt from file2.php, which appears to be using the GET method to call the action script file3.php.  I believe that you want to call with the POST method for two reasons.  First, your data will be found where you expect it, in $_POST and second, you must never use a GET method if your script can change the data model.  

The reasons for this protocol restriction are fairly complicated but a simple example will help.  Let's say you have a delete script that uses the GET method.  You can start this script from the browser with a URL like delete.php?id=1 and it will delete record #1.  That also means that a malicious or stupid person could create a web page with link after link saying delete.php?id=2, delete.php?id=3, etc.  What do you suppose would happen if someone fed that page to Google and Google followed all the links?

		ECHO '<form action="File_3.php" method="get">
			<input  style="color: #fff; background-color: #800000; width: 100px; float: left;" 
					type="submit" value="Delete" 
					name="delete" id="frm_delete" />
			</form>	';  // DELETE BUTTON

Open in new window

Avatar of selhamawy
selhamawy

ASKER

Ray Paseur,

Thanks for the heads up on the security breach.
I changed it to post instead of get and still have errors...

Also:
it is still not calling the onDelete() script before going to File_3
this is used to confirm the delete
also it is still not passing chkDel  from File_2 to File_3.
this seems to have been the problem from the beginning....
Right - your file2.php has 2 forms - the first one has a DELETE button that will submit an empty form to your file3.php script, and the second form has all the form fields but no submit, so there's no way of submitted the form to your script page (deleteEntryCommit.php)
Dan ,

Thanks for the advise and as suspected it is not being passed to file_3

Tagit,

my problem is that i am checking the box(es) but they are not being passed to file_3.
File_1 works fine i dont know why File_2 is not working.
File_1 also has the ability to delete as well..
You might want to step back to a simpler example, master that, and build up from there.  Here is how to handle HTML forms in PHP:
http://php.net/manual/en/tutorial.forms.php
Are my posts invisible again??
Chris,

Sorry I did not see your comment.

I think you may have something.
Please elaborate because I don't follow...

Thanks in advance...

"and May the Odds be Ever in Your Favor" - The Hunger Games
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Chris,

WOW that did it.....
I was tearing whats left of my hear out.

Thanks a BILLION...

your reward...

"One Ring to Rule them All..." - The Lord of the Rings,  J.R.R. Tolkien