deleteing an array

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
jamrop
Forum Commoner
Posts: 80
Joined: Fri May 16, 2003 5:38 pm

deleteing an array

Post by jamrop »

Hey

Trying to delete multiple records from my database. I use checkboxs and tick which ones i want to delete (approve.php). I press the delete key, and a confirm page shows (confirm.php). It does not delete the records or record.

approve.php

Code: Select all

<?php
<form name="delete" method="post" action="delad.php">
                      <input type="Submit" name="submitdel" value="Delete">
<input type="Checkbox" name="advert_id[]" value="<?php echo $advert_id ?>">

?>

I tested it so when it passes throught to confirm.php it echos the advert_id which it does work.

But if i click on yes to confirm it does not delete

confirm.php

Code: Select all

<?php

ob_start(); 


$advert = $_POST['advert_id'];



echo $advert[$x];
if (isset($_POST['no'])) {
header( "Location: approve.php" );}

if (isset($_POST['yes'])) {
for ($x=0; $x<sizeof($advert); $x++)

{
	

$query = "delete from advert where advert_id = '$advert[$x]'";
mysql_query($query,$db);
}


mysql_close($db);}
header("Location: approve.php");
?>

<form name="delete" action="<?php $PHP_SELF ?>" method="post">
			
			<input type="submit" name="yes" value="Yes">
			<input type="submit" name="no" value="No">
			</form>

<?php  

ob_end_flush(); 



?>

When i click on yes, it does not seem to pass the advert_id through.

Can any one help?

Many thanks
User avatar
igoy
Forum Contributor
Posts: 203
Joined: Fri May 02, 2003 11:57 pm
Location: India
Contact:

Post by igoy »

Okay.. I've been thru this before... it was PITA... so this is what I can do for you ................

this was my checkbox..

Code: Select all

&lt;input name="delbox&#1111;]" type="checkbox" id="delbox&#1111;]" value="&lt;?php echo $row_rsApp&#1111;'rid']; ?&gt;"&gt;
when I hit delete....
this is the part where my PHP does the magik....

Code: Select all

<?

if ((isset($HTTP_POST_VARS['delbox'])) && (count($HTTP_POST_VARS['delbox']) > 0)) {
	$a = $HTTP_POST_VARS['delbox']; 
	$i = 0;
	foreach ($a as $v) { 
  		$deleteSQL = "DELETE FROM $table WHERE rid='$delbox[$i]'";
		$Result = mysql_query($deleteSQL, $conn) or die(mysql_error());
		$i++; 	
	}
	header("location:$goto");
}

?>
this works smoothhhh.... try it.. off course make changes accordingly your needs... and do let me know if it works and even if it doesn't
jamrop
Forum Commoner
Posts: 80
Joined: Fri May 16, 2003 5:38 pm

Post by jamrop »

the thing is, it is passing the info to confirm.php, but it is when i press the yes button that it loses the values. I tried echo advert_id when the yes has been pressed but nothing comes up

Before i press yes or no, i tested and used echo advert_id and that should the array

many thanks
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

on your confirm page, when you click yes or no, you are submmiting a new form which doesn't have the array or value in it. The only value you are submitting is the submit value.

You need to use some hidden form fields to store the advert_id values.

Mark
User avatar
igoy
Forum Contributor
Posts: 203
Joined: Fri May 02, 2003 11:57 pm
Location: India
Contact:

Post by igoy »

okay okay... sorry for my mistake.... hmm... so instead of mid-page to confirm action...
why don't you use javascript confirm. that way you save extra page routine ... you can either make a javascript prompt onClick() to submit button or at start of approve.php...
jamrop
Forum Commoner
Posts: 80
Joined: Fri May 16, 2003 5:38 pm

Post by jamrop »

hey thanks

i have tried

Code: Select all

<?php
form name="delete" action="<?php $PHP_SELF ?>" method="post">
			<input type="hidden" name="<?php echo $advert_id[x] ?>" value="<?php echo $advert_id[x] ; ?>">
			<input type="submit" name="yes" value="Yes">
			<input type="submit" name="no" value="No">
			</form>

?>
but that is not passing the values as i tried not hidden, but text as the input type and nothing showed.

So confused

Have not got a clue how to use javascript :(
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

Post by Drachlen »

Code: Select all

<script language="JavaScript">
	function deleten(){

	var decision = confirm("Are you sure you want to delete?");
		
		if (decision == true){
			document.yourform.action = "yourfile.php";
		} else {
			document.yourform.action = "";
		}
	}

</script>

<form name="yourform">
<input type="submit" onclick="deleten();" />
</form>
Maybe this will work?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Code: Select all

<?php echo $advert_id[x] ?>
That is wrong? why did you put x in there?

Also, you had PHP tags inside PHP tags...all not good!

you need to generate a hidden form field for each array value

like this

Code: Select all

foreach ($advert as $temp_advert) {
    echo "<input type="hidden" name="advert_id[]" value=".$temp_advert."">";
}
jamrop
Forum Commoner
Posts: 80
Joined: Fri May 16, 2003 5:38 pm

Post by jamrop »

that worked

many thanks for your help
Post Reply