Jump to content

Recommended Posts

Hello I'm trying to pass a php variable from produto.php to another file descProduto.php it ajax but without success. Please someone can tell me what I'm doing wrong? The ajax is working fine but I can't get the value/variable on descProduto.php

file index.php

<script type="text/javascript" src="jquery-1.11.min.js"></script>
<script type="text/javascript" src="ajax.js"></script>
 

This is where I click file produto.php

<img class="btn-details" src="plus.png" data-idproduto="'.$idproduto.'"/>

 

My ajax file ajax.js

$(function(){
$(".btn-details").on('click', function(){

var idproduto = $(this).data('idproduto');

$.ajax({
type: "POST",
url: "descProduto.php",
async: false,
dataType: "html",
data: {'idproduto': idproduto},
success: function(result){

console.log("success");

console.log(idproduto);

},
error: function(){

console.log("error");

}
});
return false;
});
});

 

Where I get the variable file descProduto.php

if(isset($_POST['idproduto'])){

$idproduto = $_POST['idproduto'];

echo $idproduto;
}

 

I have print_r($_POST) and print a empty array()

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/291207-ajax-passing-php-variable/
Share on other sites


$(document).ready(function(){
$(function(){
$(".btn-details").on('click', function(){

var idproduto = $(this).data('idproduto');

$.ajax({
type: "POST",
url: "descProduto.php",
async: false,
dataType: "html",
data: {'idproduto': idproduto},
success: function(result){
console.log("success");
console.log(idproduto);
},
error: function(){
console.log("error");
}
});
return false;
});
});
});

your code works as expected for me, with a slightly different jquery version. are you sure the <img ...> tag you are clicking on has a data-idproduto='...' attribute in it?

 

about the only way you could get an empty $_POST array would be if var idproduto = $(this).data('idproduto'); doesn't result in any value.

 

what are you getting in your browser's console log?

that is stange part in my website don't work.

Yes the img tag where I click have the attribuite on it. 

console.log print the product id and success the problem is in descProduto.php is not geting the value (product id) don't understand why.

any ideias?

 

Thanks 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.