jQuery and PHP undefined index


jQuery and PHP undefined index

大家好!

我正在尝试使用 jQuery 加载外部php文件,并且得到了undefined index,因为显然我在外部 php 文件中使用了GET方法。

这是我的代码:

主文件

<script>
$(function() {
  $('#loadClients').load('clientsTable.php');
});
</script>
<body>
<div id="loadClients"><!-- load here--></div>
</body>

客户端表.php

<table class="searchTbl" >
  <thead>
    <tr>
      <th width="200">Account #</th>
      <th width="300">Customer Name</th>
      <th width="150">Balance</th>
      <th width="100">Action</th>
    </tr>
  </thead>
  <tbody>
    <?php
      require ("db.php");
      $add = $_GET['address'];
      if ($_GET['address']=="All") 
      {
      $gets = mysql_query("SELECT * FROM customers");
      }
      else
      {
      $gets = mysql_query("SELECT * FROM customers WHERE cusadd='$add'");
      }
      while($row = mysql_fetch_assoc($gets))
      {
      ?>
    <tr>
      <td> <?= $row['accno']; ?> </td>
      <td> <?= $row['name']; ?> </td>
      <!-- <td style="color:#f0356e; "> <?php //number_format($row['totbal']); ?> </td> -->
      <td style="color:#f0356e; "> <?= formatMoney($row['totbal'], true); ?> </td>
      <td> <a href="#" data-reveal-id="myModal" data-reveal-ajax="records.php?id=<?= $row['accno']; ?>" id="viewData-<?= $row['accno']; ?>"> View Data </a> </td>
    </tr>
    <?php } ?>
  </tbody>
</table>

在我的主文件中,示例网址是 localhost/accountsknc/main.php?address=All .我收到的错误/通知Notice: Undefined index: address,因为它可能是外部文件。

有没有办法解决这个问题,以便客户端的数据将显示在我的主文件中?提前谢谢你。

PS:我知道我的代码很脆弱,因为 mysql 已被弃用,但我只在本地测试它,并且在我已经实现它时使用 PDO。

您需要对这两行进行小的修改。

$add = $_GET['address'];
if ($_GET['address']=="All") 

$add = isset($_GET['address']) ? $_GET['address'] : 'All';
if($add == "All")

因此,如果未设置 $_GET['address'],它将查找所有地址,就好像您的 $_GET['地址'] 是"全部"。

该错误是因为 url 上不存在具有address名称的此类参数,因此$_GET['address']不存在并且无法获取最终导致错误的值。如果没有收到请求,请给它一些值:

<?php
require ("db.php");
$add = isset( $_GET['address'] ) ?  $_GET['address'] : 'All';
if ($add=="All") 
{
  $gets = mysql_query("SELECT * FROM customers");
}
else
{
  $gets = mysql_query("SELECT * FROM customers WHERE cusadd='$add'");
}