0

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.

PRASANTH
  • 695
  • 3
  • 15
  • 33
  • Not sure if this is what you were asking but change the double quotes to single quotes. The dollar symbol gets executed as a variable in double quotes unless you escape it or use single quotes instead of double. – Danny Aug 03 '13 at 04:22
  • 1
    Use a GET request, but seriously the php code is executed on the server before the browser even gets the js to execute. – Musa Aug 03 '13 at 04:23
  • Musa... my point for the question is i can call a php function but why cant i can call with javascript generated value. – PRASANTH Aug 03 '13 at 04:28
  • ok got it... The value which is printed is been computed previously and the value generated will replace the php tags .. Sorry for vague question and thanks Dvorak for getting me clear. – PRASANTH Aug 03 '13 at 04:57

1 Answers1

1

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.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150