how to get javascript variable value in php in same page?

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(); 
        } 
        }); 
        }); 
   }); 
    
 
 
 

Note: URL for this page must be domain-name.php?Id=someValue

How to pass JavaScript variables to PHP?

simply get the JavaScript variable value on the same page in PHP


document.writeln(res);";
?>

I hope you get an idea about how to assign jquery variable value to php variable in same page?.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment