0

I have a PDO object with a certain number of DB entries. I'm looping through every result generating HTML content. Furhtermore, if a user is logged in, I am displaying extra buttons to edit and delete the content, like so:


if (isset($_SESSION['login']) and $_SESSION['idMembre'] == $id_utilisateur) { ?>

<br>
<a class="btn btn-outline-secondary btn-sm" href="" id="modify" data-toggle="modal" data-target="#modifierRecette" data-id="<?=$recette->idRecette;?>" onclick="modifierRecette(<?= $recette->idRecette; ?>)">Modifier</a>
<span>|</span>
<a class="btn btn-outline-danger btn-sm" href="" value="submit" onclick="supprimerRecette(<?=$recette->idRecette;?>)">Supprimer</a>

<?php } ?>

When the user clicks on "Modifier", a modal pops-up. Idea is that user can change some data inside the modal and click on a "Save Changes" button to commit the changes to the DB.

However, I have a hard time accessing to my PDO loop-through from within the modal.

<div class="modal-footer">
  <button type="button" class="btn btn-secondary" data-dismiss="modal">Annuler</button>
  <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="sauvegarderRecette(<?= $recette->idRecette; ?>)">Sauvegarder</button>
</div>

Idea being in every loop-through, the function "sauvegarderRecette()" would get the unique ID of the data object I am working with. This in turn fires off an AJAX script to send the data to the DB:

function sauvegarderRecette(idRecette) {
        let request = $.ajax({
            'url' : 'ajax/sauvegarderRecette.php',
            'type' : 'POST',
            'data' : {
                'idRecette' : idRecette 
            }
});

The issue now is that every loop-through, the ajax function sends off the same idRecette to the PHP script.

Any ideas on how I can dynamically access variable from JavaScript so it 'preserves' the value from the loop-through?

I thank you in advance for your help.

Dananjaya
  • 2,135
  • 8
  • 22
  • 27
  • 1
    Could simply set a [`data-id`](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes) attribute on the elements, reference that client side? Broader discussion here: https://stackoverflow.com/questions/1917576/how-to-pass-javascript-variables-to-php – ficuscr May 23 '19 at 19:59
  • Could you show your loop for sauvegarderRecette() function? – tcj May 23 '19 at 20:04
  • @ficuscr thanks for the link. I did add a data attribute on the elements but from within the modal it gives me the id from the first iteration of the loop. JavaScript doesn't seem to get the updated values for subsequent loops :( – Dananjaya May 23 '19 at 20:05
  • 1
    @Dananjaya ok I think this might be a closure issue, please look at this similar question : https://stackoverflow.com/questions/20333133/jquery-ajax-loop-and-iteration-scope – tcj May 23 '19 at 20:07
  • Guess I am not seeing enough of the PHP code. See no loop. Not sure why your primary key is not in scope. Are labels the same over and over... `= $recette->nom ?>` or whatever? – ficuscr May 23 '19 at 20:07
  • @tcj here's the sauvegarderRecette() file : https://github.com/dananjayavr/projet-1-hitema-front/blob/master/ajax/sauvegarderRecette.php I added some echo statements for debugging purposes. And here you can find the complete code responsible for displaying the data : https://github.com/dananjayavr/projet-1-hitema-front/blob/master/membre-detail.php Thank you for your time – Dananjaya May 23 '19 at 20:09

0 Answers0