Jump to content

[SOLVED] php and buttons


kev wood

Recommended Posts

is it possible to get php to run some code on a button click.  i have some code that deletes files from the server but i want the user to be able to delete the image that has been uploaded if it is the wrong image.  i have set my form up as follows

 

	<form name="delete" method="post" enctype="multipart/form-data">
	<input name="Delete" type="button" id="image_del" value="remove image" />
</form>

 

if i set up a page with the code on for the image deletion and put action="delete.php" would this then just run the code on that page and remove the image from the server.

Link to comment
https://forums.phpfreaks.com/topic/109549-solved-php-and-buttons/
Share on other sites

i have changed my code it now looks like this

 

	<form name="delete" method="post" action=action="<?=$_SERVER['PHP_SELF'];?>">
	<input name="Delete" type="button" id="image_del" value="remove image" />
</form>

 

and the code for dealing with the posted information is

 

if(isset($_POST['delete'])) {
$dir = 'image/thumbs/';
foreach(glob($dir.'*thumb_image1*') as $v){
    	unlink($v);
}
include_once"table_drop.php";
include_once"table_create.php";
} 

 

the first section of the code is supposed to delete the files from the server.  i no this code works as i am using it on other areas of the site but when i try to run it from a button click it does not remove the files.

i have changed that but there is still nothing happening.  when the button is clicked the page remains the same and the images are still on the server. if i run the code outside of the if isset it works fine and deletes all the images.  when i run the code this way i have a full stop inside of the asterisk's and it deletes everything which has a full stop inside of the file name. 

	<form name="delete" method="post" action=action="<?=$_SERVER['PHP_SELF'];?>">
	<input name="Delete" type="button" id="image_del" value="remove image" />
</form>

ARRRRRGGGHHH! ARRRGGHHHH!

 

Short tags...

 

ARRGHHHH!

 

On topic, though:

You've named your form 'delete' too. I'm not sure if that might affect it. There's not much need to name forms. Also, you've made the action=action="<?php echo $_SERVER['PHP_SELF']; ?>" which is bad ;)

fixed version (from your other post, that got deleted)

[code]
<form id="delete" name="delete" method="post" action="?">
	<input name="delete" type="button" id="image_del" value="remove image" />
</form>
<?php
if(isset($_POST['delete'])) {
print "/n<br /><center>Deleting</center>";
$dir = 'image/thumbs/';
foreach(glob($dir.'*.*') as $v){
    	unlink($v);
}
include_once"table_drop.php";
include_once"table_create.php";
}
?>

[/code]

that didnt work.  something is defo going wrong somewhere Deleting was not printed anywhere on the page.  i will upload the section of code as it is on the page with the other section still around it may have something to do with how i have positioned the code.  ???

 

if(isset($_POST['delete'])) {
print "/n<br /><center>Deleting</center>";
$dir = 'image/thumbs/';
foreach(glob($dir.'*.*') as $v){
    	unlink($v);
}
include_once"table_drop.php";
include_once"table_create.php";
} 

?> 
</p>
	<form name="newad" method="post" enctype="multipart/form-data"  action="">
	<input type="file" name="broad1_image"  >
	<input name="Submit" type="submit" id="image1" value="Upload image" />
</form>
<p>
<form id="remove file" name="delete" method="post" action="<? $_SERVER['PHP_SELF'];?>">
	<input name="delete" type="button" id="image_del" value="remove image" />
</form>

you didn't print PHP_SELF, so this is all that would output when run:


</p>
	<form name="newad" method="post" enctype="multipart/form-data"  action="">
	<input type="file" name="broad1_image"  >
	<input name="Submit" type="submit" id="image1" value="Upload image" />
</form>
<p>
<form id="remove file" name="delete" method="post" action="<? $_SERVER['PHP_SELF'];?>">
	<input name="delete" type="button" id="image_del" value="remove image" />
</form>

<?php
if(isset($_POST['delete'])) {
print "/n<br /><center>Deleting</center>";
$dir = 'image/thumbs/';
foreach(glob($dir.'*.*') as $v){
    	unlink($v);
}
include_once"table_drop.php";
include_once"table_create.php";
} 

?> 
</p>
	<form name="newad" method="post" enctype="multipart/form-data"  action="">
	<input type="file" name="broad1_image"  >
	<input name="Submit" type="submit" id="image1" value="Upload image" />
</form>
<p>
<form id="remove file" name="delete" method="post" action="<?php print $_SERVER['PHP_SELF'];?>">
	<input name="delete" type="button" id="image_del" value="remove image" />
</form>

so at least i now know it is not deleting or even attempting to delete the files.  i have never used php to interact with a form in this way before can it be done?

 

if i put the delete code on a different page and instead of getting it to try and do it from the same page redirect the user with the action attribute to the page that deletes the files.  on that page just have files being deleted printed across then use some code to redirect the user to the page they where on.

i got it working eventually.  the code i ended up using is as follows

 

the form:

	<form name="remove_file" method="post" action="unlink_file_broad.php">
	<input name="delete" type="submit" id="delete" value="remove image" />
</form>

 

unlink_file_broad.php

 

<?php
$dir = 'image/thumbs/';
foreach(glob($dir.'*thumb_image1*') as $v){
    unlink($v);
}

$dir2 = 'image/';
foreach(glob($dir2.'*image1*') as $v){
    unlink($v);
}

header('location:http://www.acmeart.co.uk/mercury/upload.php');

?>

 

the code works by sending the user to the unlink page and then redirects the user back to the page they where on.  as the page with this button is in an iframe it works good as the page holding the iframe does not reload so the user does not even no the page has been reloaded.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.