May 22, 200917 yr Hi, I want the page to be reloaded after the user hits 'submit' Before, this can happen, a function is called by submit i.e. if(isset($_POST['Submit'])) { //rest of code so, maybe put somewhere in function ??? Edited January 29, 201016 yr by Sam G added [code] tags
May 22, 200917 yr You could use header to redirect the user back to the same page: header('Location: http://www.example.com/page.php'); You need to use the full path with header (i.e. http:// blah blah etc.)
May 22, 200917 yr Author You could use header to redirect the user back to the same page: header('Location: http://www.example.com/page.php'); You need to use the full path with header (i.e. http:// blah blah etc.) I actually did try that and got an error Warning: Cannot modify header information - headers already sent
May 22, 200917 yr Try placing this at the top of your page: <? ob_start(); ?> then at the bottom of the page place this line of code: <? ob_flush(); ?> Does it work then?
May 22, 200917 yr to use headers - they must be submitted to the page before ANY html is parsed (even comments).
May 23, 200917 yr you could use javascript, ill try and find you the code i use... for a refresh button, inside the site.
May 23, 200917 yr <head> <script LANGUAGE="JavaScript"> </script> </head> <body> <form> <input type="button" value="Refresh" onclick="history.go()" class="refresh" /> </form> </body> just mix it with the current submit button you have.
May 24, 200917 yr Author <head> <script LANGUAGE="JavaScript"> </script> </head> <body> <form> <input type="button" value="Refresh" onclick="history.go()" class="refresh" /> </form> </body> just mix it with the current submit button you have. Cheers, will try it out
May 24, 200917 yr I would use the method posted by Sam_G. The fact that you get the error suggests that your form handling method has some code which creates some unnecessary output. If you post the code we could perhaps find out which line of code is sending output to the browser and you could remove it, and then your redirect using Header will work fine.
May 25, 200917 yr Author Try placing this at the top of your page: <? ob_start(); ?> then at the bottom of the page place this line of code: <? ob_flush(); ?> Does it work then? Thanks for the input Sam
May 25, 200917 yr <input type="button" value="Refresh" onclick="history.go()" class="refresh" /> Although, if course it's better to not use inline Javascript.... I think it would help if we could take a look at the script. From the sounds of it, your PHP can easily be constructed to run a function when the form is submitted - but if you want the function to occur as the user presses submit - it's a JS problem. From what I'm reading, I don't think JS is the answer.
May 25, 200917 yr Author Although, if course it's better to not use inline Javascript.... I think it would help if we could take a look at the script. From the sounds of it, your PHP can easily be constructed to run a function when the form is submitted - but if you want the function to occur as the user presses submit - it's a JS problem. From what I'm reading, I don't think JS is the answer. Hi, yes I want the page to reload once the user has clicked 'submit' / 'delete'
May 25, 200917 yr Sure, I can see that from the opening post - but perhaps something is getting lost in translation. Is there a separate submit/delete button? What is the page's function? For example: You can make a contact page that submits to itself (i.e: action="contactpage.php") and use PHP conditionals to alter or preserve the form accordingly. The page can be constructed so that the contact form displays until criteria x has been established, and then it will only display the response message - i.e: "Thankyou for your email" once the form's action has been executed. No need for two separate pages. You can use headers too - but it depends on the context as to whether or not they're appropriate. Another alternative is that you can wrap up an anchor tag in a button graphic that passes a $_GET variable to the same page, so once the user presses the button it reloads with the additional information passed in the URL. Just make sure you don't use this method to hit a database. Anyway, if we could get a more detailed picture of your aims, it would be easier to work out a solution.
May 26, 200917 yr Or you could create a separate page thats only role is to process the result and then redirect back to the forms page.
May 26, 200917 yr Author Sure, I can see that from the opening post - but perhaps something is getting lost in translation. Is there a separate submit/delete button? What is the page's function? For example: You can make a contact page that submits to itself (i.e: action="contactpage.php") and use PHP conditionals to alter or preserve the form accordingly. The page can be constructed so that the contact form displays until criteria x has been established, and then it will only display the response message - i.e: "Thankyou for your email" once the form's action has been executed. No need for two separate pages. You can use headers too - but it depends on the context as to whether or not they're appropriate. Another alternative is that you can wrap up an anchor tag in a button graphic that passes a $_GET variable to the same page, so once the user presses the button it reloads with the additional information passed in the URL. Just make sure you don't use this method to hit a database. Anyway, if we could get a more detailed picture of your aims, it would be easier to work out a solution. Okay, Ill tell you more about it I needed to write code in PHP that would present an end user with a simple, easy to use interface, to delete images from a specified folder, e.g. homepage pictures that will be used in a flash gallery. The code works fine, the user is brought to the page whereby they can delete images by checking a box beside all the images they wish to delete, then the user must click 'delete' (i.e. page only contains one delete button). I also have some code included in the same, 'deleteImage.php' file, to renumber the remaining images in a specified folder, in this example, the homepage folder. This needs to be done, so that there are no gaps in the sequential order, i.e. so that each image can be read and included in the flash gallery, i.e. 1.jpg, 2.jpg, 3.jpg, etc So, this is all works fine, but I noticed a few problems while I was testing. The image is deleted and the rest of the images are renumbered fine, but the view for the end user needs to be reloaded / refreshed after they click 'delete', so the new information is updated That clear enough? Emma
May 26, 200917 yr Author Try placing this at the top of your page: <? ob_start(); ?> then at the bottom of the page place this line of code: <? ob_flush(); ?> Does it work then? Sorry Sam, I meant to ask you, what exactly is this supposed to be doing?
May 26, 200917 yr Author I would use the method posted by Sam_G. The fact that you get the error suggests that your form handling method has some code which creates some unnecessary output. If you post the code we could perhaps find out which line of code is sending output to the browser and you could remove it, and then your redirect using Header will work fine. <?php $path = "C:apache/webserver/htdocs/modxHAT/gallery/home/"; if(isset($_POST['file']) && is_array($_POST['file'])){ foreach($_POST['file'] as $file) { unlink($path . "/" . $file) or die("Failed to delete file"); } } ?> <form name="form1" method="post"> <?php $path = "C:apache/webserver/htdocs/modxHAT/gallery/home/"; $dir_handle = @opendir($path) or die("Unable to open folder"); while (false !== ($file = readdir($dir_handle))) { if($file == "index.php") continue; if($file == ".")continue; if($file == "..")continue; echo "<input type='CHECKBOX' name='file[]' value='$file'>"; echo "<img src='/modxHAT/gallery/home/$file' alt='$file'><br />"; } closedir($dir_handle); ?> <input type="submit" name="Delete" value="Delete" onclick="history.go()" class="refresh"> </form> <?php if(isset($_POST['Delete'])) { // code to re-number files // code works fine }?> So basically, everything works fine, its the view that is presented to the user needs to be uploaded / refreshed each time the 'delete' button is hit
May 26, 200917 yr That clear enough? Yup, sounds simple enough. Does the page submit to itself? How are you obtaining a resource handle for the images you want to display? Is it a database call or a readdir type function?
May 26, 200917 yr Author Yup, sounds simple enough. Does the page submit to itself? How are you obtaining a resource handle for the images you want to display? Is it a database call or a readdir type function? Good stuff! Yes the page submits to itself The files are stored in a folder called /home basically and I use a readdir type function Need anymore info?
May 26, 200917 yr No, that seems straightforward enough. So, once the page submits, you read the post array - use the contents to delete the relevant images, then call the readdir function to display the folders new contents. As long as you process everything in sequence, this should be pretty straightforward.
May 27, 200917 yr Author No, that seems straightforward enough. So, once the page submits, you read the post array - use the contents to delete the relevant images, then call the readdir function to display the folders new contents. As long as you process everything in sequence, this should be pretty straightforward. Right, I feel like I'm back where I started about 10 posts ago, I need some code to do a reload and I need to know where to put that in?
May 27, 200917 yr <snip> DOH! My apologies, didn't see you have actually posted your code!! Lol - my bad Emms - I'll take another look.
May 27, 200917 yr <?php $path = "C:apache/webserver/htdocs/modxHAT/gallery/home/"; if(isset($_POST['file']) && is_array($_POST['file'])){ foreach($_POST['file'] as $file) { unlink($path . "/" . $file) or die("Failed to delete file"); } } ?> <form name="form1" method="post"> <?php $path = "C:apache/webserver/htdocs/modxHAT/gallery/home/"; $dir_handle = @opendir($path) or die("Unable to open folder"); while (false !== ($file = readdir($dir_handle))) { if($file == "index.php") continue; if($file == ".")continue; if($file == "..")continue; echo "<input type='CHECKBOX' name='file[]' value='$file'>"; echo "<img src='/modxHAT/gallery/home/$file' alt='$file'><br />"; } closedir($dir_handle); ?> <input type="submit" name="Delete" value="Delete" onclick="history.go()" class="refresh"> </form> <?php if(isset($_POST['Delete'])) { // code to re-number files // code works fine }?> So basically, everything works fine, its the view that is presented to the user needs to be uploaded / refreshed each time the 'delete' button is hit Hi emms, I've had a look at that code and think I have a possible solution. Backup the code you already have, and then replace it with this: <?php $path = "C:apache/webserver/htdocs/modxHAT/gallery/home/"; $form = ''; if(isset($_POST['file']) && is_array($_POST['file'])){ foreach($_POST['file'] as $file) { unlink($path . "/" . $file) or die("Failed to delete file"); } } $form .= '<form name="form1" method="post">'; $dir_handle = @opendir($path) or die("Unable to open folder"); while (false !== ($file = readdir($dir_handle))) { if($file == "index.php") continue; if($file == ".") continue; if($file == "..") continue; $form .= "<input type='CHECKBOX' name='file[]' value='$file'>"; $form .= "<img src='/modxHAT/gallery/home/$file' alt='$file'><br />"; } closedir($dir_handle); $form .= '<input type="submit" name="Delete" value="Delete" class="refresh">'; $form .= '</form>'; if(isset($_POST['Delete'])) { // code to re-number files // code works fine } echo $form; ?> Insert the Header("Location")... wherever you need to perform redirection and it should work, as long as you do it before "echo $form;"
May 27, 200917 yr ok, i've jiggered it about a bit, and this seems to work locally. You'll probably have to take a look at the form construction and how it assigns values and names to the images - but it seems to work. <?php $path = "/your/path/"; //process form IF submitted if(isset($_POST['submitted'])) { if(isset($_POST['file']) && is_array($_POST['file'])) { foreach($_POST['file'] as $file) { unlink($path . "/" . $file) or die("Failed to delete file"); } } } //OBTAIN DIRECTORY CONTENTS $dir_handle = @opendir($path) or die("Unable to open folder"); while (false !== ($file = readdir($dir_handle))) { if($file == "index.php") continue; if($file == ".")continue; if($file == "..")continue; $newfile[]=$file; } closedir($dir_handle); ?> <form name='form1' action="thispage.php" method='post'> <?php foreach ($newfile as $n) { echo "<input type='CHECKBOX' name='file[]' value='$n'>"; echo "<img src='/your/path/$n'><br />"; } ?> <input type="submit" name="Delete" value="Delete" class="refresh"> <input type="hidden" name="submitted"> </form>
May 27, 200917 yr I know you probably did check this but are you sure it is actually deleting the file? And you missed this off action="thispage.php"
May 27, 200917 yr I've checked it locally, and the files are being deleted, in multiples and singles - and the file names are visible in the $_POST array. I'm not sure what you mean when I missed 'action = "thispage.php" '....?
May 27, 200917 yr I've checked it locally, and the files are being deleted, in multiples and singles - and the file names are visible in the $_POST array.I'm not sure what you mean when I missed 'action = "thispage.php" '....? I think he means that the form action attribute hasn't been set. This doesn't matter as long as the method is set. When no action is set, the form submits itself to the current page anyway.
May 27, 200917 yr Author ok, i've jiggered it about a bit, and this seems to work locally. You'll probably have to take a look at the form construction and how it assigns values and names to the images - but it seems to work. <?php $path = "/your/path/"; //process form IF submitted if(isset($_POST['submitted'])) { if(isset($_POST['file']) && is_array($_POST['file'])) { foreach($_POST['file'] as $file) { unlink($path . "/" . $file) or die("Failed to delete file"); } } } //OBTAIN DIRECTORY CONTENTS $dir_handle = @opendir($path) or die("Unable to open folder"); while (false !== ($file = readdir($dir_handle))) { if($file == "index.php") continue; if($file == ".")continue; if($file == "..")continue; $newfile[]=$file; } closedir($dir_handle); ?> <form name='form1' action="thispage.php" method='post'> <?php foreach ($newfile as $n) { echo "<input type='CHECKBOX' name='file[]' value='$n'>"; echo "<img src='/your/path/$n'><br />"; } ?> <input type="submit" name="Delete" value="Delete" class="refresh"> <input type="hidden" name="submitted"> </form> We'll get there, we'll get there eventually lol I'll try this out and also the code posted by mr brightside thanks very much for all the efforts, Ill let you know how I get on
May 27, 200917 yr Author Hi emms, I've had a look at that code and think I have a possible solution. Backup the code you already have, and then replace it with this: <?php $path = "C:apache/webserver/htdocs/modxHAT/gallery/home/"; $form = ''; if(isset($_POST['file']) && is_array($_POST['file'])){ foreach($_POST['file'] as $file) { unlink($path . "/" . $file) or die("Failed to delete file"); } } $form .= '<form name="form1" method="post">'; $dir_handle = @opendir($path) or die("Unable to open folder"); while (false !== ($file = readdir($dir_handle))) { if($file == "index.php") continue; if($file == ".") continue; if($file == "..") continue; $form .= "<input type='CHECKBOX' name='file[]' value='$file'>"; $form .= "<img src='/modxHAT/gallery/home/$file' alt='$file'><br />"; } closedir($dir_handle); $form .= '<input type="submit" name="Delete" value="Delete" class="refresh">'; $form .= '</form>'; if(isset($_POST['Delete'])) { // code to re-number files // code works fine } echo $form; ?> Insert the Header("Location")... wherever you need to perform redirection and it should work, as long as you do it before "echo $form;" Heya, thanks for the effort, I'll try this out and get back to you
June 7, 200917 yr Author So, did either of those work Emms? Would you believe that I havn't had a minute to try it out yet, shocking I know, will do it today or tomorrow and PM ya
Create an account or sign in to comment