Can I include a php file from within javascript and get a return variable from that php.I mean- <script type="text/javascript"> var data = <?php include('getData.php') ?> </script> Code (markup): Can anybody tell me how to do the above thing?
As long as the example code you have in your post is in a PHP file it should work. Although, the way I have done something similar is to use PRINT to print out the Javascript portion inside a PHP block in a PHP file. For example: print('<script type="text/javascript">'); print('var data = '); ** print out your data you were getting from getData.php ** print('</script>');
you have to know that php runs on the server to generate a page, and js on the browser, so js starts after php ends... you can generate js dynamic over php, but you couldn't generate php dynamic over js...
You will need to use JSON output from PHP. It will convert php data structure to javascript variable: <?php $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); echo json_encode($arr); ?> Code (markup): The above example will output: {"a":1,"b":2,"c":3,"d":4,"e":5} Купить землю/учаÑток в Киево-СвÑтошинÑком районе Купить землю/учаÑток в ВаÑильковÑком районе Купить землю/учаÑток в ВышгородÑком районе