Jump to content

Ajax passing php variable


jtgmagic

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 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.