Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By sarahswansea

Thread: Can't figure out how to get the sender's mail address showing

  1. #1
    Junior Member
    Join Date
    Nov 2015
    Posts
    5
    Member #
    52419

    Remove content from contactform after successful submit

    I'm working on a contactform which is working pretty okay at the moment. Now I have set the form to keep the values that were entered when there's an error so people don't have to fill in everything anew, however as soon as there is a successful submit, the boxes still keep their values.
    I was wondering...is there a way to keep the entered data on error, but as soon as the form is submitted it will remove the data?

    Below is the code

    HTML Code:
    <body>
    <div class="two-thirds column">
    				<p>Om contact op te nemen, kunt u de informatie aan de linkerkant gebruiken of het formulier 
    					hieronder invullen om een mail te sturen. Alle velden zijn verplicht.</p>
    				<div><?php echo $result; ?>	</div>
    				<form method="post" action="contact2.php">
    					<label>Naam:</label>
    					<input name="naam" placeholder="Typ hier" value="<?php echo htmlspecialchars($_POST['naam']); ?>">
    					<?php echo "<p class='text-danger'>$errName</p>";?>
    					<label>E-Mail:</label>
    					<input name="email" placeholder="Typ hier" value="<?php echo htmlspecialchars($_POST['email']); ?>">
    					<?php echo "<p class='text-danger'>$errEmail</p>";?>
    					<label>Onderwerp:</label>
    					<input name="onderwerp" placeholder="Typ hier">
    					<label>Bericht:</label>
    					<textarea name="bericht" placeholder="Typ hier"><?php echo htmlspecialchars($_POST['bericht']);?></textarea>
    					<?php echo "<p class='text-danger'>$errMessage</p>";?>
    					<input id="submit" name="submit" type="submit" value="Verzenden">
    				</form>
    			</div>
    
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    </body>
    PHP Code:
    <?php
        
    if (isset($_POST["submit"])) {
            
    $name $_POST['naam'];
            
    $email $_POST['email'];
            
    $onderwerp $_POST['onderwerp'];
            
    $message $_POST['bericht'];
            
    $to 'myemail@myemail.com'//DIT MOET NOG AANGEPAST!!!!!
            
    $subject 'Onderwerp: '$onderwerp;
            
            
    $body ="Afzender: $name\n E-Mail: $email\n Onderwerp: $onderwerp\n\n Message:\n $message";
            
            
    $headers 'From: ' $email;

            
    // Check if name has been entered
            
    if (!$_POST['naam']) {
                
    $errName 'Vul uw naam in';
            }
            
            
    // Check if email has been entered and is valid
            
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
                
    $errEmail 'U heeft geen (geldig) E-mail adres ingevuld';
            }
            
            
    //Check if message has been entered
            
    if (!$_POST['bericht']) {
                
    $errMessage 'Vul een bericht in';
            }
            
    // If there are no errors, send the email
    if (!$errName && !$errEmail && !$errMessage) {
        if (
    mail ($to$subject$body$headers  "MIME-Version: 1.0\r\n" "Content-Type: text/plain; charset=utf-8")) {
            
    $result='<div class="alert alert-success">Uw bericht is verzonden.</div>';
        } else {
            
    $result='<div class="alert alert-danger">Er is een fout opgetreden. Probeer het nogmaals.</div>';
        }
    }
        }
    ?>
    Last edited by Juzbeingme; Nov 07th, 2015 at 07:03 AM. Reason: Got the form working, but stuck on one more thing...

  2.  

  3. #2
    Senior Member sarahswansea's Avatar
    Join Date
    Oct 2015
    Location
    Swansea, UK
    Posts
    154
    Member #
    52184
    Liked
    64 times
    Simplest way, in the PHP code where you send the e-mail (with no errors), then you can remove the posted variables:

    unset($_POST['naam']);
    unset($_POST['email']);
    unset($_POST['bericht']);
    brandMatt likes this.

  4. #3
    Senior Member brandMatt's Avatar
    Join Date
    Oct 2015
    Location
    St .Catharines Ontario
    Posts
    240
    Member #
    52164
    Liked
    51 times
    I don't just unset the variables after a form submission, I redirect the user back to the form or the sites homepage, after a successful submission. Here is a sample of the code I use.

    echo "<script>setTimeout(function(){ window.location.href ='http://link.url'}, 2000);</script>";
    Last edited by brandMatt; Nov 07th, 2015 at 09:29 AM.

  5. #4
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,693
    Member #
    5580
    Liked
    717 times
    Use an AJAX form that validates errors without leaving the page.

    Some found using google search:
    35 Best PHP AJAX Contact Forms


  6. #5
    Junior Member
    Join Date
    Nov 2015
    Posts
    5
    Member #
    52419
    Quote Originally Posted by brandMatt View Post
    echo "<script>setTimeout(function(){ window.location.href ='http://link.url'}, 2000);</script>";
    I'm kindof a PHP noob so I'm not really sure where to implement it in the code, but would that mean that instead of giving the users the div with the success message i.e. " $result='<div class="alert alert-success">Uw bericht is verzonden.</div>';" <-- that part, I insert the code you proposed and have them redirected to a page that shows a success message? Also in your code I'm wondering what the 2000 is for...

    Oh one more question, I'm also using the same form setup for a subscription, so there's a possibility people will have to fill in the form more than once (multiple participants for example). Would there be a way to give the subscriber some kind of popup message with a "your subscription was successful" text and an OK button, so when they click the OK button the form would be reset?
    Last edited by Juzbeingme; Nov 16th, 2015 at 02:32 AM.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 04:39 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com