Menu
I have a form that is using php. If the user presses refresh on the page they are sent to then the POST values are resent and the php script runs again which causes problems, is there some HTML or PHP to fix this?
Thankyou very much
-moose
Opinions vary on the "best way" to do a thing. My experience is that the "best way" to process a form is to have it submitting to itself -- as this makes it more logical to present form validation error messages and for users to make any needed corrections for resubmission. But... Each to their own.[/QUOTE]
[code=php] if (isset($_POST['my_form'])) {
$notrun = $_POST['notrun'];
if ($notrun == "") { *Script goes here* } else { *oppositescript* }
[/code]
Is that something a 302 does? Because I normally see the [Back] and [Refresh] causing a resubmit of the same post data.[/QUOTE]Have a play with the simple script below. You will find the only way to send a POST request is by pressing the submit button. Pressing refresh on the other hand only submits a GET request.
[code=php]<?php
if(isset($_GET['success'])) # success page
{
echo 'The form submitted ok!';
die;
}
elseif(isset($_POST['submit'])) # handle form here
{
$error = null;
# validate form here... on success send 302
if('form validates')
{
header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?success');
die;
}
else # handle errors here
{
$error = "Some relevant error message here!<br>n";
}
}
echo $error;
# load/reload sticky form
?><form action="" method="post">
<input type="submit" name="submit" value="submit">
</form>[/code]
I don't see any 302 in there. What was mentioning 302 all about?[/QUOTE]...
[URL=http://www.php.net/header]header()[/URL] : The second special case is the "Location:" header. Not only does it send this header to the browser, but it also returns a REDIRECT (302) status code to the browser unless some 3xx status code has already been set.[/QUOTE]
302 doesn't cancel anything. The post data is still sent and validated.[/QUOTE]
Since the browser has been advised that the resource the POST data was sent to has temporarily moved no amount of pressing the refresh button will convince it to resubmit the form to that resource.[/QUOTE]
Duh! That is not what I said[/QUOTE]so_is_this I am not attempting to put words in your mouth. This board is read by people, many of whom do not share English as their mother tongue. My purpose is to make any response I post as clear and useful as possible to all its readers.
Have a play with the simple script below. You will find the only way to send a POST request is by pressing the submit button. Pressing refresh on the other hand only submits a GET request.[/QUOTE]
This depends on the browser, firefox for instance opens a popup asking if you would like to resend the POST data.[/QUOTE]Not with my script posted above!
[CODE]
<?php
include "MIME.class";
define('TO', '[email protected]'); # CHANGE THIS TO A REAL ADDRESS (yours?)
// Has there been a form submission? If yes, go on and
// process...
if (is_array($HTTP_POST_VARS)) {
if ($resume != 'none') {
$fname = './'.$resume_name; // make a real filename
// Get the content-type of the uploaded file
if (preg_match("!/x-.+!i", $resume_type))
$type = OCTET;
else
$type = $resume_type;
$from = sprintf("'%s' <%s>", $name, $email) ;
copy($resume, $fname); //do error checking if need
$mime = new MIME_mail($from, TO, 'online application form', "Please find attached the resume from the online application form from $name", "Cc: $email");
$mime->fattach($fname, "Resume of $name", $type);
$mime->send_mail();
} else {
echo "Dear $name<p>You have not submitted your resume. Please use the browse button to attach it and click send!<br>";
}
}
?>
[/CODE]
I have a similar problem[/QUOTE]Your problem is here:
[code=php] if (is_array($HTTP_POST_VARS)) {[/code]
$HTTP_POST_VARS is always an array.Your problem is here:[code=php] if (is_array($HTTP_POST_VARS)) {[/code]
$HTTP_POST_VARS is always an array.[/QUOTE]
Also with the code i pasted in above, does anyone know how I can get it to put all the files that people send into a folder, say called "resumes". Instead of just storing them in the same flder as the website?[/QUOTE]
move_uploaded_file ( string filename, string destination )[/QUOTE]
[CODE]<?php
include "MIME.class";
define('TO', '[email protected]'); # CHANGE THIS TO A REAL ADDRESS (yours?)
// Has there been a form submission? If yes, go on and
// process...
if (is_array($HTTP_POST_VARS)) {
if ($resume != 'none') {
$fname = './'.$resume_name; // make a real filename
// Get the content-type of the uploaded file
if (preg_match("!/x-.+!i", $resume_type))
$type = OCTET;
else
$type = $resume_type;
$from = sprintf("'%s' <%s>", $name, $email) ;
copy($resume, $fname); //do error checking if need
$mime = new MIME_mail($from, TO, 'online application form', "Please find attached the resume from $name", "Cc: $email");
$mime->fattach($fname, "Resume of $name", $type);
$mime->send_mail();
} else {
if (move_uploaded_file($_FILES["resume_name"]["uploaded_resumes"]))
echo "Dear $name<p>You have not submitted your resume. Please use the browse button to attach it and click send!<br>";
}
}
?>[/CODE]
so i got there.....(is it in the right spot?)
if (move_uploaded_file($_FILES["resume_name"]["uploaded_resumes"]))
is that right?
the files names are "resume_name" and i made a folder on the server called "uploaded_resumes"...[/QUOTE]
You need to read this:
[URL=http://www.php.net/manual/en/features.file-upload.php]Handling File Uploads[/URL] [/QUOTE]
$uploaddir = $_SERVER['DOCUMENT_ROOT']."/home/montica/mainwebsite_html/uploads";
$uploaddir.="resume_name";
//Copy the file to some permanent location
if (move_uploaded_file($_FILES["resume_name"]["tmp_name"], $uploaddir))
...the second argument represents the TO file path[COLOR=Red][B][U]and[/U][/B][/COLOR] name.[/QUOTE]
[CODE]<?php
include "MIME.class";
define('TO', '[email protected]'); # CHANGE THIS TO A REAL ADDRESS (yours?)
// Has there been a form submission? If yes, go on and
// process...
if($_POST){
if ($resume != 'none') {
$fname = './'.$resume_name; // make a real filename
// Get the content-type of the uploaded file
if (preg_match("!/x-.+!i", $resume_type))
$type = OCTET;
else
$type = $resume_type;
$from = sprintf("'%s' <%s>", $name, $email) ;
copy($resume, $fname); //do error checking if need
$mime = new MIME_mail($from, TO, 'online application form', "Please find attached the resume from $name", "Cc: $email");
$mime->fattach($fname, "Resume of $name", $type);
$mime->send_mail();
} else
{
$uploaddir = $_SERVER['DOCUMENT_ROOT']."/home/montica/uploads/";
$uploadfile .= "resume_name/" . basename($_FILES['resume_name']['name']);
//Copy the file to some permanent location
if (move_uploaded_file($_FILES["resume_name"]["tmp_name"], $uploaddir, $uploadfile))
echo "Dear $name<p>You have not submitted your resume. Please use the browse button to attach it and click send!<br>";
}
}
?>[/CODE]
[CODE]
$uploaddir = $_SERVER['DOCUMENT_ROOT']."/home/montica/mainwebsite_html/uploads/";
[COLOR=Blue]$uploaddir[/COLOR] .= "resume_name/" . basename($_FILES['resume_name']['name']);[/CODE]
[CODE]
$uploaddir = $_SERVER['DOCUMENT_ROOT']."/home/montica/mainwebsite_html/uploads/";
[COLOR=Red]$uploadfile[/COLOR] = "resume_name/" . basename($_FILES['resume_name']['name']);[/CODE]
[CODE]
if (move_uploaded_file($_FILES["resume_name"]["tmp_name"], $uploaddir [COLOR=Red][B].[/B][/COLOR] $uploadfile))
______________________________________________________________________^[/CODE]
[CODE]<?php
include "MIME.class";
define('TO', '[email protected]'); # CHANGE THIS TO A REAL ADDRESS (yours?)
// Has there been a form submission? If yes, go on and
// process...
if($_POST){
if ($resume != 'none') {
$fname = './'.$resume_name; // make a real filename
// Get the content-type of the uploaded file
if (preg_match("!/x-.+!i", $resume_type))
$type = OCTET;
else
$type = $resume_type;
$from = sprintf("'%s' <%s>", $name, $email) ;
copy($resume, $fname); //do error checking if need
$mime = new MIME_mail($from, TO, 'online application form', "Please find attached the resume from $name", "Cc: $email");
$mime->fattach($fname, "Resume of $name", $type);
$mime->send_mail();
} else
{
$uploaddir = $_SERVER['DOCUMENT_ROOT']."/home/montica/uploads/";
$uploadfile = "resume_name/" . basename($_FILES['resume_name']['name']);
//Copy the file to some permanent location
if (move_uploaded_file($_FILES["resume_name"]["tmp_name"], $uploaddir . $uploadfile))
echo "Dear $name<p>You have not submitted your resume. Please use the browse button to attach it and click send!<br>";
}
}
?>[/CODE]
first up is this line..
if ($resume != 'none')
I don't see any declaration of 'resume' anywhere in your code.
Let's assume that somewhere you've got
$resume = $_POST['resume'][/QUOTE]
[code=php]<?php
if($_SERVER['REQUEST-TYPE'] == 'POST') {
if($_FILES['resume_name']['tmp_name'] == 'none') {
echo "<p>Dear $name</p>n<p>You have not submitted your resume. Please use the browse button to attach it and click send!</p>n";
} else {
if(!is_uploaded_file($_FILES['resume_name']['tmp_name'])) {
echo "<p>Possible file upload attack!</p>n";
exit;
}
$uploaddir = $_SERVER['DOCUMENT_ROOT']."/home/montica/uploads/";
$uploadfile = "resume_name/" . basename($_FILES['resume_name']['name']);
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir . $uploadfile)) {
echo "<p>File is valid, and was successfully uploaded.</p>n";
} else {
echo "<p>File move falied.</p>n";
}
}
}
?>[/code]
[CODE]
<script language=javascript>
<!--
function submitMyForm()
{
document.formname.action = "your_form_action_page.php"
document.newissue.target = "_blank"; // a new window
document.newissue.submit(); // Submit
window.location.href="/index.php"; // redirect this window to some kind of "Your form was successfully submitted page"
return true;
}
-->
</script>[/CODE]
[code=php]echo "<script>javascript:window.open('/close.html', '_self');</script>";[/code]
[code=html] <html><body>
<script language="JavaScript">window.close();</script>
</body></html>[/code]
0.1.9 — BETA 5.25