Advanced Search

Results 1 to 5 of 5

Thread: UTF8 on my contact form

  1. #1
    Join Date
    Feb 2006
    Location
    Sweden
    Posts
    27
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default UTF8 on my contact form

    Hi!

    I have been searching on the web for solutions but for some reason it seems that I can't implement them. I am a rookie in PHP and perhaps the mistake has been under my nose all this time but I did not manage to see it...

    Here goes my contact form:

    <form method="post" action="kontakt.php">

    <label>Namn *</label>
    <input name="name" placeholder="Type Here">

    <label>Telefon *</label>
    <input name="telef" placeholder="Type Here">

    <label>E-post *</label>
    <input name="email" type="email" placeholder="Type Here">

    <label>Meddelandet</label>
    <textarea name="message" placeholder="Type Here"></textarea>

    <label>*Hur mycket är 2+2? (Anti-spam)</label>
    <input name="human" placeholder="Type Here">

    <input id="submit" name="submit" type="submit" value="Submit">


    <?php
    $name = $_POST['name'];
    $telef = $_POST['telef'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'Lounge Terrasse';
    $to = 'kontakt@lfsolutions.se';
    $subject = 'Lounge Terrasse - Ny meddelande';
    $human = $_POST['human'];

    $headers = "MIME-Version: 1.0" . PHP_EOL;
    $headers .= "From: $from <$email> ". PHP_EOL;
    $headers .= "Content-type: text/html;charset=UTF-8 ". PHP_EOL;


    $body = "From: $name\n Telefon: $telef\n E-Mail: $email\n Message:\n $message";

    if ($_POST['submit']) {
    if ($name != '' && $email != '' && $telef != '') {
    if ($human == '4') {
    if (mail ($to, $subject, $body, $from)) {
    echo '<p>Your message has been sent!</p>';
    } else {
    echo '<p>Something went wrong, go back and try again!</p>';
    }
    } else if ($_POST['submit'] && $human != '4') {
    echo '<p>You answered the anti-spam question incorrectly!</p>';
    }
    } else {
    echo '<p>You need to fill in all required fields!!</p>';
    }
    }
    ?>

    </form>


    So... any suggestions in order to implement correctly the utf8, since the e-mails are still coming with weird characters?

    Thanks in advance!!

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,624
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    This will depend on two factors:

    1) the charset your webpage is being served with (check the HTTP headers; the <meta> is usually not sufficient on its own)

    2) the charset your email is being sent with (check the email headers).

    The form itself, strictly speaking, has nothing to do with it.
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  3. #3
    Join Date
    Feb 2006
    Location
    Sweden
    Posts
    27
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    Well, this is my header:

    Code:
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    And as you say, it does not help. As for the e-mail headers, isn't it what I have?

    Code:
    $headers = "MIME-Version: 1.0" . PHP_EOL;
    $headers .= "From: $from <$email> ". PHP_EOL;
    $headers .= "Content-type: text/html;charset=UTF-8 ". PHP_EOL;

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,624
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by jonybigude View Post
    Well, this is my header:
    Code:
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    check the actual HTTP header. If your server sends an HTTP header by default (virtually all do), browsers almost always take it as authoritative and ignore the <meta> element.
    Most web browsers allow you to view the actual headers sent by the server.

    It is best to set it in your Apache config (or .htaccess file), but you can also set it deliberately in your PHP script:
    PHP Code:
    header('Content-Type: text/html; charset=utf-8'); 
    Quote Originally Posted by jonybigude View Post
    And as you say, it does not help. As for the e-mail headers, isn't it what I have?
    Double-check that they're actually getting sent that way (most email clients will have a "view headers" option).
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  5. The Following User Says Thank You to traq For This Useful Post:

    jonybigude (06-20-2013)

  6. #5
    Join Date
    Feb 2006
    Location
    Sweden
    Posts
    27
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    In the end of the day I manage to solve it like this:

    Code:
    <?php
    					$name = $_POST['name'];
    					$telef = $_POST['telef'];
    					$email = $_POST['email'];
    					$message = $_POST['message'];
    					$from = 'Lounge Terrasse: Ny meddelande'; 
    					$to = 'kontakt@lfsolutions.se'; 
    					$subject = 'Ny meddelande';
    					$human = $_POST['human'];
    
    
    					$headers = "MIME-Version: 1.0" . PHP_EOL;
    					$headers .= "From: $from <$email> ". PHP_EOL;
    					$headers .= "Content-type: text/html;charset=UTF-8 ". PHP_EOL;
    
    					$name = str_replace( '[at]','@', $name);
    					$message = str_replace( '[at]','@', $message);
    
    					$body = "<strong>Från:</strong> $name<br> <strong>Telefon:</strong> $telef<br> <strong>E-post:</strong> $email<br><br> <strong>Meddelandet:</strong><br> $message";
    
    
    					if ($_POST['submit']) {
    						if ($name != '' && $message != '' && $telef != '') {
    							if ($human == '4') {                 
    								if (mail ($to, $subject, $body, $headers)) { 
    									echo '<p style="background:#ff0000; line-height:30px; vertical-align:middle; color:#FFF; font-weight:bold;">Ditt meddelande har skickats!</p>';
    								} else { 
    									echo '<p style="background:#ff0000; line-height:30px; vertical-align:middle; color:#FFF; font-weight:bold;">Någonting gick fel. Var vänlig och försök igen!</p>'; 
     						        } 
     							    } else if ($_POST['submit'] && $human != '4') {
     							        echo '<p style="background:#ff0000; line-height:30px; vertical-align:middle; color:#FFF; font-weight:bold;">Du har gett fel svar på Anti-Spam frågan!</p>';
    							    }
        							} else {
            							echo '<p style="background:#ff0000; line-height:30px; vertical-align:middle; color:#FFF; font-weight:bold;">Du behöver att fylla alla fält i formuläret!</p>';
        							}
    					}
    
    				?>
    Thanks for all the help traq! Much appreciated!

Similar Threads

  1. Contact form..how..?
    By JFlores in forum HTML
    Replies: 12
    Last Post: 05-08-2012, 05:49 PM
  2. Resolved utf8 read from file
    By ggalan in forum PHP
    Replies: 1
    Last Post: 02-25-2012, 07:51 PM
  3. Contact us form
    By goldenimage in forum Looking for such a script or service
    Replies: 4
    Last Post: 07-17-2010, 03:43 AM
  4. Help with contact form
    By gelu4 in forum ASP
    Replies: 3
    Last Post: 12-12-2006, 01:34 PM
  5. Contact Form
    By Johnnymushio in forum HTML
    Replies: 2
    Last Post: 08-25-2006, 05:20 PM

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
  •