Skip to content
Advertisement

Ajax call dynamically loaded rows

I have dynamically loaded(based on search result) content. (see the following)
(stackoverflow is not allowing me to embed the image as I am still new)
https://i.imgur.com/WVVc0wM.png

Code for the above;

JavaScript

I want to pass sid, classID to a seperate php file called add-student.php

JavaScript

The following is add-student.php

JavaScript

When I click ‘Add’ button for first time, it successfully adds to the database.
But when I click ‘Add’ button in a different row, I am getting The record already exists error.
Please give your advises.

Advertisement

Answer

Add the required data to pass on the element directly via data-attributes for $(".sid").val() will only ever get you the value of the first element based on the html you supplied.

When the first value is always the one being passed, it is expected that your checks on your backend will notify you that the record already existed.

You can change your markup to this:

JavaScript

Then listen on the click event and send the ajax

JavaScript

https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement