【问题标题】:PHP4 problems for Ajax, json_encode and databaseAjax、json_encode 和数据库的 PHP4 问题
【发布时间】:2014-07-11 09:20:13
【问题描述】:

我有我的公司项目,他们需要 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>

提交.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

【问题讨论】:

    标签: javascript ajax json php4


    【解决方案1】:

    json_encodejson_decode函数不包含在PHP4中,请使用PHP4 json操作库。

    PS:你已经包含Json.php,库没有json_*函数,Json.php文件有Services_JSON类,请这样使用

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

    【讨论】:

    • 嗨,你的意思是我不应该包含 Json.php 文件并在 submit.php 文件中使用上面的代码行?谢谢。
    • 嗨,我尝试删除 Json.php 文件并包括上面你给我的代码行,但仍然没有得到数据..
    • @user3659737 Json.php文件有JSON操作方法如encodedecode,请创建Services_JSON类的对象并按对象调用方法.,我在底部提到了示例代码
    • @user3659737 你得到了什么 ajax 响应,你能和我分享一下吗?
    • 嗨,我得到了复选框过滤选项,只是一个没有来自数据库的任何数据的表。唯一的问题是我没有从数据库中获取数据。否则我认为我的复选框将过滤数据一次从数据库中获取数据。
    猜你喜欢
    • 2014-12-18
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 2010-09-25
    • 2010-12-03
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    相关资源
    最近更新 更多