jtgmagic Posted September 21, 2014 Share Posted September 21, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/291207-ajax-passing-php-variable/ Share on other sites More sharing options...
gristoi Posted September 22, 2014 Share Posted September 22, 2014 $(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; }); }); }); Quote Link to comment https://forums.phpfreaks.com/topic/291207-ajax-passing-php-variable/#findComment-1491791 Share on other sites More sharing options...
mac_gyver Posted September 22, 2014 Share Posted September 22, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/291207-ajax-passing-php-variable/#findComment-1491792 Share on other sites More sharing options...
jtgmagic Posted September 22, 2014 Author Share Posted September 22, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/291207-ajax-passing-php-variable/#findComment-1491803 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.