jQuery / PHP中某些字符的问题

时间:2010-11-12 18:58:35

标签: php jquery encoding utf-8

在我的代码中添加了一些jQuery之后,我遇到了一些字符问题。

http://www.blueskycouncil.com/login-form.php(登录:stack / this)

当我在php中完成所有操作时它工作得很好但是现在它很奇怪地转换了一些字符。

这是我正在使用的jQuery代码:

<script type="text/javascript">

// Check to see if document is ready

$(document).ready(function () {

    // Set sort mode to Best        

    $.post("_db_index.php", 
        {sort_id: "best"},

            // Take data from _db_index.php and put it into the HTML
            function(output){
                $('#left').html(output).show();
        });

    });
        // Check to see whether user have voted on item before 
        function updateKarma(element, id, sortId){
          $.post("idea_karma.php", 
              {pagetype: "index", karmatype: "ideaspace", id: id, sort_id: sortId}, 

        function(output){
                element.parentNode.className="karma-btn_voted";
              element.parentNode.innerHTML="<span class=\"voted\">"+output+"</span>";
          });

              return false;
        }

        function viewMode(sortId){
            $.post("_db_index.php", 

                {sort_id: sortId},
                    function(output){
                    $('#left').html(output).show();

                    $.post("subnavigation.php", 
                        {sort_id: sortId},

                            function(output){
                            $('#base').html(output).show();
                        });


                    });




            };

                $(function(){  
                 $(".base a").hover(function(){  
                  $(this).children("span").fadeOut();  
                 }, function(){  
                  $(this).children("span").fadeIn();  
                 })    
                });

并在_db_index.php文件中以此方式获取它

<?php

// Start session

require_once('auth.php');
require_once('config.php');
require_once('db_open_select.php');

// Functions

include('trunctate_text.php');

$sort_id = $_POST['sort_id'];
$member_id = $_SESSION['SESS_MEMBER_ID'];

// Check for PHP Insert Hack
if(array_key_exists("sort_id",$_POST)){
    $sort_allowed = array("best","new","comments");
        if(in_array($_POST["sort_id"],$sort_allowed)){
            $sort_id = $_POST["sort_id"];
        }
}

echo "<div id=\"gradient\">";

        //If User selected Best Rated or if url is empty:
        if (empty($_POST) OR $sort_id == "best") {

            //Perform database query
             $result = mysql_query("SELECT * , (SELECT COUNT( 1 ) FROM comments C WHERE C.idea_id = I.id) AS COMMENTS, ( SELECT login FROM members M WHERE I.member_id = M.member_id ) as login FROM ideaspace I ORDER BY KARMA DESC", $connection); 
            $query = mysql_query("SELECT idea_id FROM vote_idea WHERE member_id = $member_id", $connection);


            //Create array with which ideas the current user has voted on already
                $user_voted_on_this = array();

                    while($row = mysql_fetch_assoc($query))
                        {
                         $user_voted_on_this[] = $row["idea_id"];
                        }

                //If User selected newest:
                } elseif ($sort_id == "new") {

                            $result = mysql_query("SELECT * , (SELECT COUNT( 1 ) FROM comments C WHERE C.idea_id = I.id) AS COMMENTS, ( SELECT login FROM members M WHERE I.member_id = M.member_id ) as login FROM ideaspace I ORDER BY DATE DESC", $connection); 
                            $query = mysql_query("SELECT idea_id FROM vote_idea WHERE member_id = $member_id", $connection);

                            //Create array with which ideas the current user has voted on already
                                $user_voted_on_this = array();

                                    while($row = mysql_fetch_assoc($query))
                                        {
                                         $user_voted_on_this[] = $row["idea_id"];
                                        } 

                        //If User selected most commented:                
                            } else {

                                if ($sort_id == "comments")

                                $result = mysql_query("SELECT * , (SELECT COUNT( 1 ) FROM comments C WHERE C.idea_id = I.id) AS COMMENTS, ( SELECT login FROM members M WHERE I.member_id = M.member_id ) as login FROM ideaspace I ORDER BY COMMENTS DESC", $connection); 
                                $query = mysql_query("SELECT idea_id FROM vote_idea WHERE member_id = $member_id", $connection);



                                    $user_voted_on_this = array();

                                        while($row = mysql_fetch_assoc($query))
                                            {
                                             $user_voted_on_this[] = $row["idea_id"];
                                            }  

                }

                if (!$result && !$query) {    
                    die("Database connection failed: " . mysql_error());
                }



        // 4. Use data from database
        while ($row = mysql_fetch_array($result)) {

            echo 
                "<dt id=\"idea\">";

                        if (in_array($row['id'],$user_voted_on_this)) {

                                        echo 
                                            "<div class=\"karma-btn_voted\">
                                                <span class=\"voted\">{$row['karma']}</span>
                                                    </div>";

                                                    } else { 

                                        echo 
                                            "<div class=\"karma-btn\">

                        <a href=\"javascript:void(0);\" onclick=\"return updateKarma(this,'{$row['id']}', '$sort_id')\"><img src=\"images/btn_lrg_karma.png\" alt=\"Alternative text\"><span class=\"voted\"><div class=\"newkarma\">{$row['karma']}</div></span></a>
                                            </div>";    

                            }

                                        echo    
                                            "<div class=\"textbox\">
                                                <P class=\"category\">" . $row['category'] . "</p>
                                                    <P class=\"headline\"> <a href=\"details_idea.php?itemid={$row['id']}\">" . $row['d_header']."</a></P>
                                                        <P>" . $shortdesc = myTruncate($row['d_description'], 220, " ") . "</p>" .
                                                            "<P class=\"name\">Submitted by " . $row['login'] . " " . date('D d Y', strtotime($row['date'])) . "<img src=\"images/comments.png\" align=\"center\"><a href=\"#\">". $row['COMMENTS'] ."</a></p>" .

                                                                    "</div>
                                                                        </dt></div>";


        }



?>


    <?Php
    require_once('db_close.php');
    ?>

正如我所说,当它是PHP时它工作得很好但是现在我获取数据它用钻石取代了一些字符?角色图标。

3 个答案:

答案 0 :(得分:2)

是一个编码问题,需要检查很多内容 - UTF-8 problem when saving to mysql

答案 1 :(得分:1)

响应以cp1252编码,但您的页面采用UTF8编码

消失的char有十进制代码146

您应该在发送响应之前更改编码(或者在将数据插入数据库之前更好)。

mb_convert_encoding($str, "UTF-8", "CP1252");

答案 2 :(得分:0)

谢谢大家我修好了

mysql_query(“SET NAMES utf8”);在db_connection文件中。