i'm trying live search jquery, php, , mysql. i'm no expert know , understand enough dangerous. @ rate, seems working except when of search results contain single or double quotes. example, search results may contain:
contact, door 3/4" recessed
motion, detector 35' x 50'
and on , on.
my code is:
<script type="text/javascript"> function lookup(inputstring) { if(inputstring.length == 0) { // hide suggestion box. $('#suggestions').hide(); } else { $.post("get_parts.php", {querystring: ""+inputstring+""}, function(data){ if(data.length >0) { $('#suggestions').show(); $('#autosuggestionslist').html(data); } }); } } function fill(thisvalue) { $("#inputstring").val(thisvalue); settimeout("$('#suggestions').hide();", 200); } </script>
and
<?php if(isset($_post['querystring'])) { $querystring = $_post['querystring']; if(strlen($querystring) >0) { $query = "select short_desc equipment short_desc '$querystring%' order short_desc asc limit 10"; $result = mysql_query($query) or die("there error in database"); while($row = mysql_fetch_array($result)){ $escaped_desc_html = htmlentities($row['short_desc']); $escaped_desc_escape = addslashes($row['short_desc']); echo "<li onclick=\"fill('".$escaped_desc_html."');\">".$escaped_desc_html."</li>"; }; }; }; ?>
i've tried storing data in mysql this:
contact, door 3/4\" recessed
motion, detector 35\' x 50\'
but comes out on page looking that.
i've tried addslashes(), htmlentities(), combinations of both, , keeps breaking. tried escape() in javascript don't formatting , list continuous block of jumbled text.
my problem when results of search passed jquery, quote or double quotes makes think command if on , error missing ). there way make jquery ignore html, slashes, or quotes come data? maybe have treat object instead of commands. htmlentities() &quo; still breaks code. it's driving me crazy! problem breaks in these 2 places:
$('#autosuggestionslist').html(data);
and
$("#inputstring").val(thisvalue);
i had similar problem. problem double quotes converted them "
on input using:
$output = str_replace("\"",""",$input);
if causes problem javascript use same code replace "
"-" or something.
when user input stage sure protect injection attacks others have mentioned!
sorry seen date on one!! hope helps though
Comments
Post a Comment