使用JavaScript

时间:2018-04-26 01:13:43

标签: javascript php jquery ajax

尝试在点击链接的同一页面上将php结果(来自其他页面)加载到div

我已尝试按照here

中的答案

但这对我没有用。以下是我试过的内容

price.php

<div class="pricepop"></div>

<?php
$sql = $db->prepare("SELECT * FROM commodityprices");
$result = $sql->execute();
while ($row = $result->fetchArray(SQLITE3_ASSOC))
{
    $id = $row['id'];
    $_SESSION['id'] = $id;
    $name = $row['name'];
    $unit = $row['unit'];
    $iprice = $row['iprice'];
    $lprice = $row['lprice'];
    $irate = $row['irate'];
    $lrate = $row['lrate'];
    $terms = $row['cterms'];
    $asat = date('d/M/Y');
    $market = $row['market'];

    echo '<tr>
    <td>'.$name.'</td>
    <td>'.$unit.'</td>';
        if ($irate == "Down")
        {
            echo '
                 <td style="background: #ef5a66; color: #fff"><i class="fa fa-caret-down"> </i> '.$iprice.'</td>';
        }
        else
        {
            echo '
                  <td style="background: #28bc88; color: #fff"><i class="fa fa-caret-up"> </i> '.$iprice.'</td>';
        }
            echo '<td>'.$terms.'</td>';
            if ($lrate == "Down")
            {
                echo '
                  <td style="background: #ef5a66; color: #fff"><i class="fa fa-caret-down"> </i> '.$lprice.'</td>';
             }
             else
             {
                echo '
                  <td style="background: #28bc88; color: #fff"><i class="fa fa-caret-up"> </i> '.$lprice.'</td>';
             }
                echo '<td>'.$asat.'</td>
                      <td>'.$market.'</td>
                      <td><a class="comprice" href="pricedetails.php?id='.$id.'">View more</a>//Link to dispaly more results
</td>
                          </tr>';
             }

pricedetails.php

<?php
                  $priceId = $_GET['id'];
                  $sql = $db->prepare("SELECT * FROM commodityprices WHERE id = ?");
                  $sql->bindParam(1, $priceId, SQLITE3_INTEGER);
                  $result = $sql->execute();
                  while ($row = $result->fetchArray(SQLITE3_ASSOC))
                  {
                      $id = $row['id'];
                      $name = $row['name'];
                      $iprice = $row['iprice'];
                      $lprice = $row['lprice'];
                      $lrate = $row['lrate'];
                      $terms = $row['cterms'];
                      $asat = date('d/M/Y');
                      $market = $row['market'];
                      $priceperbags = $row['priceperbags'];

                          echo '<tr>
                          <td>'.$name.'</td>';
                          echo '<td>'.$terms.'</td>';
                          if ($lrate == "Down")
                          {
                              echo '
                              <td style="background: #ef5a66; color: #fff"><i class="fa fa-caret-down"> </i> '.$lprice.'</td>';
                          }
                          else
                          {
                            echo '
                            <td style="background: #28bc88; color: #fff"><i class="fa fa-caret-up"> </i> '.$lprice.'</td>';
                          }
                          echo '<td>'.$asat.'</td>
                          <td>'.$market.'</td>
                          <td class="comprice">'.$priceperbags.'</td>
                          </tr>';
                  }

JavaScript

$(document).ready(function() {
    $(".comprice").on("click", function (e) {
        e.preventDefault();
        $(".pricepop").load("pricedetails.php");
    });
});

当我点击 price.php 中的查看更多链接时, pricedeatils.php 中的结果应显示在{{ 1}}(在price.php页面中)但当我点击当前.pricepop div为空时。请问我该如何解决这个问题?感谢。

1 个答案:

答案 0 :(得分:1)

您的代码未加载正确的网址。已修改,因此id参数是网址的一部分。

请改用此js脚本。

$(document).ready(function() {
    $(".comprice").on("click", function (e) {
        e.preventDefault();
        $(".pricepop").load($(this).attr('href'));
    });
});