0

Good day everyone!

I'm trying to load an external php file using jQuery and I'm getting an undefined index because clearly I'm using a GET method in my external php file.

Here is my code:

main file

<script>
$(function() {
  $('#loadClients').load('clientsTable.php');
});
</script>

<body>
<div id="loadClients"><!-- load here--></div>
</body>

clientsTable.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>

In my main file, the sample url is localhost/accountsknc/main.php?address=All. The error/notice I'm getting is Notice: Undefined index: address because maybe it's an external file.

Is there a way to handle this so that the data of the clients will show up in my main file? Thank you in advance.

PS: I know that my code is vulnerable because mysql is getting deprecated but I'm only testing this locally and will use PDO when I will implement it already.

wobsoriano
  • 12,348
  • 24
  • 92
  • 162

2 Answers2

1

The error because no such params with address name exist on url, thus $_GET['address'] are not exist and can't get the value which will end up with error. Give it some value if did't get the request :

<?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'");
}
Norlihazmey Ghazali
  • 9,000
  • 1
  • 23
  • 40
  • With your code, whatever address is equal to, it becomes "All" – wobsoriano Mar 01 '16 at 02:39
  • No, it should not. How you call it? Can you post the mentioned url – Norlihazmey Ghazali Mar 01 '16 at 02:41
  • If it's main.php?address=All it should show all the clients, but for example if it's main.php?address=Alabama, it should only show clients from Alabama – wobsoriano Mar 01 '16 at 02:46
  • If i understand your case correctly, right now you have `main.php` page that call the page `clientsTable.php` by using jquery load. And the url of current browsing for example is `main.php?address=Albama` and you want this `Albama` value available inside the code you called from jquery load right? – Norlihazmey Ghazali Mar 01 '16 at 02:57
  • Yes, something like that. – wobsoriano Mar 01 '16 at 02:58
  • Then, it does not work. The page you called from jquery only react the url from this `$('#loadClients').load('clientsTable.php');` call. Not the url of current browsing page. You get it? – Norlihazmey Ghazali Mar 01 '16 at 03:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/104942/discussion-between-fewflyby-and-norlihazmey-ghazali). – wobsoriano Mar 01 '16 at 03:03
1

You need to make a small modification to these two lines.

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

To

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

Therefore, if the $_GET['address'] is not set, it'll look for all address as if your $_GET['address'] is "All".

Chin Leung
  • 14,621
  • 3
  • 34
  • 58