Cancel A Webform Submit With Php
Solution 1:
You can't look at the button with php - the whole request will have been sent before PHP gets that information.
What you could do is put the cancel button in a different <form>
, like this:
<form><inputtype="file> <input type="submit" value="upload"></form><form><inputtype="submit"value="cancel"></form>
(This is just an example of course - there's bits missing).
When they click cancel, the browser should abandon its request and start a new one, cancelling the upload.
Solution 2:
Greg has the right idea there. In stead of following the majority, or looking at how things are done, look at smart alternatives, like Greg's explanation above.
Any upload cannot function without a valid link between the client and the server; hence if you close your browser window, or re-direct it to some place else, the upload is killed.
use an iframe and name it eg.: "myframe". you can easily hide it in a div. have a form with your action="somefile.php" and target="myframe". add a lil Javascript to your form's "file" field: onFocus="uploadlistener()". you can name this function anything you like, but use it to check if the person "opened" anything. the browser will automatically swap focus, whether, the user clicked "browse", or if he then opened a file. difference is, after a file has been "selected", the input field receives the focus again, but remember you have a listener on the "onFocus()" event. so: if the field is not empty, call: document.uploadform.submit()
Using this method, you don't even need a submit button. if you want to skin your "browse" button, just make it transparent using CSS, eg: input name="myfile" type="file" style="width: 40px; -moz-opacity: 0; filter: alpha(opacity=0)" onFocus="somefunction()" Well to cancel upload just redirect the iframe to some other place, eg: input type="button" value="cancel" onClick="document.uploadform.action='blank.php'; document.uploadform.submit()"
Pardon if the html above spans across multiple lines, it's the first time I'm posting here.
To track progress, you need to add a server side listener, either with PHP-5.2, or some Perl script to check how many bytes have been loaded thus far of the total file size checked before upload was started. this check interval can be achived with some AJAX, or if you don't know the term, look up: HTTP-REQUEST. this runs server side code in the background and outputs a response like you would see in the browser, but here you can capture it in a variable, and do your stuff. I hope this was helpful to anyone.
Solution 3:
There's no way for PHP to stop a file upload in progress. Your PHP code isn't even started until the file upload is done, so by the time it's running, it's too late.
Solution 4:
If it indeed turns out that cancelling the upload is not possible, you might consider implementing an easy to use undo or delete function, so that users can immediately undo the uploading of the image (i.e. the image is deleted). This may not be useful in your experiment, but perhaps you can use it in a production application?
Post a Comment for "Cancel A Webform Submit With Php"