0

I have the following php script:

<?php

$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
$submit = $_REQUEST['submit'];
$to = "torayevagajan@gmail.com";

if(isset($submit))
{

    function isValidEmail($email)
    {
        return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);
    }

    if(!empty($name) && !empty($email) && !empty($message) && isValidEmail($email))
    {
        $text = "From: ".$email."<br>Name: ".$name."<br>".$message;
        if(mail($to, $subject, $text) && !empty($submit)){
            echo "<script type=\"text/javascript\">alert(\"Your message has been sent!\");</script>";
            }
        else{
            echo "<script type=\"text/javascript\">alert(\"Error Occured!\");</script>";
            }
    }elseif(empty($name) || empty($email) || empty($message))
    {
        echo "<script type=\"text/javascript\">alert(\"Please fill in the all fields!\");</script>";
    }else echo "<script type=\"text/javascript\">alert(\"Incorrect email format!\");</script>";

    unset($submit);
}
?>

I have the following problem, after submitting details to contact php, whenever I want to refresh page, it tries again and again send mail, I mean it tries to run script, even I unset($submit); how can fix this?

Justin T.
  • 3,643
  • 1
  • 22
  • 43
user873286
  • 7,799
  • 7
  • 30
  • 38
  • you get your `$_REQUEST` data from `$_POST` or `$_GET` ? either way, submission on page reload is normal behavior. but if you use `$_GET` you could try to change `window.location` with javascript on successful submit. – maialithar Jan 02 '12 at 16:53
  • @user873286 Then you should use $_POST not $_REQUEST. – Dejan Marjanović Jan 02 '12 at 16:56
  • possible duplicate of [Prevent form from being submitted twice](http://stackoverflow.com/questions/3614197/prevent-form-from-being-submitted-twice) – hakre Jan 02 '12 at 16:56
  • quick and dirty solution would be to echo small form with `` on successful submit. – maialithar Jan 02 '12 at 16:56

5 Answers5

2

The solution to this problem is called Post Redirect Get

You can look for a sample code solution here :

Simple Post-Redirect-Get code example

Community
  • 1
  • 1
Justin T.
  • 3,643
  • 1
  • 22
  • 43
0

1) You shouldn't use $_REQUEST as it opens some security issues for your application. Use $_POST instead.

2) Why do you need to refresh the contact page? An alternative is to have a separate script for processing the form submission and then redirect back to the original contact page upon completion.

Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
0

Dude refresh means send the request again which will consist the varaiables also to be resent.. That's why many websites use such caution to not refresh after submit...

But there is a solution for that..

Redirect the script to the same page after you send the mail..

header('Location: http://www.example.com/contact.php');

That way it will resend the page request to the same page but this time with no variables..

Or send them to another page after successful send of mail..

Rajat Singhal
  • 11,234
  • 5
  • 38
  • 56
  • LOL @Rajat _"Dude refresh means send the request again"_ –  Jan 02 '12 at 17:00
  • Dude try this and then comment: make a form, make action as another page, and on that page of action insert that data to database...then keep your user to that action page, and refresh that..and check your database..with(f5) or refresh button... – Rajat Singhal Jan 02 '12 at 17:02
  • problem is: home page, about, contact are all in the same page. One page design. So after submitting form if there is error I output error, else I echo success. So whenever I refresh the page it tries to resend. I think it is possible with unsetting $submit, why it is not possible? – user873286 Jan 02 '12 at 17:03
  • refresh will not send the variables again if you click on url and again enter..that way you are making an new request to the server.. – Rajat Singhal Jan 02 '12 at 17:03
  • Because if your user will manually refresh the page after submitting the variables (by f5) it will again send the variables.. – Rajat Singhal Jan 02 '12 at 17:05
  • What you can do is if its a success then redirect them to next page with success message..or send form with javascript instead...Dude it is a very long already discussed issue.. – Rajat Singhal Jan 02 '12 at 17:06
  • Or even you can send them to the same page with some get variable as $GET['success']=1, and check on top if it is set then display the success message part.. – Rajat Singhal Jan 02 '12 at 17:09
  • @Rajat chill out bro, not laughing at you, simply appreciating your answer. You're 100% right ... and 99.9% too sensitive :) –  Jan 02 '12 at 17:12
0

When you refresh the page, the browser resends the POST data from your form. Unsetting $submit does not do you any good, as it is being set again every time you execute the script. While I'm not sure what your goal is with refreshing the page, you would have to write code to refresh the page after script has been done. (Unless you want to use sessions to detect if that form has been submitted.)

Anyway, this would work to refresh. Note that you cannot have any output (even whitespace) before the headers.

<?php header('Refresh: 0'); ?>

You would of course have to have this when $submit is set, and not otherwise.

Christian Neverdal
  • 5,655
  • 6
  • 38
  • 93
  • before and after my script there are html elements, I want after submitting contact details, it show me javascript alert and stay on the same page, but as page opened for the first time, I mean if user wants to refresh page it should not send the same values again and try to send message – user873286 Jan 02 '12 at 17:16
0

you unset the var $submit, but not the $_REQUEST['submit'].

so on every reload you set $submit again with the data still stored in the $_REQUEST Array :)

Robert
  • 21
  • 2