Avoiding double-submissions with mail script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chuck W.

    Avoiding double-submissions with mail script

    So I have a very simple mail script just to send a quick 'n dirty
    notification of a form posting -- it goes like this:

    $message = $_POST["salutation "] . "\", first_name = \"".
    $_POST["first_name "] . "\",last_na me = \"". $_POST["last_name"] .
    "\",title = \"" . $_POST["title"] . "\", company = \"" .
    $_POST["company"] . "\", leadsource = \"" . $_POST["lead_sourc e"] ;

    $headers .= 'From: Sender <sender@blahbla h.com>' . "\r\n";
    $subject = 'New record notification';
    $to = 'recipient@blah blah.com';

    // Mail it
    mail($to, $subject, $message, $headers);

    Crude, huh. Works though.

    So I'm wondering how can I code this thing so when someone hits the
    submit button on the form twice it doesn't send two email messages (or
    more)? Wrap a session variable around it somehow?

    Thanks,

    Chuck
    PHP Noob

  • Jim Dornbos

    #2
    Re: Avoiding double-submissions with mail script

    On 1 Feb 2006 09:49:44 -0800, "Chuck W." <cwyatt@w3minds .com> wrote:
    [color=blue]
    >So I have a very simple mail script just to send a quick 'n dirty
    >notification of a form posting -- it goes like this:[/color]
    [color=blue]
    >$message = $_POST["salutation "] . "\", first_name = \"".
    >$_POST["first_name "] . "\",last_na me = \"". $_POST["last_name"] .
    >"\",title = \"" . $_POST["title"] . "\", company = \"" .
    >$_POST["company"] . "\", leadsource = \"" . $_POST["lead_sourc e"] ;
    >
    >$headers .= 'From: Sender <sender@blahbla h.com>' . "\r\n";
    >$subject = 'New record notification';
    >$to = 'recipient@blah blah.com';
    >
    >// Mail it
    >mail($to, $subject, $message, $headers);
    >
    >So I'm wondering how can I code this thing so when someone hits the
    >submit button on the form twice it doesn't send two email messages (or
    >more)? Wrap a session variable around it somehow?[/color]

    From one newb to another... in addtion to mailing the message, I would
    unset a couple of (all?) your $_POST values. Then add some code prior
    to mailing to make sure they have values. Hitting submit once would
    send the message. Hitting submit a second time would throw up your
    error message about missing values (because you just wiped them out.)

    Jim

    Comment

    • Gordon Burditt

      #3
      Re: Avoiding double-submissions with mail script

      >From one newb to another... in addtion to mailing the message, I would[color=blue]
      >unset a couple of (all?) your $_POST values. Then add some code prior
      >to mailing to make sure they have values. Hitting submit once would
      >send the message. Hitting submit a second time would throw up your
      >error message about missing values (because you just wiped them out.)[/color]

      Unsetting $_POST values has no effect whatever on the next submission,
      for much the same reason that drawing a beard on a photograph of
      you doesn't give you a beard. Consider $_POST as read-only, although
      you *CAN* alter it, it gets thrown away once your script is finished
      running.

      Gordon L. Burditt

      Comment

      • xclarky@gmail.com

        #4
        Re: Avoiding double-submissions with mail script

        Use sessions! You could either set a session as a static value and then
        check whether it exists, if so do not allow the script to run,
        basically a temporary (sessions, by default configuration, close on the
        closure of all browser instances) spam solution OR create a session key
        which contains the users last inputs, either the entire post array or
        individual fields. You can thefore compare the form input with the
        session data to determine if the data is duplicate. This concept will
        only work if the session still exists of course.

        Comment

        • Chuck W.

          #5
          Re: Avoiding double-submissions with mail script

          Cool. Do you have any thoughts on a rudimentary example of what that
          might look like?

          Thanks so much!

          Chuck

          Comment

          • Jim Dornbos

            #6
            Re: Avoiding double-submissions with mail script

            On Wed, 01 Feb 2006 20:27:06 -0000, gordonb.q95s8@b urditt.org (Gordon
            Burditt) wrote:[color=blue][color=green]
            >>quote from Jim
            >>From one newb to another... in addtion to mailing the message, I would
            >>unset a couple of (all?) your $_POST values. Then add some code prior
            >>to mailing to make sure they have values. Hitting submit once would
            >>send the message. Hitting submit a second time would throw up your
            >>error message about missing values (because you just wiped them out.)[/color]
            >
            >quote from Gordon
            >Unsetting $_POST values has no effect whatever on the next submission,
            >for much the same reason that drawing a beard on a photograph of
            >you doesn't give you a beard. Consider $_POST as read-only, although
            >you *CAN* alter it, it gets thrown away once your script is finished
            >running.[/color]

            Thanks for catching me on that, Gordon. I used unset() for a logout
            page... but just enough of the details were sticking with me to get me
            in trouble.

            Jim

            Comment

            • william.clarke@gmail.com

              #7
              Re: Avoiding double-submissions with mail script

              I've used something along the lines of:

              //I'll assume you know how to start a session, if not check the
              manual.
              //Call it whatever you like....it is just a flag to stop the message
              getting resent.
              $_SESSION("Mess age_Sent") = true;

              Then before mailing, check the value of this session variable.

              You could also uses Javascript to disable the button (but this can get
              fiddly if the user hits refresh, but using PHP to generate the
              javascript code means you can check the same Session variable and keep
              the button disabled if it's value is true.).

              Comment

              Working...