Witam,
chciałem zrobić na stronie za pomocą AJAXa w polu wyszukiwania opcję auto suggest. Na temat kodowania polskich znaków wiele było.. i wiele już przeczytałem.. niestety bliski jestem poddania się (IMG:
style_emoticons/default/sad.gif)
Skrypt który tutaj prezentuję nie jest moim dziełem. Dostosowałem go tylko do połączenia z moją bazą MSSQL.
Wydaje mi się, że problem jest po stronie AJAXa. Bardzo proszę o pomoc.
Nagłówek oczywiście rozwiązuje moje problemy z polskimi znakami:
<?php
header('Content-type: text/html; charset=windows-1250'); ?>
o dziwo windows-1250 a nie UTF-8 (na którym mam krzaki).
W opcji wyszukiwania mam taki dziwny problem. Przykład:
Słowo szukane: MAŚĆ. Autosugestia po wpisaniu MA... pokazuje MAŚĆ ale po wpisaniu całego słowo podpowiedz znika.
Kolejny przykład słowo: ŚCIANA -> autosugestia nie znajduje takiego słowa mimo, że w bazie jest.
Podsumowując:
Skrypt wyświetla wszystkie polskie znaki ł,ź,ć itd ale już nie wyszukuje po tych znakach..
Skrypt który wykorzystuję:
index.php
<?php
header('Content-type: text/html; charset=windows-1250'); ?>
<body onLoad="DoCostam();">
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript">
</script>
<link href="styles.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="i_hate_IE.css" /></link>
</head>
<body>
<center>
<div class="ajax-div">
<div class="input-div">
User:
<input type="text" onKeyUp="getScriptPage('box','text_content')" id="text_content" size="40">
</div>
<div id="box"></div>
</div>
</center>
</body>
</html>
script_page.php
<?php
header('Content-type: text/html; charset=windows-1250'); ?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; windows-1250" />
<script type="text/javascript" src="ajax.js"></script>
</head>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=windows-1250">
<?php
include('conn.php');
{
$sel = mssql_query
("SELECT DISTINCT NAZWISKO_IMIE FROM PRACOWNIK WHERE NAZWISKO_IMIE LIKE '".trim($str)."%'");
if(mssql_num_rows($sel))
{
echo "<table border =\"0\" width=\"100%\">\n"; if(mssql_num_rows($sel))
{
echo "<script language=\"javascript\">box('1');</script>"; while($row = mssql_fetch_array($sel))
{
$NAZWISKO_IMIE = str_ireplace($str,"<b>".$str."</b>",($row['NAZWISKO_IMIE']));
echo "<tr id=\"word".$row['id']."\" onmouseover=\"highlight(1,'".$row['id']."');\" onmouseout=\"highlight(0,'".$row['id']."');\" onClick=\"display('".$row['NAZWISKO_IMIE']."');\" >\n<td>".$NAZWISKO_IMIE."</td>\n</tr>\n"; }
}
}
}
else
{
echo "<script language=\"javascript\">box('0');</script>"; }
?>
</head>
do tego oczywiście połączenie z bazą conn.php i ajax.js
subject_id = '';
function handleHttpResponse() {
if (http.readyState == 4) {
if (subject_id != '') {
document.getElementById(subject_id).innerHTML = http.responseText;
}
}
}
function getHTTPObject() {
var xmlhttp;
@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object
function getScriptPage(div_id,content_id)
{
subject_id = div_id;
content = document.getElementById(content_id).value;
http.open("POST", "script_page.php?content=" + escape(content), true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
if(content.length>0)
box('1');
else
box('0');
}
function highlight(action,id)
{
if(action)
document.getElementById('word'+id).bgColor = "#C2B8F5";
else
document.getElementById('word'+id).bgColor = "#F8F8F8";
}
function display(word)
{
document.getElementById('text_content').value = word;
document.getElementById('box').style.display = 'none';
document.getElementById('text_content').focus();
}
function box(act)
{
if(act=='0')
{
document.getElementById('box').style.display = 'none';
}
else
document.getElementById('box').style.display = 'block';
}
Będę wdzięczny za wszelkie uwagi.