Partage
  • Partager sur Facebook
  • Partager sur Twitter

Seul le premier bouton fonctionne dans une boucle

Sujet résolu
    23 septembre 2018 à 22:54:45

       Bonsoir à tous, voir bonjour. Je suis actuellement en train de créer un système de collection sur mon site et pour ce faire j'ai une boucle Php qui affiche chaque item en fonction d'un certain critère. J'ai ainsi rajouté pour chaque occurence des boutons "wish" "add" "remove" pour ajouter, supprimer ou mettre en litre de souhait. À quoi j'ai ajouté un script permettant lorsqu'on clique sur ces boutons (pour le moment uniquement wish) d'envoyer une requête sql via ajax et la méthode post. Cependant uniquement le premier bouton fonctionne sur le site. Pouvez vous m'aider à faire fonctionner les autres boutons ? 

    Voici mon code : 

    <?php try //BDD conexion
    {
    $bdd = new PDO('mysql:host=localhost;dbname=wordpress;charset=utf8', '', '');
    }
    catch(Exception $e)
    {
        die('Erreur : '.$e->getMessage());
    }?>
                                <script>
                                    var user_id = "<?php echo $user_id; ?>";
                                        jQuery("#wish").click(function(){
                                        var $el = jQuery(this);
                                        var post_id = console.log($el.data('id'));
                                        jQuery.ajax({
                                            url : 'https://popfr.fr/wp-content/plugins/ultimate-posts-widget/templates/wish.php',
                                            type : 'POST',
                                            data : 'user_id=' + user_id + '&post_id='+ post_id,
                                            dataType : 'html',
                                            success : function(){
                                                alert("Vous venez d'ajouter un objet à votre liste de souhait");
                                            },
                                            error : function(){
                                                alert("erreur")
                                            }
                                                    });
                                                });
                                </script>
    
    <!-- Fin de connexion à la BDD -->
    <?php if ($instance['before_posts']) : ?>
    <div class="upw-before">
        <?php echo wpautop($instance['before_posts']); ?>
    </div>
    <?php endif; ?>
    
    <div class="upw-posts hfeed">
    
        <?php if ($upw_query->have_posts()) : ?>
    
        <?php while ($upw_query->have_posts()) : $upw_query->the_post(); ?>
    
        <?php $current_post = ($post->ID == $current_post_id && is_single()) ? 'active' : ''; ?>
    
        <article <?php post_class($current_post); ?>
    
            <header>
    
                <?php if (current_theme_supports('post-thumbnails') && $instance['show_thumbnail'] && has_post_thumbnail()) : ?>
                <div class="entry-image">
                    <!-- Stickers -->
                    <?php $stickers_valeur = 0; ?>
                    <?php 
                        $post_tags = get_the_tags();
                        foreach( $post_tags as $tag) :
                        if ( $tag->name === 'chase') :
                            ?>
                    <img class="autocollant" src="https://popfr.fr/wp-content/uploads/2018/09/ChaseSticker.png" style="width:9%;">
                    <?php $stickers_valeur = 1; ?>
                    <?php 
                            elseif ( $tag-> name === 'Vaulted' ) :
                        ?>
                    <img class="autocollant" src="https://popfr.fr/wp-content/uploads/2018/09/VaultedSticker.png" style="width:9%;">
                    <?php $stickers_valeur = 1; ?>
                    <?php
                        elseif ( $tag->name === 'Exclusive' && $stickers_valeur==1) :
                            ?>
                    <img class="autocollant2" src="https://popfr.fr/wp-content/uploads/2018/09/excluSticker.png" style="width:9%;">
                    <?php 
                        elseif ( $tag->name === 'Exclusive' && $stickers_valeur!=1) :
                            ?>
                    <img class="autocollant" src="https://popfr.fr/wp-content/uploads/2018/09/excluSticker.png" style="width:9%;">
                    <?php
                        else :
                        endif;
                        endforeach
                            ?>
                    <a href="<?php the_permalink(); ?>" rel="bookmark">
                        <?php the_post_thumbnail($instance['thumb_size']); ?>
                    </a>
                </div>
                <?php endif; ?>
    
    
    
                <?php if (get_the_title() && $instance['show_title']) : ?>
                </a>
                <!-- Start add/wish/remove/item -->
                    <?php $post_id = get_the_ID(); ?>
                    <?php if ( is_user_logged_in() ): ?>
                        <?php $user_id = get_current_user_id();  ?>
                            <input type="image" data-id="<?php echo $post_id; ?>" src="https://popfr.fr/wp-content/uploads/2018/09/gift-box.png" id="wish">
                            <input type="image" src="https://popfr.fr/wp-content/uploads/2018/09/minus.png" id="remove">
                            <input type="image" src="https://popfr.fr/wp-content/uploads/2018/09/add-2.png" id="add">
                    <?php else : ?>
                    <?php endif ?>
    
                <h4 class="entry-title">
                    <a href="<?php the_permalink(); ?>" rel="bookmark">
                <a href="<?php $link ?>">
                 <!--   <img src="https://popfr.fr/wp-content/uploads/2018/09/acheter.png" class="buy_img">-->
    
                        
                        <?php the_title(); ?>
                        <hr id="separator">
                        <?php 
                                         
                                         $price_value_end = $bdd->query('SELECT prix_moyen FROM prix_moyen_table WHERE post_id="'.$post_id.'"');
                                while ($data_price = $price_value_end->fetch())
                                {
                                    echo '<a id="price">' . $data_price['prix_moyen'] . '.00€ </a>';
                                }
                                $price_value_end->closeCursor();
                                ?>
                    </a>
    
                </h4>
                <?php endif; ?>
    
                <?php if ($instance['show_date'] || $instance['show_author'] || $instance['show_comments']) : ?>
    
                <div class="entry-meta">
    
                    <?php if ($instance['show_date']) : ?>
                    <time class="published" datetime="<?php echo get_the_time('c'); ?>">
                        <?php echo get_the_time($instance['date_format']); ?></time>
                    <?php endif; ?>
    
                    <?php if ($instance['show_date'] && $instance['show_author']) : ?>
                    <span class="sep">
                        <?php _e('|', 'upw'); ?></span>
                    <?php endif; ?>
    
                    <?php if ($instance['show_author']) : ?>
                    <span class="author vcard">
                        <?php echo __('By', 'upw'); ?>
                        <a href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>" rel="author" class="fn">
                            <?php echo get_the_author(); ?>
                        </a>
                    </span>
                    <?php endif; ?>
    
                    <?php if ($instance['show_author'] && $instance['show_comments']) : ?>
                    <span class="sep">
                        <?php _e('|', 'upw'); ?></span>
                    <?php endif; ?>
    
                    <?php if ($instance['show_comments']) : ?>
                    <a class="comments" href="<?php comments_link(); ?>">
                        <?php comments_number(__('No comments', 'upw'), __('One comment', 'upw'), __('% comments', 'upw')); ?>
                    </a>
                    <?php endif; ?>
    
                </div>
    
                <?php endif; ?>
    
            </header>
    
            <body>
            </body>
            <?php if ($instance['show_excerpt']) : ?>
            <div class="entry-summary">
                <p>
                    <?php echo get_the_excerpt(); ?>
                    <?php if ($instance['show_readmore']) : ?>
                    <a href="<?php the_permalink(); ?>" class="more-link">
                        <?php echo $instance['excerpt_readmore']; ?>
                    </a>
                    <?php endif; ?>
                </p>
            </div>
            <?php elseif ($instance['show_content']) : ?>
            <div class="entry-content">
                <?php the_content() ?>
            </div>
            <?php endif; ?>
    
            <footer>
    
                <?php
                $categories = get_the_term_list($post->ID, 'category', '', ', ');
                if ($instance['show_cats'] && $categories) :
                ?>
                <div class="entry-categories">
                    <strong class="entry-cats-label">
                        <?php _e('Posted in', 'upw'); ?>:</strong>
                    <span class="entry-cats-list">
                        <?php echo $categories; ?></span>
                </div>
                <?php endif; ?>
    
                <?php
                if ($instance['show_tags'] && $tags) :
                ?>
                <div class="entry-tags">
                    <strong class="entry-tags-label">
                        <?php _e('Tagged', 'upw'); ?>:</strong>
                    <span class="entry-tags-list">
                        <?php echo $tags; ?></span>
                </div>
                <?php endif; ?>
    
                <?php if ($custom_fields) : ?>
                <?php $custom_field_name = explode(',', $custom_fields); ?>
                <div class="entry-custom-fields">
                    <?php foreach ($custom_field_name as $name) :
                      $name = trim($name);
                      $custom_field_values = get_post_meta($post->ID, $name, true);
                      if ($custom_field_values) : ?>
                    <div class="custom-field custom-field-<?php echo $name; ?>">
                        <?php
                          if (!is_array($custom_field_values)) {
                            echo $custom_field_values;
                          } else {
                            $last_value = end($custom_field_values);
                            foreach ($custom_field_values as $value) {
                              echo $value;
                              if ($value != $last_value) echo ', ';
                            }
                          }
                          ?>
                    </div>
                    <?php endif;
                    endforeach; ?>
                </div>
                <?php endif; ?>
    
            </footer>
    
        </article>
    
        <?php endwhile; ?>
    
        <?php else : ?>
    
        <p class="upw-not-found">
            <?php _e('No posts found.', 'upw'); ?>
        </p>
    
        <?php endif; ?>
    
    </div>
    
    <?php if ($instance['after_posts']) : ?>
    <div class="upw-after">
        <?php echo wpautop($instance['after_posts']); ?>
    </div>
    <?php endif; ?>



    • Partager sur Facebook
    • Partager sur Twitter
      24 septembre 2018 à 7:43:53

      Bonjour,

      un id doit être unique sur la page : tu as un élément d'id "wish" par objet, donc plusieurs fois le même id. Et JavaScript s'arrête au premier parce qu'il s'attend à ce qu'il n'y en ait qu'un :)

      Transforme ton id en classe et répercute le changement dans le JS, ça devrait suffire pour réparer.

      • Partager sur Facebook
      • Partager sur Twitter

      Pas d'aide concernant le code par MP, le forum est là pour ça :)

        24 septembre 2018 à 10:45:05

        Effectivement ça fonctionne parfaitement, merci beaucoup :)
        • Partager sur Facebook
        • Partager sur Twitter

        Seul le premier bouton fonctionne dans une boucle

        × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
        × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
        • Editeur
        • Markdown