This question already has an answer here:
Hi i have made a php function that has to be called from script tag since the value is generated from javascript. In short i have to execute php function dynamically in one page itself. The think i can achieve is that i can call a function with static parameters such as
$(".countryoptions").click(function(){
console.log($(this).val());
console.log(<?php echo json_encode(getRegions(NULL,NULL));?>);
console.log(<?php echo json_encode(getRegions(NULL,"$(this).val()"));?>);
});
The first two values are been printed as expected but the third makes me sad, values are been passed as string ("$(this).val()") itself not the value. It may be stupid but is there any way to encounter this problem without POST request.
Thanks in advance.
</div>
Javascript is a client-side language, and PHP is server-side. PHP has no control over the page elements once the page has been loaded. What you're trying to do now is echo the values in your console using <?php
tags. This will not work.
If you want to access the PHP variables in your JS code, you can use a GET
request or even POST
.