Ajax,json_encode和数据库的PHP4问题

时间:2014-05-22 11:06:54

标签: javascript ajax json php4

我有我的公司项目,他们需要PHP4开发。请检查完整的代码。它应显示移动品牌和价格列表,并应根据多个复选框进行过滤。因为我必须在PHP4中工作,所以我不知道我在做什么错误。它不显示数据库中的信息。

的index.php

<!DOCTYPE HTML>
    <html>
      <head>
        <meta charset="utf-8">
        <title>AJAX filter demo</title>
        <style>
          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;
          }
        </style>
      </head>
      <body> 
        <h1>Phones database</h1>

        <table id="phones">
          <thead>
            <tr>
              <th>ID</th>
              <th>Brand</th>
              <th>Model</th>
              <th>Price</th>
            </tr>
          </thead>
          <tbody>
          </tbody>
        </table>

        <div id="filter">
          <h2>Filter options</h2>
          <div>
            <input type="checkbox" id="Samsung" checked>
            <label for="Samsung">Samsung</label>
          </div>
          <div>
            <input type="checkbox" id="iPhone" checked>
            <label for="iPhone">iPhone</label>
          </div>
          <div>
            <input type="checkbox" id="HTC" checked>
            <label for="HTC">HTC</label>
          </div>
          <div>
            <input type="checkbox" id="LG" checked>
            <label for="LG">LG</label>
          </div>
          <div>
            <input type="checkbox" id="Nokia" checked>
            <label for="Nokia">Nokia</label>
          </div>
        </div>

        <script src="http://code.jquery.com/jquery-latest.js"></script> 
        <script>
          function makeTable(data){
            console.log(data);
           var tbl_body = "";
              $.each(data, function() {
                var tbl_row = "";
                $.each(this, function(k , v) {
                  tbl_row += "<td>"+v+"</td>";
                })
                tbl_body += "<tr>"+tbl_row+"</tr>";
              })

            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");
        </script> 
      </body> 
    </html>

submit.php

<?php 
require 'Database.php';
#### TEMP SET NAMES FÜR UTF8 ###################################################
require_once('Json.php');

  $opts = $_POST['filterOpts'];
  foreach ($opts as &$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);
?>

的Json enter link description here

1 个答案:

答案 0 :(得分:0)

json_encode或者json_decode函数不包含在PHP4中,请使用PHP4 json操作库。

PS:您已包含Json.php,该库没有json_*个功能,Json.php文件有Services_JSON个类,请使用此方式

$jsonObj = new Services_JSON();
echo $jsonObj->encode($data);