0

I have a weird situation. my page as below:

<html>
<script>
$(document).ready(function(){
$("input").keyup(function(){
$("#mydiv").load("showlist.php?q="+this.value);
});
</script>
<input><br>
<div id="mydiv">ajax content appear in here</div>
</html>

my showlist.php as simple as:

echo $_GET['q'];

The page runs fine when I enter i.e: abcde in the text box {it means the content in the mydiv tag replaced by what output by showlist.php }. BUT when I type abcd efgh {it means it has ONE space in the string } then nothing appear within the div. It is just blank! What have I done wrong?

Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
gaixixon
  • 13
  • 5

2 Answers2

2

You should encode the string before passing it to the server using encodeURIComponent():

$("#mydiv").load("showlist.php?q="+encodeURIComponent(this.value));

Edit: A useful SO question concerning encodeURIComponent() vs encodeURI()

Community
  • 1
  • 1
Mathachew
  • 2,044
  • 2
  • 18
  • 31
0

The ' ' (space) needs to be urlencoded to %20 (a '+' will also do)

Encode your URI's with:

var newUri = 'showlist.php?' + encodeURI("q="+this.value);
$("#mydiv").load(newUri);
Sp4cecat
  • 991
  • 1
  • 8
  • 18