Delete Record Validation Check Problem

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
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Delete Record Validation Check Problem

Post by mmc01ms »

I have button which deletes a record from the database however i want some form of validation which would basically ask if they are sure they want to delete. Don't know how i could do this any ideas or examples?


code for delete_vinyl.php is:

Code: Select all

<?php
	session_start();
	
	require('page.inc');
	require_once('update_delete_fns.php');
	
	checkAdminLogin();

	
	$editform = new Page();
	
	$editform -> Display();
	
	display_main_menu();

	
  if (isset($_POST&#1111;'cat_no'])) 
  &#123;
    $catno = $_POST&#1111;'cat_no'];
    if(delete_vinyl($catno))
      echo '<center>Book '.$catno.' was deleted.</center><br />';
    else
      echo '<center>Book '.$catno.' could not be deleted.</center><br />';
  &#125; 
  

do_html_footer();

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

something similar to:

Code: Select all

if(isset($_POST&#1111;'cat_no']))
&#123;
  if(isset($_POST&#1111;'do']) && $_POST&#1111;'do'] == 'confirm')
    delete_vinyl();
  else
    // print the confirmation request, that has hidden form fields to set the do value to 'confirm', as well as pass the cat_no value.
&#125;
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

or you could use some JS and do it on the form page itself:

Code: Select all

<script>
function checkIt()&#123;
   if(confirm("are you sure you want to delete"))&#123;
    document.MyForm.submit();
   &#125;
&#125;
</script>

<form name="MyForm" action="delpage.php">
<input type="button" value="Delete This" onClick="checkIt();">
</form>
Burr
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Post by mmc01ms »

Thanks for the replies guys i like the idea of the JS Pop-up Box however i tried implementing it. Sorry i may sound dumb but i couldn't get the code you said to work in my script. My attempt is below.

Code: Select all

<?php 
	
	//display_vinyl_form.php
	
	require_once('shopping_cart_fns.php');
	
	
	echo '<script>'; 
		function checkIt()&#123; 
			if(confirm("are you sure you want to delete"))&#123; 
				document.MyForm.submit(); 
			&#125; 
		&#125; 
	echo '</script>'; 
		
		
		
		
		
		
		
		function display_vinyl_form($vinyl = '')
	&#123;
		
		$edit = is_array($vinyl);
?>
<center><form align="center" name="newrecord" method="post" action="<?php echo $edit?'edit_vinyl.php' : 'insert_vinyl.php';?>">
		<table bordercolor="#FFFFFF" border="1" bgcolor="#7b9815">
			<font color="#000000" size="2" face="Courier New, Courier, mono">
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Cat_No: </font></td>
					<td><input name="cat_no" type="text" maxlength="6" value="<?php echo $vinyl&#1111;'cat_no']; ?>"></td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Title: </font></td>
					<td><input name="title" type="text" maxlength="40" value="<?php echo $edit?$vinyl&#1111;'title']:' '; ?>"></td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Artist: </font></td>
					<td><select name="artist_id">
							<?php
		$artist_array=get_artists();
		foreach ($artist_array as $thisartist)
		&#123;
			echo '<option value="';
			echo $thisartist&#1111;'artist_id'];
			echo '"';
			if ($edit && $thisartist&#1111;'artist_id'] == $vinyl&#1111;'artist_id'])
				echo 'selected';
			echo '>';
			echo $thisartist&#1111;'artist_id'];
			echo'</option>';
			echo"\n";
		&#125;
							?>
						</select>
					</td>
				</tr>
				
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Genre: </font></td>
					<td><select name="genre_ref">
							<?php
		$genre_array=get_genres();
		foreach ($genre_array as $thisgenre)
		&#123;
			echo '<option value="';
			echo $thisgenre&#1111;'genre_ref'];
			echo '"';
			if ($edit && $thisgenre&#1111;'genre_ref'] == $vinyl&#1111;'genre_ref'])
				echo 'selected';
			echo '>';
			echo $thisgenre&#1111;'genre_ref'];
			echo'</option>';
			echo"\n";
		&#125;
							?>
						</select>
					</td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Vinyl Status: </font></td>
					<td><select name="status_code">
							<?php
		$status_array=get_status();
		foreach ($status_array as $thisstatus)
		&#123;
			echo '<option value="';
			echo $thisstatus&#1111;'status_code'];
			echo '"';
			echo '>';
			echo $thisstatus&#1111;'status_description'];
			echo'</option>';
			echo"\n";
		&#125;
							?>
						</select>
					</td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Price: </font></td>
					<td><input name="price" type="text" maxlength="5" id="price" value="<?php echo $edit?$vinyl&#1111;'price']: ' '; ?>"></td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Qty: </font></td>
					<td><input name="qty" type="text" maxlength="3" id="qty" value="<?php echo $edit?$vinyl&#1111;'stock_level']: ' '; ?>"></td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Vinyl Information</font></td><td><textarea name="comments" maxlength="225" cols="60" rows="10"><?php echo $vinyl&#1111;'vinyl_information']?></textarea></td>
				</tr>
				<tr>
					<td <?php if(!$edit) echo 'colspan="2"'; ?> align="center">
						<?php
		if ($edit)
			echo '<input type="hidden" name="oldcatno" value="'.$vinyl&#1111;'cat_no'].'">';
		
						?>
						<td><input type="submit" value="<?php echo $edit?'Update' : 'Add';?>Record"></form><?php if($edit)
		&#123;
			echo '';
			echo '<form name="MyForm" action="delete_vinyl.php"> ';
			echo '<input type="button" value="Delete Vinyl" onClick="checkIt();"> ';
			echo '<input type="hidden" name="cat_no" value="'.$vinyl&#1111;'cat_no'].'">';
			echo '</form></td>';
		&#125; ?>
					</tr>
					<table></form>	</center>
				
				<div align="left"></div></td>
		</tr>
	</table></td>

			</tr>
			</table>
			<?
	&#125;
		
		do_html_footer();
					?>
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

w/o reading the entire set of code you have there, I see an error right off the bat at the top. Because this is js, it needs to be sent to the user, the function is a js function, not a php function so you need to include that in your echo like this:

Code: Select all

echo "<script> 
      function checkIt()&#123; 
         if(confirm("are you sure you want to delete"))&#123; 
            document.MyForm.submit(); 
         &#125; 
      &#125; 
   </script>";
as I said, I didn't read over all of your code, but make sure that you include everything that needs to call that js function in the stuff that gets sent to the user.

plug away at that and lemme know if you need anythign else.

Burr

edit: not sure if that semi-colon is gonna throw it off in the js itself, best bet would be to just end your php block and include that js function on the page then restart your php block...
Post Reply