1

I'm trying to use a JS variable inside of php code that executes a function. Based on the selected radio button, Javascript gets a string of code from a data attribute of the radio button (trimcode) and I need to set this as the key in my PHP function where one of the variables requires an array key.

The result will be, whenever a radio button is selected, the PHP function inside this function will execute using the 'code' variable and thus return it's return value as the inside content of the "byo" class div via javascript innerhtml. However, I can't figure out how to use a javascript variable inside of PHP code that is inside another javascript variable. Can someone point me in the right direction?

<script type='text/javascript'> 

$(document).ready(function(){
var code;
var colorsavailable;

$("input:radio[name=trimlevel]").click(function() {
    code = $(this).data("trimcode");
    colorsavailable = ("<?=byoTrimImageColors($modelMap["+code+"],$hexcolors)?>");
     $('#byo').html(colorsavailable);

  });
 });
});
</script>  
Josh979
  • 361
  • 4
  • 17
  • Here's a clue. Server PHP executes on the server. The result is a generated page. That page is sent to the browser where the Javascript can then execute. There is no PHP in the browser, only Javascript and HTML. So, think of the PHP as a pre-processing step that prepares the page to be sent to the browser. Then, the Javascript gets to run the result of the PHP. So, NO - PHP can't use Javascript variables. – jfriend00 Aug 14 '15 at 16:36
  • 1
    http://stackoverflow.com/questions/15286331/run-php-client-side – cviejo Aug 14 '15 at 16:37
  • So PHP can't be run inside a javascript code, because its the server that runs the php, not the browser? – Josh979 Aug 14 '15 at 16:37
  • @Josh979 did I adequately answer your question? If so, please accept my answer so that others who come to the post know what to take away from it. – rdgd Aug 13 '16 at 20:09

4 Answers4

2

I think you're confusing the notions of server-side code and client-side code.

The PHP code runs once, in its entirety, when the page is requested. Then, after it's executed, the resulting page is sent to the browser where the JavaScript code runs. And that line of PHP code isn't going to execute for each click event. It's going to execute once, and only once, and emit only one result to the page.

Given what it looks like you're trying to do, you probably have two options:

  1. Include the functionality you're invoking in JavaScript code and just invoke it there instead of in PHP code. Whatever that array is, whatever that function is, implement those in JavaScript. Then there's no need to involve PHP at all.
  2. If the functionality needs to be in server-side code, then you're going to need to use AJAX to invoke it. Essentially you'd create a separate PHP "page" (which emits JSON data, not an actual page) to accept the value, invoke the functionality, and emit the result. The JavaScript code would make an AJAX request to that resource and use the result.

Client-side, that AJAX call might look something like this:

code = $(this).data("trimcode");
$.post('someNewPage.php', { code : code }, function (result) {
    $('#byo').html(result);
});

Server-side (and I'm mostly guessing here), someNewPage.php might have something like:

$code = $_POST['code'];
// you'll want to sanitize the $code value here, since it's user input
$result = byoTrimImageColors($modelMap[$code],$hexcolors);
echo json_encode($result);
David
  • 208,112
  • 36
  • 198
  • 279
1

Your idea is not possible since for PHP code to execute it requires a page refresh. The best solution would be using Ajax to send the data to PHP asynchronously and changing your HTML, using JS, with the data that comes back from PHP.

To learn how Ajax/PHP works visit this site: http://www.w3schools.com/php/php_ajax_php.asp.

dnavas77
  • 98
  • 1
  • 8
1

It is not possible to call a PHP function from Javascript. PHP is a pre-processor and ALL PHP code will finish executing before the page loads. Once the page loads, the browser will load your Javascript files and JS will take it from there.

In order to "call a PHP function" again after the page has loaded, you will need to make a new request to the page. This is typically handled in Javascript via AJAX. Using ajax, Javascript can send a new request to the server, PHP will read it and send a response back to Javascript.

If you really need that PHP function to execute on the Javascript level synchronously, then consider porting that function to Javascript.

internet-nico
  • 1,637
  • 19
  • 17
1

Right now what you are doing is constructing a string that resembles some PHP code. It looks like you are doing that part just fine.

However, you cannot execute PHP code on the client side. There is no interpreter available on the client (the browser) that knows what to do with your PHP code.

What I would recommend doing is if you need to run some PHP code, create a controller method or some script that you can execute on the server, make an AJAX request to that (passing the JS code variable), have the PHP method return the value of your byoTrimImageColors method, and then handle that data however you please.

rdgd
  • 1,451
  • 2
  • 18
  • 33