Today, We want to share with you how to get javascript variable value in php in same page.In this post we will show you pass javascript variable to php same page ajax, hear for passing javascript variable to php in the same page we will give you demo and example for implement.In this post, we will learn about how to pass value from one page to another in php with an example.
how to store javascript value in php variable?
You can’t get a js variable directly in PHP server code, you have create a PHP HTTP request with data (Like as a as your $.ajax() request above) after that use $_POST or $_GET in PHP to fetch the value.
first of all include jquery library file in HTML Code.
HTML Code
Javascript
$(document).ready(function(){ var url = window.location.href; var all_datas = url.split('?ID='); var id = (all_datas[1]); $.ajax({ type:"POST", url:"page.php", data:{id:id}, success:function(outputs){ $("#results-display").html(outputs); } }); });
PHP : domain-name.php
Complete One page code:
domain-name.php
ipt> $(document).ready(function(){ var url = window.location.href; var all_datas = url.split('?ID='); var id = (all_datas[1]); $("#productsave").click(function(){ $.ajax({ type:"POST", url:"demo.php", data:{id:id}, success:function(outputs){ $("#results-display").html(outputs); $("#productsave").hide(); } }); }); });