1

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.

3
  • 4
    The URL is using id, while PHP is using pid. Might be a typo when posting here, so I wont offer it as an answer unless of course it is the problem. Commented Sep 22, 2013 at 10:18
  • Why don't put a link on your button and use PHP to create your link with pid? Commented Sep 22, 2013 at 10:22
  • $_GET["pid"] should be $_GET["id"] as you are passing "id" not "pid" ..? is that a typo..? Commented Sep 22, 2013 at 10:24

2 Answers 2

2

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);
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks David. I didnt notice it.
@Sergio What will be the problem, if changing in js? Is there any performance issue?
Not me downvoting btw
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.