-2

I am trying to have mysql data displayed on a bootstrap modal and for some reason it is only pulling the ID of the first link that is clicked and not the ID for the row specified on the link clicked to display said modal. (hope that makes sense).

The link to be clicked:

 <a data-toggle="modal" data-target="#leadModal" href="?id=<?php echo $id; ?>"><?php echo $f; echo '&nbsp;'; echo $l; ?></a>

Here is the code that surrounds the link:

 <div class="table-responsive">
        <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
          <thead>
            <tr>
              <th>Name</th>
              <th>Email Address</th>
              <th>Primary Phone</th>
              <th>Date Created</th>
              <th>Arrival Date</th>
              <th>Departure Date</th>
            </tr>
          </thead>
          <tfoot>
            <tr>
              <th>Name</th>
              <th>E-Mail Address</th>
              <th>Primary Phone</th>
              <th>Date Created</th>
              <th>Arrival Date</th>
              <th>Departure Date</th>
            </tr>
          </tfoot>
          <tbody>
          <?php
$agent = $userRow['userName'];

$query = "SELECT * FROM leads WHERE status='Follow-Up' && agent='$agent'";
$result = mysql_query($query) or die(mysql_error());
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { 
$f = $line['first'];
$l = $line['last'];
$e = $line['email'];
$p1 = $line['PrimePhone1'];
$p2 = $line['PrimePhone2'];
$p3 = $line['PrimePhone3'];
$a = $line['arrival'];
$d = $line['departure'];
$ag = $line['agent'];
$id = $line['id'];
$dc = $line['datecreated'];
$es = $line['emailsent'];
?>
            <tr>
              <td><a data-toggle="modal" data-target="#leadModal" href="?id=<?php echo $id; ?>"><?php echo $f; echo '&nbsp;'; echo $l; ?></a></td>
              <td><?php echo $e; ?></td>
              <td><?php echo $p1; echo '-'; echo $p2; echo'-'; echo $p3; ?></td>
              <td><?php echo $dc; ?></td>
              <td><?php echo $a; ?></td>
              <td><?php echo $d; ?></td>
            </tr>
            <?php } ?>
          </tbody>
        </table>
      </div>
    </div>

And here is what I have for code to display the data in the modal:

<!-- Lead Modal-->
    <div class="modal fade" id="leadModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header"><?php
          $id = $_GET["id"];
           $query = "SELECT * FROM leads WHERE id=$id";
$result = mysql_query($query);
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>
            <h5 class="modal-title" id="exampleModalLabel"><?php echo $line['first']; ?>&nbsp;<?php echo $line['last']; ?>'s Existing Lead</h5><?php } ?>
            <button class="close" type="button" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">×</span>
            </button>
          </div>
          <div class="modal-body"><?php

