- 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.
you can copy and paste jQuery codes into your javascript file.
AJAX
general use:
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.
sepet.js
attr ("data-id");
we pull out the ID of the product and add it to the basket with ajax.
sepete_ekle.php
Here, we'll transfer the data from Ajax to the array that we created with session.
let's create a sepet_goruntule.php file to check the operations we do. and let's write these codes into it.
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-pr...den-php-calistirmak-php-sepet-uygulamasi.html
Çevirmen/Translator Gauloran
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-pr...den-php-calistirmak-php-sepet-uygulamasi.html
Çevirmen/Translator Gauloran
Moderatör tarafında düzenlendi: