user2290403
user2290403

Reputation: 11

send variable with ajax call and get in php

I want to sent a variable on an ajax call when I click on a button. The button has an id and on click of the button the id should pass through the ajax call to a php file

<button class="btn-view-products" id="mangoes">View Products</button>

and my js is

var gallery = $("#product-gallery"); 
var viewproducts = $('.btn-view-products');
viewproducts.click(function(){
pid = $(this).attr('id');
gallery.load("gallery.php?id="+ pid, hideLoading);
});

and in my php

$fname = $_GET["pid"];
$images_dir = "images/products/".$fname."/showcase/";

I am not getting the id value in php. please help. thanks a ton in advance.

Upvotes: 1

Views: 335

Answers (2)

David Houde
David Houde

Reputation: 4778

In your JavaScript, you are sending a request to gallery.php?id= where in your PHP script, you are looking for $_GET['pid']. One of them needs to change as they don't match.

Change:

gallery.load("gallery.php?id="+ pid, hideLoading);

To:

gallery.load("gallery.php?pid="+ pid, hideLoading);

OR Change:

$fname = $_GET["pid"];

To:

$fname = $_GET["id"];

Upvotes: 1

Anshad Vattapoyil
Anshad Vattapoyil

Reputation: 23463

You are passing id from js and accessing pid in php. So replace your js to,

var gallery = $("#product-gallery"); 
var viewproducts = $('.btn-view-products');
viewproducts.click(function(){
  pid = $(this).attr('id');
  gallery.load("gallery.php?pid="+ pid, hideLoading);
});

Upvotes: 2

Related Questions