www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: E-mail submission page - re-sends data when page reloads/refreshes.

  1. #1
    Join Date
    May 2005
    Posts
    157

    Question E-mail submission page - re-sends data when page reloads/refreshes.

    Ok, well ive posted quite a lot of code (excluding the html which is unnessisary for my problem), but this code is for my home page which has a mailing list submission form, which works fine.... nearly.

    It all works apart from when you submit a valid e-mail address and then refresh or re-visit the page, where it posts the e-mail adress you submitted again as the page is loading.

    - perhaps the form/page needs resetting somehow...??

    Its a bit of a challenge but could anyone solve the problem. Any help will be much appriciated, thanks.


    <?php

    $value = 'Your E-mail address';

    if(isset($_POST['B1'])){

    $value = $_POST['Email'];

    // get posted data into local variables
    $EmailTo = "mailinglist@theparoles.co.uk";
    $Subject = "Mailing list";
    $Email = Trim(stripslashes($_POST['Email']));

    // prepare email body text
    $Body = "";
    $Body .= "Email: ";
    $Body .= $Email;
    $Body .= "\n";


    // send email
    $CheckEmail = trim($_POST['Email']);
    $regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,6})$";
    if (eregi($regexp, $CheckEmail)){
    $success = @mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
    }

    }

    echo <<< HEAD




    <html>
    <head>
    </head>

    <body>

    //my web pgae html code goes here


    HEAD;

    if($success){

    echo "<script language=\"javascript\">\n";
    echo "<!--\n";
    echo "alert(\"Thank you, your request has been sent.\");";
    echo "\n\/\/-->";
    echo "</script>";


    print <<< FORM

    <form method="POST" action="http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}">
    <p style="margin-top: 1; margin-bottom: 1"><input type="text" name="Email" size="15" style="color: #000000; font-family: Arial; font-size: 11px" value="$value">&nbsp;

    <input type="submit" value="Join" name="B1" style="color: #000000; font-family: Arial; font-size: 11px"></p>
    </form>

    FORM;
    }


    elseif(isset($_POST['B1']) && $success == FALSE){

    echo "<script language=\"javascript\">\n";
    echo "<!--\n";
    echo "alert(\"Message sending failed! Please check your details and try again!\");";
    echo "\n\/\/-->";
    echo "</script>";

    }

    if(!isset($_POST['B1']) | $success == FALSE){

    echo <<< FORM


    <form method="POST" action="http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}">
    <p style="margin-top: 1; margin-bottom: 1"><input type="text" name="Email" size="15" style="color: #000000; font-family: Arial; font-size: 11px" value="$value">&nbsp;

    <input type="submit" value="Join" name="B1" style="color: #000000; font-family: Arial; font-size: 11px"></p>
    </form>

    FORM;
    }



    echo <<< FOOT


    </body>

    </html>

    FOOT;
    ?>
    Garden Ballistics.co.uk - Home made cannons, pyrotechnics and amazing ballistic inventions!
    Garden Ballistics Forum

  2. #2
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    That is not a problem with the script. Rather it is your browser storing the data. Maybe setting a cookie will stop it. Try the following:
    PHP Code:
    <?php

    $value 
    'Your E-mail address';

    if(isset(
    $_POST['B1'])){

    $value $_POST['Email'];
        
    // get posted data into local variables
    $EmailTo "mailinglist@theparoles.co.uk";
    $Subject "Mailing list";
    $Email Trim(stripslashes($_POST['Email']));

    // prepare email body text
    $Body "";
    $Body .= "Email: ";
    $Body .= $Email;
    $Body .= "\n";


    // send email
    $CheckEmail trim($_POST['Email']);
    $regexp "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,6})$";
    if (
    eregi($regexp$CheckEmail) && !isset($_COOKIE['email'])){
        
    $success = @mail($EmailTo$Subject$Body"From: <{$_POST['Email']}>");
        if(
    $success){
            
    setcookie("email"'1');
        }
    }

    }

    echo <<< HEAD

    <html>

    <head>
    </head>

    <body>

    HEAD;

    if(
    $success){

    print 
    'Your message was sent';

    }elseif(isset(
    $_POST['B1']) && $success == FALSE && !isset($_COOKIE['email'])){

    print 
    'Message sending failed! Please check your details and try again!<br><br>';

    }elseif(isset(
    $_COOKIE['email'])){
        print 
    'Your email address has already been submitted';
    }

    if(!isset(
    $_COOKIE['email'])){
        if(!isset(
    $_POST['B1']) | $success == FALSE){

    echo <<< FORM


    <form method="POST" action="http://
    {$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}">
    <p style="margin-top: 1; margin-bottom: 1"><input type="text" name="Email" size="15" style="color: #000000; font-family: Arial; font-size: 11px" value="
    $value">&nbsp;

    <input type="submit" value="Join" name="B1" style="color: #000000; font-family: Arial; font-size: 11px"></p>
    </form>

    FORM;

        }
    }



    echo <<< FOOT

    </body>

    </html>

    FOOT;
    ?>
    This time try to copy the code without breaking it.

  3. #3
    Join Date
    May 2005
    Posts
    157
    Okay thanks, but i want to change a couple of things:

    Firstly, i want to change the print comands to the following:
    (the message will be changed respectivly)

    echo "<script language=\"javascript\">\n";
    echo "<!--\n";
    echo "alert(\"Thank you, your request has been sent.\");";
    echo "\n\/\/-->";
    echo "</script>";


    Secondly i would prefer to not use the "your email has been sent already" feature.

    I would and have tried to do it myself, but i think this level of scripting is too advanced for me as i keep getting errors

    The help is much appriciated, thanks.
    Garden Ballistics.co.uk - Home made cannons, pyrotechnics and amazing ballistic inventions!
    Garden Ballistics Forum

  4. #4
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    <script language="javascript"> is invalid. It should be: <script type="text/javascript">. Personally I hate the javascript alert idea. And secondly let me assure you this is very basic code and that I am a complete beginner.
    Last edited by bokeh; 07-27-2005 at 02:56 AM.

  5. #5
    Join Date
    May 2005
    Posts
    157
    can i just ask what program you use to edit php because im just using notepad which makes it difficult.
    Garden Ballistics.co.uk - Home made cannons, pyrotechnics and amazing ballistic inventions!
    Garden Ballistics Forum

  6. #6
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    There are a couple of free ones. Crimson Editor and ConText. I like Crimson best. It highlights your code and helps with brackets. When an opening bracket is focused it underlines the closing bracket.

  7. #7
    Join Date
    May 2005
    Posts
    157
    i don't want to keep demanding things of you but i want the form to always 'be there' and you can submit the same e-mail as many times as someone wants and, i like the alerts.

    The thing that needs changing is the fact that the form is sent when the code is run (i.e. when the page loads).

    Thanks again
    Garden Ballistics.co.uk - Home made cannons, pyrotechnics and amazing ballistic inventions!
    Garden Ballistics Forum

  8. #8
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    You are not making any sense! You keep asking for different things. You said: 'It all works apart from when you submit a valid e-mail address and then refresh or re-visit the page where it posts the e-mail adress you submitted again as the page is loading'. Now you are saying you want that to happen. And why would you want to display the form once the client has done the submission?

  9. #9
    Join Date
    May 2005
    Posts
    157
    sorry im not making sense, this is a page using the code i first submitted.

    http://www.theparoles.co.uk/testing.php

    It works how i want it to apart from when you refresh the page, where it re-submitts the e-mail address.

    If you visit another page and come back it works fine untill it is refreshed again.

    I need some way of sopping the browser from storing the data submitted and reusing it when the page is refreshed.
    Garden Ballistics.co.uk - Home made cannons, pyrotechnics and amazing ballistic inventions!
    Garden Ballistics Forum

  10. #10
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    What is the point of being able to submit the same email numerous times? And why do you want the form to still be displayed after the submission has been done?
    Last edited by bokeh; 07-27-2005 at 07:54 AM.

  11. #11
    Join Date
    May 2005
    Posts
    157
    Well i don't nessiseraly want the same person to be able to sign up using the same e-mail address, its just that if more than one person wanted to use the same browser they would not be able to sign up as the other person already has done so (unless they revisit the site, clearing the stored email data)

    I want the form to be displayed all the time as it is part of the website layout.

    It might be a better idea for the code to write to a txt file creating a list of e-mail addreses when a subscribe radio button is selected. Then if someone signs up with the same email it could cheack the txt file for the same email, also it could delete email address when the person selects a radio button unsubscribe. - might be to hard, i duno

    thanks
    Garden Ballistics.co.uk - Home made cannons, pyrotechnics and amazing ballistic inventions!
    Garden Ballistics Forum

  12. #12
    Join Date
    Jan 2005
    Location
    Lithia Springs, GA USA
    Posts
    886
    Quote Originally Posted by bokeh
    And secondly let me assure you ... that I am a complete beginner.
    Ya right LOL aren't we all

    As for your form thing. As Bokeh said that is a browser issue. The browser is storing the info.

    You could store that info in a txt file, but I believe that if you have access to a database that would be much much much better! txt files can get unwieldly after awhile.

    You could also think about setting a session variable so that when the form is submitted it processes it then destroy the session variable upon a successful submission so that when someone refreshes the page or goes back they will only see the original form. This will not stop them from refilling out the form and submitting it though.

  13. #13
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Another way might be set a cookie containing the last posted email. That way youu could say
    PHP Code:
    if(!$_COOKIE['email'] == $_POST['email']{
    //send mail
    }
    // set cookie 

  14. #14
    Join Date
    May 2005
    Posts
    157
    Well i have just tried adding echo "history.back()"; tothe javascript sections and it seems to have worked.
    This is how the code looks withe the added code:
    (this is the webpage with the form on: www.theparoles.co.uk/index2.php)


    if($success){

    echo "<script language=\"javascript\">\n";
    echo "<!--\n";
    echo "alert(\"Thank you for singing up to our mailing list.\");";
    echo "history.back()";
    echo "\n\/\/-->";
    echo "</script>";


    }elseif(isset($_POST['B1']) && $success == FALSE){

    echo "<script language=\"javascript\">\n";
    echo "<!--\n";
    echo "alert(\"You did not enter a valid E-mail address, please try again.\");";
    echo "history.back()";
    echo "\n\/\/-->";
    echo "</script>";

    }


    Thanks for all your help.
    Garden Ballistics.co.uk - Home made cannons, pyrotechnics and amazing ballistic inventions!
    Garden Ballistics Forum

  15. #15
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    That still allows the post data to be sent multiple times. Yes, the form dissapears but that doesn't stop the form dat a being re sent.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles