php - Jquery使用utf-8提交表单

标签 php jquery html mysql ajax

我需要表单提交方面的帮助。

这是我的代码

<script type="text/javascript" src="js/jquery-1.7.2.js"></script>

    <script type="text/javascript">  
  $(document).ready(function(){  
    $("#form").submit(function(event) {  

        /* stop form from submitting normally */  
        event.preventDefault();   

        $.post( \'clase/app/add_meniu.php\', $("#form").serialize(),  
          function( data ) {  
              $("#status").append(data);  
          }  
        );  
      });  
  });  
</script>  

这是 html

<form id="form" method="post" accept-charset="UTF-8" >
         <fieldset>
<p>
        <label>Denumire</label>
        <input class="text-input small-input" type="text" name="text"   maxlength="200" value="">
    </p>
<p>
        <label>Link</label>
        <input class="text-input small-input" type="text" name="link"  maxlength="200" value="">
    </p>

这是add_meniu.php代码

   require_once('../core/db.php');
$text=$_POST['text'];
    $link=$_POST['link'];
    $limba=$_POST['limba'];
echo'<div class="notification success png_bg">
                <a href="#" class="close"><img src="resources/images/icons/cross_grey_small.png" title="Close this notification" alt="close" /></a>
                <div>
                    Meniul '.$text.' a fost adaugat cu succes
                </div>
            </div>';

$query = "Insert into Meniu (Denumire,Link,ID_Limba)  VALUES ('".$text."','".$link."','".$limba."')";
$result = mysql_query($query)
or die("query failed: " . mysql_error());

问题是,在数据库中,当我从外部文件“add_meniu.php”插入信息时,它不会使用 UTF-8 插入它。我有我的页面、表单和mysql表utf-8,你能给我一个解决方案吗?

最佳答案

首先,强烈建议您检查您的PHP文件是否为无BOM的UTF-8格式,您可以使用Notepad++轻松查看这一点。并在必要时进行转换。

我建议检查的其他要点:

HTML

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

PHP

ini_set( 'default_charset', 'UTF-8' );
mb_internal_encoding('UTF-8');

MySQL

mysql_query("SET NAMES 'utf8'");

关于php - Jquery使用utf-8提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13139893/

相关文章:

javascript - 通过Jquery更改表列的内容

javascript 在提示后显示用户输入

html - 如何删除网站和导航栏周围的空间[CSS] [margin collapsing]

PHP 下一个第二个星期五相对于当前日期

php - Laravel 获取按关系列排序的模型集合

php - 是否可以在 PHP 中从自身引用匿名函数?

javascript - 如何覆盖选项卡或用户单击的焦点(焦点)

php - 无法覆盖 s3 中的内容配置 header

javascript - 使用 javaScript 提交表单时如何更新 div 的内容

javascript - 带有 jquery 数组的 .grep() 只返回最后一位数字