title: PHP4 problems for Ajax, json_encode and database
body: <p>I have my company project which they require PHP4 development.Please check the complete code. It should display list of mobile brands and price and should filter according to multiple checkbox. As i have to work in PHP4 i dont know what mistake i am doing.Its not displaying the information from database.</p>
  
  <p>index.php</p>
  
  <pre><code>&lt;!DOCTYPE HTML&gt;
      &lt;html&gt;
        &lt;head&gt;
          &lt;meta charset="utf-8"&gt;
          &lt;title&gt;AJAX filter demo&lt;/title&gt;
          &lt;style&gt;
            body {
              padding: 10px;
            }
  
            h1 {
                margin: 0 0 0.5em 0;
                color: #343434;
                font-weight: normal;
                font-family: 'Ultra', sans-serif;   
                font-size: 36px;
                line-height: 42px;
                text-transform: uppercase;
                text-shadow: 0 2px white, 0 3px #777;
            }
  
            h2 {
                margin: 1em 0 0.3em 0;
                color: #343434;
                font-weight: normal;
                font-size: 30px;
                line-height: 40px;
                font-family: 'Orienta', sans-serif;
            }
  
            #phones {
              font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
              font-size: 12px;
              background: #fff;
              margin: 15px 25px 0 0;
              border-collapse: collapse;
              text-align: center;
              float: left;
              width: 300px;
            }
  
            #phones th {
              font-size: 14px;
              font-weight: normal;
              color: #039;
              padding: 10px 8px;
              border-bottom: 2px solid #6678b1;
            }
  
            #phones td {
              border-bottom: 1px solid #ccc;
              color: #669;
              padding: 8px 10px;
            }
  
            #phones tbody tr:hover td {
              color: #009;
            }
  
            #filter {
              float:left;
            }
          &lt;/style&gt;
        &lt;/head&gt;
        &lt;body&gt; 
          &lt;h1&gt;Phones database&lt;/h1&gt;
  
          &lt;table id="phones"&gt;
            &lt;thead&gt;
              &lt;tr&gt;
                &lt;th&gt;ID&lt;/th&gt;
                &lt;th&gt;Brand&lt;/th&gt;
                &lt;th&gt;Model&lt;/th&gt;
                &lt;th&gt;Price&lt;/th&gt;
              &lt;/tr&gt;
            &lt;/thead&gt;
            &lt;tbody&gt;
            &lt;/tbody&gt;
          &lt;/table&gt;
  
          &lt;div id="filter"&gt;
            &lt;h2&gt;Filter options&lt;/h2&gt;
            &lt;div&gt;
              &lt;input type="checkbox" id="Samsung" checked&gt;
              &lt;label for="Samsung"&gt;Samsung&lt;/label&gt;
            &lt;/div&gt;
            &lt;div&gt;
              &lt;input type="checkbox" id="iPhone" checked&gt;
              &lt;label for="iPhone"&gt;iPhone&lt;/label&gt;
            &lt;/div&gt;
            &lt;div&gt;
              &lt;input type="checkbox" id="HTC" checked&gt;
              &lt;label for="HTC"&gt;HTC&lt;/label&gt;
            &lt;/div&gt;
            &lt;div&gt;
              &lt;input type="checkbox" id="LG" checked&gt;
              &lt;label for="LG"&gt;LG&lt;/label&gt;
            &lt;/div&gt;
            &lt;div&gt;
              &lt;input type="checkbox" id="Nokia" checked&gt;
              &lt;label for="Nokia"&gt;Nokia&lt;/label&gt;
            &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt; 
          &lt;script&gt;
            function makeTable(data){
              console.log(data);
             var tbl_body = "";
                $.each(data, function() {
                  var tbl_row = "";
                  $.each(this, function(k , v) {
                    tbl_row += "&lt;td&gt;"+v+"&lt;/td&gt;";
                  })
                  tbl_body += "&lt;tr&gt;"+tbl_row+"&lt;/tr&gt;";
                })
  
              return tbl_body;
            }
  
            function getPhoneFilterOptions(){
              var opts = [];
              $checkboxes.each(function(){
                if(this.checked){
                  opts.push(this.id);
                }
              });
  
              return opts;
            }
  
            function updatePhones(opts){
              $.ajax({
                type: "POST",
                url: "submit.php",
                dataType : 'json',
                cache: false,
                data: {filterOpts: opts},
                success: function(records){
                  $('#phones tbody').html(makeTable(records));
                }
              });
            }
  
            var $checkboxes = $("input:checkbox");
            $checkboxes.on("change", function(){
              var opts = getPhoneFilterOptions();
              updatePhones(opts);
            });
  
            $checkboxes.trigger("change");
          &lt;/script&gt; 
        &lt;/body&gt; 
      &lt;/html&gt;
  </code></pre>
  
  <p>submit.php</p>
  
  <pre><code>&lt;?php 
  require 'Database.php';
  #### TEMP SET NAMES FÜR UTF8 ###################################################
  require_once('Json.php');
  
    $opts = $_POST['filterOpts'];
    foreach ($opts as &amp;$opt) {
        $opt = sprintf("'%s'", mysql_real_escape_string($opt));
    }
    $query = sprintf(
        "SELECT mobile_phone.id, name, model, price
         FROM mobile_phone
         INNER JOIN brand ON brand_id = brand.id
         WHERE name IN (%s)",
        join(',', $opts)
    );
  
    $result = mysql_query($query);
    $data   = array();
    while ($row = mysql_fetch_assoc($result)) {
        $data[] = $row;
    }
  
    echo json_encode($data);
  ?&gt;
  </code></pre>
  
  <p>Json
  <a href="http://mike.teczno.com/JSON/JSON.phps" rel="nofollow">enter link description here</a></p>