THT DUYURU

 
 
Web Programming Section where Web programming languages are shared

Seçenekler

Launch PHP without rêfreshing page with AJAX

Gauloran - ait Kullanıcı Resmi (Avatar)
Kadim Üye
Üyelik tarihi:
07/2013
Nereden:
local
Mesajlar:
7.290
Konular:
1663
Teşekkür (Etti):
656
Teşekkür (Aldı):
1504
Ticaret:
(0) %
0
499
06-12-2020 22:28
#1
In some cases, we may have to launch PHP without *******ing the page. One of these examples is the "Add to Cart" feature on E-commerce websites. In this post, you'll learn how to make transactions without updating the page using Jquery ajax.



you can copy and paste jQuery codes into your javascript file.

AJAX

general use:

Kod:
$.ajax({
      type: "POST", // metod
      url: 'dosya.php', // POST edilecek php dosyası
      data: {post_adi:dosyaya_post_edilecek_bilgi},
      success: function (phpden_gelen_bilgi){
          // başarılı ise yapılacaklar
      },
});


Basket Application

What are we going to do in the basket app?

HTML: we'll give data-id to the Div of each product and write the ID of the product in it. Later in our JS file, we'll pull the data with attr("data-id").
PHP: we'll create an array with Session and keep the Ids of the products in this array.
JQuery: click on the data-id of the product and add it to the session in the PHP file without rêfreshing the page with ajax.



PAGES

products.php
in the $ urun_id variable, you need to assign the product ID to it when you pull the products in the database and loop the data with "foreach". I write $urun_id variable "14092020" to av0id dealing with the database.

Kod:
<?php
  if(!isset($_SESSION['sepet'])) {
    $_SESSION['sepet'] = array();
  }
?>

<div class="urunler" data-id="<?php echo $urun_id ?>">
  <div class="urun_ayrinti">
    // buraya ürünün görseli, fiyatı, puanı vb. vb. gelecek
  </div>
  <button id="sepete_ekle">Sepete Ekle</button>
</div>




sepet.js

attr ("data-id");
we pull out the ID of the product and add it to the basket with ajax.

Kod:
$( documen-t ).ready(function() {

  $('.urunler').on('click', '#sepete_ekle', function() {
    var id = $(this).parents(".urunler").attr("data-id");
    alert(id);
    $.ajax({
      type: "POST",
      url: 'sepete_ekle.php',
      data: {urun_id:id},
      success: function (){
       alert(id);
      },
    });
  });


});


sepete_ekle.php

Here, we'll transfer the data from Ajax to the array that we created with session.

Kod:
<?php

  session_start();
  $_SESSION["sepet"][] = $_POST["urun_id"];

?>


let's create a sepet_goruntule.php file to check the operations we do. and let's write these codes into it.

Kod:
<?php

  session_start();
  print_r($_SESSION["sepet"]);

?>
c

That's it. You can edit the codes yourself. Click the button on the homepage and click the sepet_goruntule.php file, check the array in the php file. The ID of the product will be added.







Source: https://www.turkhackteam.org/web-pro...ygulamasi.html
Çevirmen/Translator @Gauloran
--------------------- more than you know
Konu Provido tarafından (06-12-2020 22:33 Saat 22:33 ) değiştirilmiştir.
- Teşekkür etti.

Bookmarks


« Önceki Konu | Sonraki Konu »
Seçenekler