SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 31
  1. #1
    SitePoint Zealot 810311's Avatar
    Join Date
    Mar 2010
    Location
    Canada
    Posts
    132
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    display success message on submit form

    Good day,

    I have a site hosted by a hosting company. I am using their form processing feature to process a contact form by inserting the following code into form's HTML.

    HTML Code:
    <input type="hidden" name="sendtoemail" value="webmaster@mywebsite.com">
    
    <input type="hidden" name="redirect" value="http://www.**redirect url**">
    I am wondering if that's possible ,using Javascript, to display a "Thank you. Your message has been sent" kind of message in the message field of the form, after user hits submit button.

    Here's the form's HTML

    HTML Code:
    <form action="http://www.host.com/hostmail" enctype="multipart/form-data" method="post" accept-charset="UTF-8" class="contact">
                                        
    <div>
    <input type="hidden" name="sendtoemail" value="test@test.com">
    <input type="hidden" name="redirect" value="http://test.com/contact.html">
    </div>
                                        
    		<fieldset>
                                            
                       <div class="label">
                               <label class="fixedwidth" for="name">Name</label>
                               <input id="name" name="name" type="text" size="50" value=""/>
                       </div>
                                            
    		   <div class="label">
                                <label class="fixedwidth" for="email">Email</label>
                                <input id="email" name="email" type="text" size="50" value=""/>
                       </div>
                                            
    		   <div class="label">
                                <label class="fixedwidth" for="subject">Subject</label>
                                <input id="subject" name="subject" type="text" size="50" value=""/>
                       </div>
                                            
    		  <div id="textarea">
                                 <label class="fixedwidth" for="message">Message</label><br/>
                                 <textarea id="message" name="message" rows="16" ></textarea>
                      </div>
                                            
    		  <div class="buttonarea">
    		  	<input type="submit" value="Send your message"/>
                      </div>
                                        
    </fieldset>
    </form>

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,892
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    It seems that you are able to specify the redirect url:

    Code:
    <input type="hidden" name="redirect" value="http://www.**redirect url**">
    So, why not redirect to a thank you message?
    Alternatively, use JS to display a "Message successfully submitted" kind of message on the redirect page, then have it vanish after a few seconds.
    I could help you with that if that seems like a good option.

    I would also have thought that your hosting company would have an option for such an eventuality.
    Who are they? Do you have links to any kind of documentation?

  3. #3
    SitePoint Zealot 810311's Avatar
    Join Date
    Mar 2010
    Location
    Canada
    Posts
    132
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi Pullo,

    Thanks for the quick response. I am not familiar with JS so would appreciate your help. If you can provide me with step-by-step instructions that would be great!

    The company is http://www.bluehost.com/ I am not sure they have documentation on this because they told me I would need to customize such message myself.

    Please, see attached doc file with some instructions on using the feature that I am using to process the form.

    https://drive.google.com/file/d/0B6n...it?usp=sharing

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,892
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    No probs

    From the docs you link to:

    If you do not specify a REDIRECT page then the default page that the user filling out the form would see would look like the page shown below.

    Thank you for completing our form.
    The following info was sent to webmaster@test.com:
    ...
    Isn't that exactly what you're after?

  5. #5
    SitePoint Zealot 810311's Avatar
    Join Date
    Mar 2010
    Location
    Canada
    Posts
    132
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    yes, only the text would be "Thank you. Your message has been sent" that's all. Can you show me how to do that?

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,892
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Then what about making a separate page with that message on and just redirecting to that?

    Or do you have another page you wish to redirect to?

  7. #7
    SitePoint Zealot 810311's Avatar
    Join Date
    Mar 2010
    Location
    Canada
    Posts
    132
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the tip, Pullo. I created a separate page and it gets redirected to that page with the message. The only thing is that a blue "redirect" link in the top left corner of the browser flashes for 1 second and then disappears after I hit submit button. Is it supposed to work that way with redirect feature or it just depends on the speed of my internet connection?

  8. #8
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,892
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Nice one

    Re. the the blue "redirect" link, could you post a link to the page in question so I can see it myself.
    It doesn't sound normal.

  9. #9
    SitePoint Zealot 810311's Avatar
    Join Date
    Mar 2010
    Location
    Canada
    Posts
    132
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    sorry what I meant is : after I hit submit button, the "redirect" link flashes once and then disappears. I will post a link.

  10. #10
    SitePoint Zealot 810311's Avatar
    Join Date
    Mar 2010
    Location
    Canada
    Posts
    132
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    here's the link http://prygara.com/contact.html

    I just spoke with my host support and they said that 'redirect' link flashes too fast and they are not able to actually catch what it says..., however, they said it's part of their 'redirect' feature that I use so it can't be fixed on their end as this is the way it works for now.

  11. #11
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,892
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Wow, that sucks.

    If you have PHP on your servers, why don't you just roll your own contact form?

    I could help you if you like.

  12. #12
    SitePoint Zealot 810311's Avatar
    Join Date
    Mar 2010
    Location
    Canada
    Posts
    132
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    sure. how do we do this? what info you need?

  13. #13
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,892
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Well, first off if you can upload PHP files to your webspace and execute them.

    Try making a file called info.php and pasting the following content into it:

    Code:
    <?php
      phpinfo();
    ?>
    Upload it to your web server and attempt to view the file.
    What happens?

  14. #14
    SitePoint Zealot 810311's Avatar
    Join Date
    Mar 2010
    Location
    Canada
    Posts
    132
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I did. I now have info.php on my server. It is showing file as PHP Script.

  15. #15
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,892
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Good.

    Now we'll need a contact form.

    I'm going to take this one and adapt it a little.

    Here's the code:

    Code:
    <!DOCTYPE HTML>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Contact Us!</title>
        <style>
          * {
            margin: 0;
            padding: 0;
          }
    
          body {
            font-size: 62.5%;
            font-family: Helvetica, sans-serif;
          }
    
          p {
            font-size: 1.3em;
            margin-bottom: 15px;
          }
    
          #page-wrap {
            width: 660px;
            background: white;
            padding: 20px 50px 20px 50px;
            margin: 20px auto;
            height: auto !important;
          }
    
          #contact-area {
            width: 600px;
            margin-top: 25px;
          }
    
          #contact-area input, #contact-area textarea {
            padding: 5px;
            width: 471px;
            font-family: Helvetica, sans-serif;
            font-size: 1.4em;
            margin: 0px 0px 10px 0px;
            border: 2px solid #ccc;
          }
    
          #contact-area textarea {
            height: 90px;
          }
    
          #contact-area textarea:focus, #contact-area input:focus {
            border: 2px solid #900;
          }
    
          #contact-area input.submit-button {
            width: 100px;
            float: right;
          }
    
          label {
            float: left;
            text-align: right;
            margin-right: 15px;
            width: 100px;
            padding-top: 5px;
            font-size: 1.4em;
          }  
        </style>
      </head>
      <body>
    
        <div id="page-wrap">
          <h1>Contact form</h1>
    
          <div id="contact-area">
            <form method="post" action="submit.php">
    
              <label for="Name">Name:</label>
              <input type="text" name="name" id="name" />
                
              <label for="Email">Email:</label>
              <input type="text" name="email" id="email" />
              
              <label for="Message">Message:</label><br />
              <textarea name="message" rows="20" cols="20" id="message"></textarea>
    
              <input type="submit" name="submit" value="Submit" class="submit-button" />
            </form>
          </div>
        </div>
      </body>
    </html>
    You'll see, that this is nothing more than a simple form, which gets submitted to a file called "submit.php"

    submit.php will then check the data it received. If validation fails, it'll show an error message, otherwise it'll show a success message.
    In our example, I'll make all of the fields mandatory and I'll validate the email address against a regular expression:

    Code:
    <?php
      $name = filter_var($_POST['name'],FILTER_SANITIZE_STRING);
      $subject = filter_var($_POST['subject'],FILTER_SANITIZE_STRING);
      $email = filter_var($_POST['email'],FILTER_SANITIZE_STRING, FILTER_VALIDATE_EMAIL);
      $message = filter_var($_POST['message'],FILTER_SANITIZE_STRING);
      $error = "";
    
      if (empty($name)){
        $error .= "You didn't enter a name <br />";
      }
    
      if (empty($subject)){
        $error .= "You didn't enter a subject <br />";
      }
      
      if (empty($email)){
        $error .= "You didn't enter an email address <br />";
      } elseif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})\.?$", $email)){
        $error .= "The email address appears to be invalid <br />";  
      }
      
      if (empty($message)){
        $error .= "You didn't enter a message <br />";
      }
    
      if (empty($error)) {
        echo "All fields filled out correctly!";
      } else {
        echo $error;
      }
    ?>
    Copy this code to your PC, name the files accordingly then upload them to your server.
    Then let me know if this all works or if you have any questions.

    After that we'll move on to the next step - sending the message.

  16. #16
    SitePoint Zealot 810311's Avatar
    Join Date
    Mar 2010
    Location
    Canada
    Posts
    132
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I adjusted my contact form and created submit.php. all uploaded now.

  17. #17
    SitePoint Zealot 810311's Avatar
    Join Date
    Mar 2010
    Location
    Canada
    Posts
    132
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    when I hit submit button now on my form, it get's me to submit.php saying "All fields filled out correctly!"

  18. #18
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,892
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Cool!

    Now let's make it send the mail:

    Code:
    <?php
      $name = filter_var($_POST['name'],FILTER_SANITIZE_STRING);
      $subject = filter_var($_POST['subject'],FILTER_SANITIZE_STRING);
      $email = filter_var($_POST['email'],FILTER_SANITIZE_STRING, FILTER_VALIDATE_EMAIL);
      $message = filter_var($_POST['message'],FILTER_SANITIZE_STRING);
      $error = "";
    
      if (empty($name)){
        $error .= "You didn't enter a name <br />";
      }
    
      if (empty($subject)){
        $error .= "You didn't enter a subject <br />";
      }
      
      if (empty($email)){
        $error .= "You didn't enter an email address <br />";
      } elseif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})\.?$", $email)){
        $error .= "The email address appears to be invalid <br />";  
      }
      
      if (empty($message)){
        $error .= "You didn't enter a message <br />";
      }
    
      if (empty($error)) {
        $toaddress = "youraddress@you.com";
        $fromaddress = "From:". $email;
        
        $mailcontent = "Name: ". $name. "\n".
                       "Subject: ". $subject. "\n".
                       "Email: ". $email. "\n\n".
                       $message;
                       
        mail($toaddress, "Website Contact Form", $mailcontent, $fromaddress);
        echo "Mail sent successfully!";
      } else {
        echo $error;
      }
    ?>
    Try that out and let me know if you get the mail as expected.

  19. #19
    SitePoint Zealot 810311's Avatar
    Join Date
    Mar 2010
    Location
    Canada
    Posts
    132
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Here's some issues I am seeing:

    1. "Your message has been sent" appears in a blank browser window after I hit submit. I wanted it appear within text area of the form - same way when I use my host 'redirect' feature.

    2. After I hit 'submit' it takes a long time to process.

    3. I am not receiving a test message that I sent to my host webmail.

    Please, take a look at form HTML and submit.php code below:

    HTML Code:
    <form action="submit.php" enctype="multipart/form-data" method="post" accept-charset="UTF-8" class="contact">
                                        
                                        <fieldset>
                                            
                                            <div class="label">
                                                <label class="fixedwidth" for="name">Name</label>
                                                <input id="name" name="name" type="text" size="50" value=""/>
                                            </div>
                                            <div class="label">
                                                <label class="fixedwidth" for="email">Email</label>
                                                <input id="email" name="email" type="text" size="50" value=""/>
                                            </div>
                                            <div class="label">
                                                <label class="fixedwidth" for="subject">Subject</label>
                                                <input id="subject" name="subject" type="text" size="50" value=""/>
                                            </div>
                                            <div id="textarea">
                                                <label class="fixedwidth" for="message">Message</label><br/>
                                                <textarea id="message" name="message" rows="16" cols="" ></textarea>
                                            </div>
                                            <div class="buttonarea">
    							  <input type="submit" value="Send your message"/>
                                            </div>
                                        </fieldset>
                                    </form>
    PHP Code:
    <?php
      $name 
    filter_var($_POST['name'],FILTER_SANITIZE_STRING);
      
    $subject filter_var($_POST['subject'],FILTER_SANITIZE_STRING);
      
    $email filter_var($_POST['email'],FILTER_SANITIZE_STRINGFILTER_VALIDATE_EMAIL);
      
    $message filter_var($_POST['message'],FILTER_SANITIZE_STRING);
      
    $error "";

      if (empty(
    $name)){
        
    $error .= "You didn't enter a name <br />";
      }

      if (empty(
    $subject)){
        
    $error .= "You didn't enter a subject <br />";
      }
      
      if (empty(
    $email)){
        
    $error .= "You didn't enter an email address <br />";
      } elseif (!
    eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})\.?$"$email)){
        
    $error .= "The email address appears to be invalid <br />";  
      }
      
      if (empty(
    $message)){
        
    $error .= "You didn't enter a message <br />";
      }

      if (empty(
    $error)) {
        
    $toaddress "....";
        
    $fromaddress "From:"$email;
        
        
    $mailcontent "Name: "$name"\n".
                       
    "Subject: "$subject"\n".
                       
    "Email: "$email"\n\n".
                       
    $message;
                       
        
    mail($toaddress"Website Contact Form"$mailcontent$fromaddress);
        echo 
    "Your message has been sent";
      } else {
        echo 
    $error;
      }
    ?>
    Last edited by Pullo; Feb 9, 2014 at 01:12. Reason: Removed email address

  20. #20
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,892
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    Quote Originally Posted by 810311 View Post
    1. "Your message has been sent" appears in a blank browser window after I hit submit. I wanted it appear within text area of the form - same way when I use my host 'redirect' feature.
    Building a contact form, although not overly complicated, does involve a number of steps and moving parts (so to speak). It is my intention to get the form working first of all, then customize it in a second step. Don't worry

    Quote Originally Posted by 810311 View Post
    2. After I hit 'submit' it takes a long time to process.
    3. I am not receiving a test message that I sent to my host webmail.
    This sounds like your provider has blocked PHP's mail function.
    The reason might be that the mail function is sometimes seen as a security risk, as a badly coded contact form can be used as a spam relay.

    The code you posted looks fine.

    Would it be possible for you to contact your hosting company and ask if they allow you to build your own contact forms and more specifically if PHP's mail function is specifically blocked on their servers?

  21. #21
    SitePoint Zealot 810311's Avatar
    Join Date
    Mar 2010
    Location
    Canada
    Posts
    132
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Sure. I understand. I will ask my host. Will be back soon. Thanks Pullo.

  22. #22
    SitePoint Zealot 810311's Avatar
    Join Date
    Mar 2010
    Location
    Canada
    Posts
    132
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    just got off the chat with my host, they confirmed I can have my own contact form and that PHP mail function is working on their end. They referred me to this article on how to use it : http://www.karavadra.net/blog/php-ma...ehost-working/

  23. #23
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,892
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Nice one!

    Let's take that code one for one and see if it works.

    submit.php

    Code:
    <?php 
      $emailto = 'to@domain.com';
      $toname = 'TO NAME';
      $emailfrom = 'from@domain.com';
      $fromname = 'FROM NAME';
      $subject = 'Email Subject';
      $messagebody = 'Hello.';
      $headers = 
        'Return-Path: ' . $emailfrom . "\r\n" . 
        'From: ' . $fromname . ' <' . $emailfrom . '>' . "\r\n" . 
        'X-Priority: 3' . "\r\n" . 
        'X-Mailer: PHP ' . phpversion() .  "\r\n" . 
        'Reply-To: ' . $fromname . ' <' . $emailfrom . '>' . "\r\n" .
        'MIME-Version: 1.0' . "\r\n" . 
        'Content-Transfer-Encoding: 8bit' . "\r\n" . 
        'Content-Type: text/plain; charset=UTF-8' . "\r\n";
      $params = '-f ' . $emailfrom;
      $success = mail($emailto, $subject, $message, $headers, $params);
    
      if ($success){
        echo "Mail sent successfully";
      }
      else{
        echo "Something went wrong";
      }
    ?>
    Don't forget to change the $emailto variable to your email address.
    Also, it is not a good idea to post your address in plain text in the forum - this is a good way of getting spam.
    I edited it out of your last post

    Now there are no checks as to whether the input was correct, rather when you submit the form, you should get an email with the text "Hello."

    Sorry if it seems a bit frustrating to take such small steps, but we need to get a working base, which we can then expand from.
    I'm in Europe, so if you can reply by this evening, I'll be able to write more.

  24. #24
    SitePoint Zealot 810311's Avatar
    Join Date
    Mar 2010
    Location
    Canada
    Posts
    132
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Also, it is not a good idea to post your address in plain text in the forum - this is a good way of getting spam.
    I edited it out of your last post
    Thanks for catching this for me

    Quote Originally Posted by Pullo View Post
    ..... rather when you submit the form, you should get an email with the text "Hello."
    After I submit the form, I get the email with empty text field...there's no word "Hello"..


    Quote Originally Posted by Pullo View Post
    Sorry if it seems a bit frustrating to take such small steps, but we need to get a working base, which we can then expand from.
    I'm in Europe, so if you can reply by this evening, I'll be able to write more.
    No worries. I understand the time difference

  25. #25
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,892
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    Quote Originally Posted by 810311 View Post
    After I submit the form, I get the email with empty text field...there's no word "Hello"..
    On closer inspection there is a typo in the article you link to.
    No matter, we now know that we can send mail via your host's server

    Let's try and incorporate all of that in our code so far then:

    Code:
    <?php
    $emailto = 'to@domain.com';
    $toname = 'TO NAME';
    
    $fromname = filter_var($_POST['name'],FILTER_SANITIZE_STRING);
    $emailfrom = filter_var($_POST['email'],FILTER_SANITIZE_STRING, FILTER_VALIDATE_EMAIL);
    $subject = filter_var($_POST['subject'],FILTER_SANITIZE_STRING);
    
    $message = filter_var($_POST['message'],FILTER_SANITIZE_STRING);
    $error = "";
    
    $headers = 
      'Return-Path: ' . $emailfrom . "\r\n" . 
      'From: ' . $fromname . ' <' . $emailfrom . '>' . "\r\n" . 
      'X-Priority: 3' . "\r\n" . 
      'X-Mailer: PHP ' . phpversion() .  "\r\n" . 
      'Reply-To: ' . $fromname . ' <' . $emailfrom . '>' . "\r\n" .
      'MIME-Version: 1.0' . "\r\n" . 
      'Content-Transfer-Encoding: 8bit' . "\r\n" . 
      'Content-Type: text/plain; charset=UTF-8' . "\r\n";
    $params = '-f ' . $emailfrom;
    
    if (empty($fromname)){
      $error .= "You didn't enter a name <br />";
    }
    
    if (empty($subject)){
      $error .= "You didn't enter a subject <br />";
    }
    
    if (empty($emailfrom)){
      $error .= "You didn't enter an email address <br />";
    } elseif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})\.?$", $emailfrom)){
      $error .= "The email address appears to be invalid <br />";  
    }
    
    if (empty($message)){
      $error .= "You didn't enter a message <br />";
    }
    
    if (empty($error)) {
      $mailcontent = "Name: ". $name. "\n".
                     "Subject: ". $subject. "\n".
                     "Email: ". $email. "\n\n".
                     $message;
                     
      $success = mail($emailto, $subject, $mailcontent, $headers, $params);
    
      if ($success){
        echo "Mail sent successfully";
      }
      else{
        echo "Something went wrong";
      }
    } else {
      echo $error;
    }
    ?>
    With this code, the script validates the fields as before, then attempts to send the mail.
    If it can send the mail, then it echos "Mail sent successfully" to the screen.

    Have a play with that and let me know if it works.


Bookmarks

Posting Permissions

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