jQuery/PHP 中某些字符的问题
在我的代码中添加一些 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 时它工作得很好,但现在我获取数据,它用钻石替换了一些字符?字符图标。
I am having problems with some characters after I added some jQuery to my code.
http://www.blueskycouncil.com/login-form.php (login: stack/this)
It worked fine when I was just doing it all in php but now it converts some of the characters weirdly.
This is the jQuery code I am using:
<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();
})
});
and in the _db_index.php file it fetch it like this
<?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');
?>
As I said it worked fine when it was PHP but now that i fetch the data it replaces some characters with diamond ? character icon.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是一个编码问题,有很多事情需要检查 - UTF-8保存到mysql时出现问题
is an encoding issue, lots of things to check - UTF-8 problem when saving to mysql
响应采用 cp1252 编码,但您的页面采用 UTF8
消失的字符具有十进制代码 146
您应该在发送响应之前更改编码(或者更好在将数据插入数据库之前更改编码)。
The response is encoded in cp1252, but your page in UTF8
The char that disappears has decimal-code 146
You should change the encoding before you send the response(or better before you insert the data to the db).
谢谢大家,我修复了它
mysql_query("SET NAMES utf8");在 db_connection 文件中。
Thanks everyone I fixed it
mysql_query("SET NAMES utf8"); in the db_connection file.