title: PHP, javascript, single quote problems with IE when passing variable from ajax post to javascript function
body: <p>I have been trying to get this to work for a while, and I suspect there's an easy solution that I just can't find. My head feels like jelly and I would really appreciate any help.</p>
  
  <p>My main page.php makes a .post() to backend.php and fetches a list of cities which it echoes in the form of:</p>
  
  <pre><code>&lt;li onclick="script('$data');"&gt;$data&lt;/li&gt;
  </code></pre>
  
  <p>The list is fetched and put onto the page via .html().</p>
  
  <p>My problem occurs when $data has a single quote in it. backend.php passes the variable just fine to page.php but when i run html() it throws a javascript error (in IE, not FF obviously);</p>
  
  <p><em>')' is expected</em></p>
  
  <p>IE parses the single quote and messes up the script()-call. I've been trying to rebuild the echoed string in different ways, escaping the 's on the php side and/or on the javascript side - but in vain. Do I have to review the script in total?</p>
  
  <p>page.php</p>
  
  <pre><code>$.post("backend.php", {q: ""+str+""}, function(data) {
      if(data.length &gt;0) {
           $('#results').html(data);
      }
  </code></pre>
  
  <p>backend.php</p>
  
  <pre><code>while ($row = $q-&gt;fetch()) {
      $city = $row['City'];
  //  $city = addslashes($row['City']);
  //  $city = str_replace("'","&amp;#39;",$row['City']);
      echo "&lt;li onclick=\"script('$city');\"&gt;".$city."&lt;/li&gt;";
  }
  </code></pre>