允许表单中的特殊字符提交 [英] Allowing special characters in form submit

查看:148
本文介绍了允许表单中的特殊字符提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在编写一个网站的联系表格,但我希望能够允许用户输入非拉丁字符(即é或车),但我不能准确地找出我要设置的位置在用户提交表单后,它将它传递给jQuery中的AJAX部分,然后将其处理为PHP(这样可以防止页面刷新),但尝试使用utf8_encode()无法正常工作,并且无法设置contentType .ajax()中的数据也没有帮助,所以现在我不知道该怎么做。



任何能够帮助我的人?​​



HTML表单

 < form action = id =contactUsmethod =post> 
< div class =input-group>
< span class =input-group-addon>名称< / span>
< input type =textclass =form-controlplaceholder =Your Name / Companytabindex =1name =nameid =name/>
< / div>
< div class =input-group>
< span class =input-group-addonstyle =padding-right:18px;> mail< / span>
< input type =emailclass =form-controlplaceholder =Your / Your Company Emailtabindex =2name =emailid =email/>
< / div>
< div class =input-group>
< span class =input-group-addonstyle =padding-right:18px;> subject< / span>
< input type =textclass =form-controlplaceholder =您的信息主题tabindex =2name =subjectid =subject/>
< / div>
< div class =input-group>
< span class =input-group-addonstyle =padding-right:18px;> message< / span>
< textarea class =form-controlplaceholder =你在想什么? cols =100rows =4tabindex =3name =msgid =msg>< / textarea>
< / div>
< div class =input-group>
< span class =input-group-btn>
< button class =btn btn-defaulttype =submitstyle = - webkit-border-radius:8px; border-radius:8px; position:relative; left:41%; ID = 提交 >提交< /按钮>
< / 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)。
$ b setTimeout(
function(){$('#success')。fadeOut()},5000
);
$('#contactUs')。每个(function(){
this.reset();
});
}
});

PHP
< pre $ <?php

if(isset($ _ POST ['email'])){

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

$ name = $ _POST ['name']; //必需
$ message = $ _POST ['msg']; //必需

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

函数clean_string($ string){
$ bad = array(content-type,bcc:,to:,cc:,href);
返回str_replace($ bad,,$ string);
}

$ email_message。='< table rules =allstyle =border:1px solid#666; CELLPADDING = 10 >; < td>< tt>名称:< 。 strip_tags($ name)。 < / TD>< / TR> 中;
$ email_message。=< tr>< td>< strong>电子邮件:< / strong>< / td>< td> 。 strip_tags($ email_from)。 < / TD>< / TR> 中;
$ email_message。=< tr>< td>< strong>讯息:< / strong>< / td>< td> 。 utf8_encode($ message)。 < / TD>< / TR> 中;
$ email_message。=< / table>;
$ email_message。=< / body>< / html>;


//创建电子邮件标题
$ headers ='From:'。$ email_from。\r\\\
;
$ headers ='Reply-To:'。$ email_from。\r\\\
;
$ headers ='X-Mailer:PHP /'。 phpversion();
$ headers。=MIME-Version:1.0。 \r\\\
;
$ headers =Content-type:text / html; charset = iso-8859-1。 \r\\\
;
mail($ email_to,$ email_subject,$ email_message,$ headers);

}
?>


解决方案

你有charset meta吗?

 < meta charset =utf-8> 

还可以尝试将文档的编码更改为UTF-8。

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);  

}
?>

解决方案

Do you have the charset meta?

<meta charset="utf-8">

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

这篇关于允许表单中的特殊字符提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