How to pass variables and data from PHP to JavaScript?

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 Send variables between Javascript, Form and PHP on the same page:, hear for Passing Javascript variable to php on same page without reload we will give you demo and example for implement.In this post, we will learn about how to assign jquery variable value to php variable in same page? with an example.

Read Also:  Expand Collapse Div Plus Minus Using jQuery

How to pass JavaScript variables to PHP?

<script>
   var res = "file Uploaded Successfully!!";
</script>
<?php
   echo "<script>document.writeln(res);</script>";
?>

PHP variable to Javascript variable:

Example 1:

	<?php   $product_price=10;   ?>

	<script type="text/javascript">
		jsvar = <?php echo $product_price; ?>;
		document.write(jsvar);  // Test to see if its prints 10:
	</script>

Form variable to Javascript variable:

Example 2:

<form name="setProduct4">    <input type="hidden" name="product_price" value="100">     </form>

	<script type="text/javascript">
		jsvar = document.setProduct4.product_price.value;
		document.write(jsvar) // test
	</script>

PHP variable to Form variable:

Example 3:

<form name="setProduct4">
		<input type="hidden" name="product_price" value="<?php $phpvar=10; echo $phpvar; ?>"> // PHP code inside HTML!!
	</form>

Javascript variable to Form variable:

<form name="setProduct3">
		<input type="hidden" name="product_price" value="">
	</form>

	<script type="text/javascript">
		jsvar=10;
		document.setProduct3.product_price.value = jsvar;
	</script>

I hope you get an idea about How to pass variables and data from PHP to JavaScript ?.
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.