0

Update:

The duplicate answer link posted deals with binding click events to dynamically generated content - which helped, thanks. But I'm still unsure of how to pass the $row ID of dynamically generated content to a dynamically generated modal using a click event within an AJAX statement?

So essentially, where/how does the $row id go in:

<a href="#dataModal"><img class="photog-headshot" 
src="images/'+ data.Surfboards[row].Surfboard.imageName + '" alt="' + 
data.Surfboards[row].Surfboard.imageName + '"></a>

Problem:

I'm working with code from the tutorial below and incorporating it into my existing project and I'm stuck. I'm not triggering the .view_data from the href on line 18 in surfboard.js so when a surfboard placeholder image is clicked at the bottom of of the page on Nalu.live/equipment nothing from the database appears in the modal. The functionality I want is working in the 'Surfboard Name' table above the image grid. Click on the "View" button to see it in action.

I'm also getting a weird Unrecognized expression console log error?

Helpful links:

Main Project Files for this Issue:

equipment.php, select.php, surfboards.php, surfboard.js

Notes:

A working example is on nalu.live.equipment in the "Surfboard Name" table on the page below the carousel. If you click on the 'View' button a modal opens with the corresponding name of the surfboard row and image that are loaded from the SQL database. The code to generate the modal is in select.php.

I'm trying to incorporate the same functionality from the table into the image grid below it so I can remove the example table. When the user clicks on a surfboard placeholder image the corresponding modal opens with info that matches the database ID for that image.

The image grid is being generated by JSON/JS in surfboards.js. The surfboards.php file is creating an associative array and echoing JSON. The JS in surfboards.js is looping through the data and appending a div to #board_table in equipment.php.

The modal link is on line 18 of surfboard.js. I can't sort out the syntax to trigger the .view_data function to include the info from the DB. I've added the class="view_data" to the href but that's obviously wrong.. I'm unsure how to open the modal and trigger the click().

Image Grid HTML - added class=".view_data" to bind click event:

      <div class="flexbox_container">
        <section class="view_data" id="board_table">
          <!-- surfboards.js will publish surfboards from database here -->
        </section>
      </div>

Modal Code for Both Surfboard Table and Placeholder Images:

<!-- Ajax Database Modal Start -->
      <div id="dataModal" class="modal fade">  
        <div class="modal-dialog">  
          <div class="modal-content">  
            <div class="modal-header">  
              <button type="button" class="close" data-dismiss="modal">&times;</button>  
              <h4 class="modal-title">Surfboard Details</h4>  
            </div>  
            <div class="modal-body" id="surfboard_detail">  
            </div>  
            <div class="modal-footer">  
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>  
            </div>  
          </div>  
        </div>  
      </div> 

JSON Parser - surfboard.js:

//gets the JSON from our surfboards.php
$.getJSON("surfboards.php", function (data) {
  //loop through each surfboard in the JSON file and append a <div> with the surfboard information
  $.each(data.Surfboards, function (row) {
      $("#board_table").append(
        '<div class="photog-group clearfix"><figure class="cap-bot"><a href="equipment.php?id=' 
        + data.Surfboards[row].Surfboard.id 
        + '"><a href="#dataModal" class="view_data"><img class="photog-headshot" src="images/'
        + data.Surfboards[row].Surfboard.imageName + '" alt="'
        + data.Surfboards[row].Surfboard.imageName
        + '"></a><figcaption><p>Board Name: ' 
        + data.Surfboards[row].Surfboard.boardName 
        + '<br>Year Shaped: ' 
        + data.Surfboards[row].Surfboard.year + '</p></figcaption><figure></div>');
  });
});

AJAX View Data Trigger:

// Function that responds to 'View' button onclick on equipment.php and 
inserts information from database into modal id="dataModal" 
$(document).ready(function(){  
  $('.view_data').click(function(){  
     var surfboard_id = $(this).attr("id");  
     $.ajax({  
         url:"../select.php",  
         method:"post",  
         data:{surfboard_id:surfboard_id},  
         success:function(data){  
             $('#surfboard_detail').html(data);  
             $('#dataModal').modal("show");
             console.log(data);
         }  
     });  
  });  
});
SDillon
  • 127
  • 1
  • 11
  • 1
    Please read: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and also [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) - You need to debug your code, narrow down the issue and add all the relevant code into the question itself, not as a link off-site. – M. Eriksson Aug 14 '18 at 05:49
  • Sorry about that @magnuseriksson, I added the code itself above. As mentioned in my second line, the issue is I'm not triggering the `.view_data` function from the `href` on line 18 in `surfboard.js` so when a surfboard placeholder image is clicked nothing shows up in the modal. – SDillon Aug 14 '18 at 06:07
  • @MagnusEriksson thanks for the link, it helped with binding the click event, but I'm still unsure of how to pass the ID of a dynamically generated modal to the click event? I've updated my question. – SDillon Aug 14 '18 at 06:28
  • When asking a question about a problem caused by your code, you will get much better answers if you provide code people can use to reproduce the problem. That code should be… **Minimal** – Use as little code as possible that still produces the same problem. – georgeawg Aug 14 '18 at 22:58
  • @georgeawg apologies, I was trying to supply as much information as possible to so that whomever tried to answer would have everything they need. I will re-ask this question with less text and more succinct code. – SDillon Aug 15 '18 at 04:15
  • @MagnusEriksson I solved this problem and posted the code in my updated question here [Pass Image ID - Array Index - to Modal from JSON](https://stackoverflow.com/a/54045254/4043235) in case it's helpful to anyone else. If you reopen this question I will answer it here too..? – SDillon Jan 04 '19 at 20:04

0 Answers0