Dynamic delete in a form

Accessing form elements dynamically on submit

Page 1 of 1

12 Replies - 2310 Views - Last Post: 02 October 2010 - 08:37 PM Rate Topic: -----

#1 SueLin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-October 09

Dynamic delete in a form

Posted 02 October 2010 - 05:09 PM

Thank you in advance for any help.

I have dynamically inserted a 'delete' button into an image gallery next to each image.
The code below is in a loop that populates the gallery.


 <form method="post" action="index.php">
		<input name=<?php echo $imageName[$x]; ?> type="submit"  size="3" value='Delete' />
          </form>



Now I would like to
dynamically delete the corresponding photo with the Delete button.

I can't seem to access the name correctly:


 if (isset($_POST['$imageName[$x]'])){


if (file_exists('photos/' . $imageName[$x])){
		(unlink('photos/' . $imageName[$x]));
		

			echo 'file deleted';
			
		}
		else{
			
	
	echo 'File deletion failed.';

			}

			}



Thank you,
Lindie

Is This A Good Question/Topic? 0
  • +

Replies To: Dynamic delete in a form

#2 creativecoding   User is offline

  • Hash != Encryption
  • member icon


Reputation: 931
  • View blog
  • Posts: 3,216
  • Joined: 19-January 10

Re: Dynamic delete in a form

Posted 02 October 2010 - 05:19 PM

Switch imagename with Delete.

You see, when you echo $imageName into to name, it will insert the image name.

Now php will be looking for this (say img1.gif). The post would need to be $_POST['img1.gif']; for it to receive it. But instead, it's like, "Wut? What's this variable".
Was This Post Helpful? 0
  • +
  • -

#3 SueLin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-October 09

Re: Dynamic delete in a form

Posted 02 October 2010 - 05:35 PM

Thank you for your reply creativecoding.

I'm still having an issue so I just want to make sure i understand.
When I switched $imageName for Delete in the form.

  <form method="post" action="index.php">
		<input name= '$imageName' type="submit"  size="3" value=<?php echo $imageName[$x]; ?> />
          </form>


The image names are now on the button instead of delete.

And when I tried this:


 <?php
	
            if (isset($_POST['Delete'])){


if (file_exists('photos/' . $imageName[$x])){
		(unlink('photos/' . $imageName[$x]));
		

		 
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=index.php">';
			echo 'file deleted';
			
		}
		else{
			
	
	echo 'You have the wrong path';

			}

			}
			
?>



Nothing happened.
Sorry, I'm just getting something mixed here.
Was This Post Helpful? 0
  • +
  • -

#4 creativecoding   User is offline

  • Hash != Encryption
  • member icon


Reputation: 931
  • View blog
  • Posts: 3,216
  • Joined: 19-January 10

Re: Dynamic delete in a form

Posted 02 October 2010 - 05:40 PM

<form (stuff here)>
<input type="hidden" name="fileForDeletion" value="<?php echo filename; ?>">
<input type="submit" value="Delete file">
</form>


How about this?
Was This Post Helpful? 0
  • +
  • -

#5 SueLin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-October 09

Re: Dynamic delete in a form

Posted 02 October 2010 - 05:56 PM

ok, so now here's the form.

 <form method="post" action="index.php">
        <input type="hidden" name="$imageName" value="<?php echo $imageName[$x]; ?>">
        <input type="submit" value="Delete file">
 </form>


And here is the post:
<?php
	
            if (isset($_POST['$imageName'])){


if (file_exists('photos/' . $imageName[$x])){
        (unlink('photos/' . $imageName[$x]));
		

	echo 'file deleted';
			
		    }else{	
	
	echo 'file delete failed';

   }

 }
			
?>


I must have something wrong in the post section. How can I get
the value into the 'unlink'?
Was This Post Helpful? 0
  • +
  • -

#6 creativecoding   User is offline

  • Hash != Encryption
  • member icon


Reputation: 931
  • View blog
  • Posts: 3,216
  • Joined: 19-January 10

Re: Dynamic delete in a form

Posted 02 October 2010 - 06:01 PM

