Page 1 of 1

deleteing an array

Posted: Mon Sep 15, 2003 10:47 am
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

Posted: Mon Sep 15, 2003 11:11 am
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

Posted: Mon Sep 15, 2003 11:18 am
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

Posted: Mon Sep 15, 2003 11:22 am
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

Posted: Mon Sep 15, 2003 11:29 am
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...

Posted: Mon Sep 15, 2003 11:35 am
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 :(

Posted: Mon Sep 15, 2003 11:52 am
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?

Posted: Mon Sep 15, 2003 11:56 am
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."">";
}

Posted: Mon Sep 15, 2003 12:39 pm
by jamrop
that worked

many thanks for your help