$id = $_GET["id"];
$query = "SELECT * FROM leads WHERE id=$id";
$result = mysql_query($query) or die(mysql_error());
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { 

?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr align="center">
    <td>
<table width="750" border="2" align="center" cellpadding="1" cellspacing="0" class="table">

  <tr>    
        <td>Original Agent:</td>
    <td><?php echo $line['agent']; ?></td>
  </tr>
 <?php if ($line['upagent'] == ''){  } else { ?>   <tr>    
        <td>Last Updated By:</td>
    <td><?php echo $line['upagent']; ?></td>
  </tr><?php } ?>
  <tr>
    <td width="186">First Name:</td>
    <td width="552">
      <?php echo $line['first']; ?></td>
  </tr>
<?php if ($line['last'] == ''){  } else { ?>  <tr>
    <td>Last Name:</td>
    <td><?php echo $line['last']; ?></td>
  </tr><?php } ?>
  <?php if ($line['email'] == ''){  } else { ?><tr>
    <td>E-Mail Address:</td>
    <td><?php echo $line['email']; ?></td>
  </tr><?php } ?>
  <tr>
    <td>Primary Phone:</td>
    <td><?php echo $line['PrimePhone1']; echo '-'; echo $line['PrimePhone2']; echo '-'; echo $line['PrimePhone3']; ?></td>
  </tr>
 <?php if ($line['AltPhone1'] == '' || $line['AltPhone2'] == '' || $line['AltPhone3'] == '') {  } else { ?> 
<tr>
    <td>Alternate Phone:</td>
    <td><?php echo $line['AltPhone1']; echo '-'; echo $line['AltPhone2']; echo '-'; echo $line['AltPhone3']; ?></td>
  </tr>
<?php } ?>
  <tr>
    <td>Lead Created:</td>
    <td><?php echo $line['datecreated']; ?></td>
  </tr>
<?php if($line['emailsent'] == 1){  ?> <tr>
    <td>Follow-Up Email Sent:</td>
    <td><?php echo $line['followupsent']; ?></td>
  </tr>
  <?php } ?>
  <tr>
    <td>Arrival Date:</td>
    <td><?php echo $line['arrival']; ?></td>
  </tr>
  <tr>
    <td>Departure Date:</td>
    <td><?php echo $line['departure']; ?></td>
  </tr>
  <?php if ($line['adults'] == ''){  } else { ?>
  <tr>
   <td>Adults:</td>
    <td><?php echo $line['adults']; ?></td>
  </tr><?php } ?>
 <?php if ($line['child'] == ''){  } else { ?>
  <tr>
    <td>Children:</td>
    <td><?php echo $line['child']; ?></td>
  </tr>
<?php } ?>
  <?php if ($line['area'] == ''){  } else { ?>
 <tr>
    <td>Requested Area:</td>
    <td><?php echo $line['area']; ?></td>
  </tr>
<?php } ?>
  <?php if ($line['budget'] == ''){  } else { ?>
 <tr>
    <td>Budget:</td>
    <td><?php echo $line['budget']; ?></td>
  </tr>
<?php } ?>
  <?php if ($line['suggested'] == ''){  } else { ?>
 <tr>
    <td>Suggested Properties:</td>
    <td><?php echo $line['suggested']; ?></td>
  </tr>
<?php } ?>
  <?php if ($line['discount'] == ''){  } else { ?> 
<tr>
    <td>Discount Offered:</td>
    <td><?php echo $line['discount']; ?></td>
  </tr>
<?php } ?>
  <?php if ($line['promo'] == ''){  } else { ?> 
<tr>
    <td>Promo Code:</td>
    <td><?php echo $line['promo']; ?></td>
  </tr>
<?php } ?>
  <tr>
    <td>Lead Status:</td>
    <td><?php echo $line['status']; ?></td>
  </tr>
  <?php if ($line['notes'] == ''){  } else { ?>     
<tr>
    <td height="85">Additional Notes:</td>
    <td><?php echo $line['notes']; ?></td>
  </tr>
<?php } ?>
 <?php if ($line['resnum'] == ''){  } else { ?>  
<tr>
    <td>Reservation Number:</td>
    <td><?php echo $line['resnum']; ?></td>
</tr><?php } } ?>


</table>

    </td>
  </tr>
</table>

</div>
          <div class="modal-footer">
            <button class="btn btn-secondary" type="button" data-dismiss="modal">Close</button>
            <a class="btn btn-primary" href="#">Button</a>
          </div>
        </div>
      </div>
    </div>

Any help is greatly appreciated.

Thanks, Scott

Scott Gurney
  • 3
  • 1
  • 4
  • Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Jeff Jan 06 '18 at 22:07
  • I don't think so. Please see the updated question. Thanks! – Scott Gurney Jan 06 '18 at 22:15
  • there is only _one_ modal, that will be shown whatever link/row you click. Is the page reloading? – Jeff Jan 06 '18 at 22:22
  • No the idea was to not have to refresh or load a new page. Each link clicked would display the data for the ID associated with that data row. – Scott Gurney Jan 06 '18 at 22:36
  • Actually yes, when you click the link the first time it does reload the page with the URL containing the ID of whichever one you click. After that initial link is clicked it doesn't matter which link you click on it will only load the data for the very first one that you clicked until you reload the page and click another link. – Scott Gurney Jan 06 '18 at 22:45
  • 4
    **WARNING**: If you're just learning PHP, please, do not use the [`mysql_query`](http://php.net/manual/en/function.mysql-query.php) interface. It’s so awful and dangerous that it was removed in PHP 7. A replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and a guide like [PHP The Right Way](http://www.phptherightway.com/) explains best practices. Your user data is **not** [properly escaped](http://bobby-tables.com/php.html) and there are [SQL injection bugs](http://bobby-tables.com/) and can be exploited. – tadman Jan 06 '18 at 23:19
  • 1
    @ScottGurney Your comments leads me back to my first comment about server side and client side. Either you have a modal for each and every row (not recommended) or you get the data via ajax, or you store the data in js and update the modal once you click on a row. – Jeff Jan 06 '18 at 23:22
  • Modals are part of the same page. AFAIK the link to activate them doesn’t refer to another page, does it? The modal has to get its information from Ajax if it’s going to change after the initial load. Or else reload the page which defeats the purpose of the modal. – Tim Morton Jan 06 '18 at 23:35

1 Answers1

0

What you're asking cannot be done in the current design. Your answer will be in looping your db results to create several modals (messy) or using AJAX to retrieve the modal data based on the link ID. A good example is in the answer here: Using Jquery Ajax to retrieve data from Mysql

Warren
  • 1,984
  • 3
  • 29
  • 60