Post file:

<?php
	
            if (isset($_POST['$imageName'])){


if (file_exists('photos/' . $imageName[$x]){
        if(unlink('photos/' . $imageName[$x]){
		

	echo 'file deleted';
else{
echo 'file delete failed';
}
			
		    }else{	
	
	echo 'file delete failed';

   }

 }
			
?>


html file:
<form method="post" action="index.php">
        <input type="hidden" name="imageName" value="<?php echo $imageName[$x]; ?>">
        <input type="submit" value="Delete file">
 </form>

Was This Post Helpful? 0
  • +
  • -

#7 SueLin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-October 09

Re: Dynamic delete in a form

Posted 02 October 2010 - 06:20 PM

I received this error.

Undefined offset: line 116
Undefined offset: line 117


Line 116 and 117 are:

if (file_exists('photos/' . $imageName[$x]){
if(unlink('photos/' . $imageName[$x]){
Was This Post Helpful? 0
  • +
  • -

#8 creativecoding   User is offline

  • Hash != Encryption
  • member icon


Reputation: 931
  • View blog
  • Posts: 3,216
  • Joined: 19-January 10

Re: Dynamic delete in a form

Posted 02 October 2010 - 06:21 PM

View PostSueLin, on 02 October 2010 - 05:20 PM, said:

I received this error.

Undefined offset: line 116
Undefined offset: line 117


Line 116 and 117 are:

if (file_exists('photos/' . $imageName[$x]){
if(unlink('photos/' . $imageName[$x]){


Oops, silly error.

if (file_exists('photos/' . $imageName[$x])){
if(unlink('photos/' . $imageName[$x])){
Was This Post Helpful? 0
  • +
  • -

#9 SueLin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-October 09

Re: Dynamic delete in a form

Posted 02 October 2010 - 06:27 PM

Sorry, had actually added those but getting the same error.
It works with a hard coded image so it has to be the path.
Was This Post Helpful? 0
  • +
  • -

#10 creativecoding   User is offline

  • Hash != Encryption
  • member icon


Reputation: 931
  • View blog
  • Posts: 3,216
  • Joined: 19-January 10

Re: Dynamic delete in a form

Posted 02 October 2010 - 06:30 PM

Quote

It means you're referring to an array key that doesn't exist. "Offset"
refers to the integer key of a numeric array, and "index" refers to the
string key of an associative array.

$s_arr=array("apple", "banana");
print $s_arr[2]; //undefined offset

$a_arr=array("fruit"=>"apple", "meat"=>"beef");
print $a_arr['produce']; //undefined index


~ http://bytes.com/top...undefined-index
Was This Post Helpful? 0
  • +
  • -

#11 SueLin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-October 09

Re: Dynamic delete in a form

Posted 02 October 2010 - 06:45 PM

So I tried writing this in the submit.

foreach ($imageName as $image){
	echo $image;
}


And I got a list of the image names.

And when I wrote this:

echo $imageName[2];


I got the third image in the file.

So, I'm still having a hard time understanding why $imageName[$x]
can't access the files on _POST for unlink.
Was This Post Helpful? 0
  • +
  • -

#12 SueLin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-October 09

Re: Dynamic delete in a form

Posted 02 October 2010 - 07:31 PM

Ok, so I have one more question.
Thank you for the input on what the error message was referring to.

So I added:
  foreach ($imageName as $key => $value){
				$key. " = " . $value;}
				


if (file_exists('photos/'. $value)){
		if(unlink('photos/' . $value)){


The unlink is working but the files are deleting in order
from last to first. How can I get the correct index number
to be deleted?

I really appreciate all your time.
Lindie
Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,998
  • Joined: 08-August 08

Re: Dynamic delete in a form

Posted 02 October 2010 - 08:37 PM

If you had properly indented your code:

<?php
foreach ($imageName as $key => $value){
	$key. " = " . $value;
}
if (file_exists('photos/'. $value)){
	if(unlink('photos/' . $value)){


it would have been easy to see that you're going through your entire foreach loop doing something (???) and then sending the last $value to the if statement after the loop.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1