Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    60
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Preventing submission refresh

    I have a small form on my index.php page. It is working great with one exception, if the user refreshes the page it submits the form again.

    Heres the code I am using.

    Head:
    Code:
    <?php 
    error_reporting(E_ALL ^ E_NOTICE); // hide all basic notices from PHP
    $emailSent = isset($_POST['isSent'])?$_POST['isSent']:FALSE;
    //If the form is submitted
    if(isset($_POST['submit']) && !$emailSent) {
        
    // we need at least some content
        if(trim($_POST['comments']) === '') {
            $commentError = 'You forgot to enter a message!';
            $hasError = true;
        } else {
            if(function_exists('stripslashes')) {
                $comments = stripslashes(trim($_POST['comments']));
            } else {
                $comments = trim($_POST['comments']);
            }
        }
            
        // upon no failure errors let's email now!
        if(!isset($hasError)) {
            
            $emailTo = 'utopianwd@gmail.com';
            $replyEmail = 'reply@email.com';
            $subject = 'Submitted message on Site.ca';
            //$sendCopy = trim($_POST['sendCopy']);
            $body = "Comments: $comments";
            $headers = 'From: ' .' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $replyEmail;
    
            mail($emailTo, $subject, $body, $headers);
            
            // set our boolean completion value to TRUE
            $emailSent = true;
        }
    }
    ?>
    Body:
    Code:
    <div id="actform">
    <h3><img src="./_layout/images/bg-checklist.png" />Got an idea?   Let us know below</h3>
    <form action="index.php" method="post">
    <input type="hidden" name="isSent" value="<?php echo ($emailSent)?1:0; ?>" />
    <textarea name="comments" class="actform" placeholder="Message:"></textarea>
    <input type="submit" value="Submit" name="submit" class="subname" />
    </form>
    <div class="spacer"></div>
    </div>
    What am I doing wrong?

    Thanks,
    Geoserv.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,316
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Record the form submission in the session.

    Put a unique random number in a hidden form element and when the form is submitted check for it in the session.

    If its not there, continue processing and then put it in the session. If it is there, do not process the form.

    Also please don't check for the submit button in you code - see my signature.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    New Coder
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    60
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks.

    Any idea on how to implement the items you suggested? Especially the IE issue?

  • #4
    New Coder
    Join Date
    Feb 2012
    Posts
    40
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Use unset:

    PHP Code:
    <?php 
    error_reporting
    (E_ALL E_NOTICE); // hide all basic notices from PHP
    $emailSent = isset($_POST['isSent'])?$_POST['isSent']:FALSE;
    //If the form is submitted
    if(isset($_POST['submit']) && !$emailSent) {
        
    // we need at least some content
        
    if(trim($_POST['comments']) === '') {
            
    $commentError 'You forgot to enter a message!';
            
    $hasError true;
        } else {
            if(
    function_exists('stripslashes')) {
                
    $comments stripslashes(trim($_POST['comments']));
            } else {
                
    $comments trim($_POST['comments']);
            }
        }
            
        
    // upon no failure errors let's email now!
        
    if(!isset($hasError)) {
            
            
    $emailTo 'utopianwd@gmail.com';
            
    $replyEmail 'reply@email.com';
            
    $subject 'Submitted message on Site.ca';
            
    //$sendCopy = trim($_POST['sendCopy']);
            
    $body "Comments: $comments";
            
    $headers 'From: ' .' <'.$emailTo.'>' "\r\n" 'Reply-To: ' $replyEmail;

            
    mail($emailTo$subject$body$headers);
            
            
    // set our boolean completion value to TRUE
            
    $emailSent true;
        }
           unset(
    $_POST);
    }

    ?>

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,316
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by cercos View Post
    Use unset:
    That will do nothing. As soon as a user hits the refresh button on their browser, the $_POST array is filled with the same data again. unsetting it at the end of the first instance of the script will not unset it for future instances.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,316
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by geoserv View Post
    Any idea on how to implement the items you suggested?
    Yes, as mentioned in my previous reply

    Put a unique number (from uniqid() ) into your form inside a hidden element.

    When the form is submitted, look for that value in the session (the session is $_SESSION array - call session_start() at the top of the script to use it).

    If it is not there, continue to process the form. If it IS there then do not process the form.

    Quote Originally Posted by geoserv View Post
    Especially the IE issue?
    All in my signature. Click the "IE isset bug explained" link.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •