Launch PHP without rêfreshing page with AJAX

Gauloran

Global Moderatör
7 Tem 2013
8,187
633
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.

4dKDtQ.png


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
      },
});

4dKDtQ.png


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.

4dKDtQ.png


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>

tr9aSD.png


4dKDtQ.png


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);
      },
    });
  });


});

4dKDtQ.png


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"];

?>

4dKDtQ.png


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.

Tdancq.png


Tdancq.png


CckryH.gif


Source: https://www.turkhackteam.org/web-pr...den-php-calistirmak-php-sepet-uygulamasi.html
Çevirmen/Translator Gauloran
 
Moderatör tarafında düzenlendi:
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.