kev wood Posted June 10, 2008 Share Posted June 10, 2008 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. Quote Link to comment Share on other sites More sharing options...
kev wood Posted June 10, 2008 Author Share Posted June 10, 2008 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. Quote Link to comment Share on other sites More sharing options...
trq Posted June 10, 2008 Share Posted June 10, 2008 Your button is named Delete, not delete. Quote Link to comment Share on other sites More sharing options...
kev wood Posted June 10, 2008 Author Share Posted June 10, 2008 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. Quote Link to comment Share on other sites More sharing options...
conker87 Posted June 10, 2008 Share Posted June 10, 2008 <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 Quote Link to comment Share on other sites More sharing options...
kev wood Posted June 10, 2008 Author Share Posted June 10, 2008 thanks for pointing the typo out i will give that a go and see what happens. Quote Link to comment Share on other sites More sharing options...
kev wood Posted June 10, 2008 Author Share Posted June 10, 2008 i renamed the form and gave the button a name and id just in case it had anything to do with that also and that didnt work the images remain on the server. Quote Link to comment Share on other sites More sharing options...
FlyingIsFun1217 Posted June 10, 2008 Share Posted June 10, 2008 Check to make sure you are able to use all functions (ie, if you are on free hosting). FlyingIsFun1217 Quote Link to comment Share on other sites More sharing options...
conker87 Posted June 10, 2008 Share Posted June 10, 2008 Hmm. Try: if($_POST['delete']) { $dir = 'image/thumbs/'; foreach(glob($dir.'*thumb_image1*') as $v) { unlink($v); } include_once "table_drop.php"; include_once "table_create.php"; } Quote Link to comment Share on other sites More sharing options...
jonsjava Posted June 10, 2008 Share Posted June 10, 2008 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] Quote Link to comment Share on other sites More sharing options...
kev wood Posted June 10, 2008 Author Share Posted June 10, 2008 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> Quote Link to comment Share on other sites More sharing options...
jonsjava Posted June 10, 2008 Share Posted June 10, 2008 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> Quote Link to comment Share on other sites More sharing options...
kev wood Posted June 10, 2008 Author Share Posted June 10, 2008 tried that it didnt work either. what was the print for what should this have made this do. Quote Link to comment Share on other sites More sharing options...
jonsjava Posted June 10, 2008 Share Posted June 10, 2008 that was to let you know that it's deleting properly (error checking only) Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 10, 2008 Share Posted June 10, 2008 Your form and input button have the same name. Quote Link to comment Share on other sites More sharing options...
kev wood Posted June 10, 2008 Author Share Posted June 10, 2008 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. Quote Link to comment Share on other sites More sharing options...
kev wood Posted June 10, 2008 Author Share Posted June 10, 2008 Your form and input button have the same name. i thought that might have been a problem and changed that earlier still no joy with that tho Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 10, 2008 Share Posted June 10, 2008 Your form and input button have the same name. i thought that might have been a problem and changed that earlier still no joy with that tho Change it now that you have your action= fixed. Quote Link to comment Share on other sites More sharing options...
kev wood Posted June 10, 2008 Author Share Posted June 10, 2008 it still not work. is it possible to make this work how i would like it to? or am i better going down the road i mentioned two posts ago. Quote Link to comment Share on other sites More sharing options...
kev wood Posted June 10, 2008 Author Share Posted June 10, 2008 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. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 10, 2008 Share Posted June 10, 2008 Ajax is better than iFrames. Try using it instead. P.S: I hate iframes. Quote Link to comment Share on other sites More sharing options...
kev wood Posted June 10, 2008 Author Share Posted June 10, 2008 i no it is better but it was quicker to do it this way. i get lazy sometimes. ha ha Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.