Allowing special characters in form submit [duplic

2019-09-05 05:34发布

This question already has an answer here:

So I'm coding a contact form for a website, but I want to be able to allow the user to enter non-latin based characters (i.e. é or 車) but I can't exactly figure out where I would set that up - once the user submits the form, it passes it through an AJAX section in jQuery which then processes that to PHP (which prevents the page from refreshing) but trying to use utf8_encode() didn't work and setting the contentType for the data in .ajax() didn't help either, so now I'm not sure what to do.

Anyone able to help me out?

HTML form

  <form action="" id="contactUs" method="post">
    <div class="input-group">
      <span class="input-group-addon">name</span>
      <input type="text" class="form-control" placeholder="Your Name/Company" tabindex="1" name= "name" id="name" />
    </div>
    <div class="input-group">
      <span class="input-group-addon" style="padding-right: 18px;">mail</span>
      <input type="email" class="form-control" placeholder="Your/Your Company Email" tabindex="2" name="email" id="email" />
    </div>
    <div class="input-group">
      <span class="input-group-addon" style="padding-right: 18px;">subject</span>
      <input type="text" class="form-control" placeholder="The subject of your message" tabindex="2" name="subject" id="subject" />
    </div>
    <div class="input-group">
      <span class="input-group-addon" style="padding-right: 18px;">message</span>
      <textarea class="form-control" placeholder="What's on your mind?" cols="100" rows="4" tabindex="3" name="msg" id="msg"></textarea>
    </div>
    <div class="input-group">
      <span class="input-group-btn">
        <button class="btn btn-default" type="submit" style="-webkit-border-radius: 8px; border-radius: 8px; position: relative; left: 41%;" id="submit">Submit</button>
      </span>
    </div>
  </form>

jQuery

    var dataString = 'name=' + name + '&email=' + email + "&subject=" + subject +  '&msg=' + msg;
    $.ajax ({  
            type: "POST",  
            url: "process.php",
            data: {
                name : name,
                email : email,
                subject :subject,
                msg : msg
            },  
            contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15",
            dataType: 'json',
            complete: function() {  
                $("#error").hide();
                $("#errormail").hide();
                $("textarea#msg").css("background-color", "#ffffff");
                $("input#email").css("background-color", "#ffffff");
                $("input#name").css("background-color", "#ffffff");
                $("#success").show();

                setTimeout(
                    function(){ $('#success').fadeOut() }, 5000
                );
                $( '#contactUs' ).each(function(){
                    this.reset();
                });
            }
    });

PHP

<?php

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

    $email_to = "info@infinitedream.net ";
    $email_from = $_POST['email']; // required
    $email_subject = $_POST['subject'];

    $name = $_POST['name']; // required
    $message = $_POST['msg']; // required

    $email_message = '<html><body>';

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= '<table rules="all" style="border: 1px solid #666;" cellpadding="10">';
    $email_message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . strip_tags($name) . "</td></tr>";
    $email_message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($email_from) . "</td></tr>";
    $email_message .= "<tr><td><strong>Message:</strong> </td><td>" . utf8_encode($message) . "</td></tr>";
    $email_message .= "</table>";
    $email_message .= "</body></html>";


// create email headers
$headers = 'From: '.$email_from."\r\n";
$headers = 'Reply-To: '.$email_from."\r\n";
$headers = 'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers = "Content-type:text/html;charset=iso-8859-1" . "\r\n";
mail($email_to, $email_subject, $email_message, $headers);  

}
?>

1条回答
The star\"
2楼-- · 2019-09-05 06:01

Do you have the charset meta?

<meta charset="utf-8">

Also try changing the document's encoding to UTF-8.

查看更多
登录 后发表回答