Im no good at javascript but i have a form with a field which u type in and it suggests customers from the sql db which matches the characters typed. The problem is i may have a customer with this
'good & small' which in my db looks like this 'good & small' When i select it from the list of results it displays 'good $amp; small' (but in the list it is 'good & small' before selecting) and not 'good and small'. From here i reconvert the string using php back to the proper state.
So the question is : is there some where in this code below where i can convert the string first before it is displayed in the box with '&'
I have had to convert the code into this format so that the db accepts apostropies only to find out now the javascript doesnt like &.
[IMG]http://www.fencehiresouthern.co.uk/pic1.bmp[/IMG]
[IMG]http://www.fencehiresouthern.co.uk/pic2.bmp[/IMG]
search form
print "<b>Amend Company Details</b><br>";
print"<form method=\"post\" action=amendcompany.php?action=amendcompany>";
print"<input type=\"text\" id=\"searchinput\" name=\"company\" autocomplete=\"off\" style=\"width: 240px;\" ondblclick=\"suggest(event.keyCode,this.value);\" onkeyup=\"suggest(event.keyCode,this.value);\" onkeypress=\"return noenter(event.keyCode);\" value=\"\" />";
print"<input type=submit value=Continue />";
print"</form>";
print"<script language=\"JavaScript\" src=\"suggest.js\" type=\"text/javascript\"></script>";
print"<div id=\"suggcontainer\" style=\"text-align: left; width: 520px; display: none;\">";
print"<div id=\"suggestions\" style=\"cursor: default; position: absolute; background-color: #FFFFFF; border: 1px solid #777777;\"></div>";
print"</div>";
print"</form>";
javascript
function $(e){if(typeof e=='string')e=document.getElementById(e);return e};
function collect(a,f){var n=[];for(var i=0;i<a.length;i++){var v=f(a[i]);if(v!=null)n.push(v)}return n};
ajax={};
ajax.x=function(){try{return new ActiveXObject('Msxml2.XMLHTTP')}catch(e){try{return new ActiveXObject('Microsoft.XMLHTTP')}catch(e){return new XMLHttpRequest()}}};
ajax.serialize=function(f){var g=function(n){return f.getElementsByTagName(n)};var nv=function(e){if(e.name)return encodeURIComponent(e.name)+'='+encodeURIComponent(e.value);else return ''};var i=collect(g('input'),function(i){if((i.type!='radio'&&i.type!='checkbox')||i.checked)return nv(i)});var s=collect(g('select'),nv);var t=collect(g('textarea'),nv);return i.concat(s).concat(t).join('&');};
ajax.send=function(u,f,m,a){var x=ajax.x();x.open(m,u,true);x.onreadystatechange=function(){if(x.readyState==4)f(x.responseText)};if(m=='POST')x.setRequestHeader('Content-type','application/x-www-form-urlencoded');x.send(a)};
ajax.get=function(url,func){ajax.send(url,func,'GET')};
ajax.gets=function(url){var x=ajax.x();x.open('GET',url,false);x.send(null);return x.responseText};
ajax.post=function(url,func,args){ajax.send(url,func,'POST',args)};
ajax.update=function(url,elm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.get(url,f)};
ajax.submit=function(url,elm,frm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.post(url,f,ajax.serialize(frm))};
var pos = 0;
var count = 0;
function noenter(key) {
suggcont = document.getElementById("suggcontainer");
if (suggcont.style.display == "block") {
if (key == 13) {
choiceclick(document.getElementById(pos));
return false;
} else {
return true;
}
} else {
return true;
}
}
document.onclick = function () { closechoices(); }
function suggest(key,query) {
if (key == 38) {
goPrev();
} else if (key == 40) {
goNext();
} else if (key != 13) {
if (query.length > 1 ) {
t = new Date();
ajax.get('suggest.php?q='+query+'&bla='+t.getTime(),update);
} else {
update('');
}
}
}
function update(result) {
arr = new Array();
arr = result.split('\r\n');
if (arr.length > 10) {
count = 10;
} else {
count = arr.length;
}
suggdiv = document.getElementById("suggestions");
suggcont = document.getElementById("suggcontainer");
if (arr[0].length > 0) {
suggcont.style.display = "block";
suggdiv.innerHTML = '';
suggdiv.style.height = count * 20;
for (i = 1; i <= count; i++) {
novo = document.createElement("div");
suggdiv.appendChild(novo);
novo.id = i;
novo.style.height = "14px";
novo.style.padding = "3px";
novo.onmouseover = function() { select(this,true); }
novo.onmouseout = function() { unselect(this,true); }
novo.onclick = function() { choiceclick(this); }
novo.innerHTML = arr[i-1];
}
} else {
suggcont.style.display = "none";
count = 0;
}
}
function select(obj,mouse) {
obj.style.backgroundColor = '#3399ff';
obj.style.color = '#ffffff';
if (mouse) {
pos = obj.id;
unselectAllOther(pos);
}
}
function unselect(obj,mouse) {
obj.style.backgroundColor = '#ffffff';
obj.style.color = '#000000';
if (mouse) {
pos = 0;
}
}
function goNext() {
if (pos <= count && count > 0) {
if (document.getElementById(pos)) {
unselect(document.getElementById(pos));
}
pos++;
if (document.getElementById(pos)) {
select(document.getElementById(pos));
} else {
pos = 0;
}
}
}
function goPrev() {
if (count > 0) {
if (document.getElementById(pos)) {
unselect(document.getElementById(pos));
pos--;
if (document.getElementById(pos)) {
select(document.getElementById(pos));
} else {
pos = 0;
}
} else {
pos = count;
select(document.getElementById(count));
}
}
}
function choiceclick(obj) {
document.getElementById("searchinput").value = obj.innerHTML;
count = 0;
pos = 0;
suggcont = document.getElementById("suggcontainer");
suggcont.style.display = "none";
document.getElementById("searchinput").focus();
}
function closechoices() {
suggcont = document.getElementById("suggcontainer");
if (suggcont.style.display == "block") {
count = 0;
pos = 0;
suggcont.style.display = "none";
}
}
function unselectAllOther(id) {
for (i = 1; i <= count; i++) {
if (i != id) {
document.getElementById(i).style.backgroundColor = '#ffffff';
document.getElementById(i).style.color = '#000000';
}
}
}
suggest.php
<?php
require("backend/functions.php");
dbconn(false);
//loggedinorreturn();
if (strlen($_GET['q']) > 1) {
$q = str_replace(" ",".",sqlesc("%".$_GET['q']."%"));
$q2 = str_replace("."," ",sqlesc("%".$_GET['q']."%"));
$result = mysql_query("SELECT eventcompany.company FROM eventcompany WHERE eventcompany.company LIKE {$q} OR eventcompany.company LIKE {$q2} ORDER BY company ASC LIMIT 0,10;");
if (mysql_numrows($result) > 0) {
for ($i = 0; $i < mysql_numrows($result); $i++) {
$name = mysql_result($result,$i,"company");
//$name2= htmlspecialchars($name1, ENT_QUOTES);
$name = trim(str_replace("\t","",$name));
#$name1 = mysql_result($result,$i,"id");
#$name1 = trim(str_replace("\t","",$name1));
#print "$name1 $name";
print $name;
if ($i != mysql_numrows($result)-1) {
print "\r\n";
}
}
}
}
?>