Bonjour,
Je débute dans le JS et JSON et je souhaiterais savoir comment actualiser le panier de mon site sans avoir a recharger la page.
J'ai regarder sur le net mais je ne comprends pas le principe pour le mettre en place sachant que les données à récupérer sont dans une session.
Voici le code :
ajout.js
ajout.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 $(document).ready(function(){ $('#ajout').submit(function(){ $.ajax({ type : "post", url : "traitement/ajout-produits.php", data : { 'l': $('#l').val(), 'p': $('#p').val(), 'q': $('#q').val(), 'v': $('#v').val(), 'n': $('#selvalue').val() }, dataType: 'json', success: function(response){ console.log(response); if(response.success){ $('#successmessage').text(response.success).slideDown().delay(2000).slideUp(); } if(response.errors){ $('#errormessage').text(response.errors).slideDown().delay(2000).slideUp(); } } }); return false; }); });
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 $erreur = false; $errors = []; if(empty($_POST['n'])){ $erreur = true; array_push($errors, 'Choisir un taux de nicotine'); $response = ['errors'=>$errors]; echo json_encode($response);exit; } $l = (isset($_POST['l'])? $_POST['l']: (isset($_GET['l'])? $_GET['l']:null )) ; $p = (isset($_POST['p'])? $_POST['p']: (isset($_GET['p'])? $_GET['p']:null )) ; $q = (isset($_POST['q'])? $_POST['q']: (isset($_GET['q'])? $_GET['q']:null )) ; $v = (isset($_POST['v'])? $_POST['v']: (isset($_GET['v'])? $_GET['v']:null )) ; $n = (isset($_POST['n'])? $_POST['n']: (isset($_GET['n'])? $_GET['n']:null )) ; $l = $l."".$n; $l = preg_replace('#\v#', '',$l); //On verifie que $p soit un float $p = floatval($p); if (is_array($q)){ $QteArticle = array(); $i=0; foreach ($q as $contenu){ $QteArticle[$i++] = intval($contenu); } } else $q = intval($q); if (!$erreur){ if (creationPanier() && !isVerrouille()) { $positionProduit = array_search($l, $_SESSION['panier']['libelleProduit']); if ($positionProduit !== false) { $_SESSION['panier']['qteProduit'][$positionProduit] += $q ; } else { array_push( $_SESSION['panier']['libelleProduit'],$l); array_push( $_SESSION['panier']['qteProduit'],$q); array_push( $_SESSION['panier']['prixProduit'],$p); array_push( $_SESSION['panier']['photoProduit'],$v); } } $success = []; array_push($success, 'produit ajouter au panier'); $response = ['success'=>$success]; echo json_encode($response);exit; }
Le bloc panier :
Code HTML : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 <div id="top-cart" class="header-misc-icon d-none d-sm-block"> <a href="#" id="top-cart-trigger"><i class="icon-line-bag"></i><span class="top-cart-number"><?php echo compterArticles(); ?></span></a> <div class="top-cart-content"> <div class="top-cart-title"> <h4>Panier</h4> </div> <div class="top-cart-items"> <?php $nbArticles=count($_SESSION['panier']['libelleProduit']); if ($nbArticles <= 0){ ?> <div class="top-cart-item"> <div class="top-cart-item-image"> </div> <div class="top-cart-item-desc"> <div class="top-cart-item-desc-title"> <a href="#">Panier vide :(</a> </div> </div> </div> <?php }else { for ($i=0 ;$i < $nbArticles ; $i++) { ?> <div class="top-cart-item"> <div class="top-cart-item-image"> <a href="#"><img src="produits/<?php echo $_SESSION['panier']['photoProduit'][$i]; ?>.jpg" alt="<?php echo $_SESSION['panier']['libelleProduit'][$i]; ?>" /></a> </div> <div class="top-cart-item-desc"> <div class="top-cart-item-desc-title"> <a href="#"><?php echo htmlspecialchars($_SESSION['panier']['libelleProduit'][$i]); ?></a> <?php $total = htmlspecialchars($_SESSION['panier']['prixProduit'][$i]) * htmlspecialchars($_SESSION['panier']['qteProduit'][$i]); ?> <span class="top-cart-item-price d-block"><?php echo number_format($total, 2, ',', ' '); ?></span> </div> <div class="top-cart-item-quantity">x <?php echo htmlspecialchars($_SESSION['panier']['qteProduit'][$i]); ?></div> </div> </div> <?php } ?> <?php } ?> </div> <?php if ($nbArticles <= 0){}else{ ?> <div class="top-cart-action"> <span class="top-checkout-price"><?php echo number_format(MontantGlobal(), 2, ',', ' '); ?></span> <a href="#" class="button button-3d button-small m-0">Voir le panier</a> </div> <?php } ?> </div> </div>
Merci pour vos lumières.
Partager