1

I would like to push JavaScript value into php:

var temp =  $('#field_id').val().charAt(0);

temp returns values 1-4.

var value = "<?php echo $variable[   .....    ]['id'] ;        ?>";

how to insert instead .....

Klapsius
  • 3,273
  • 6
  • 33
  • 56
  • 3
    PHP is processed by your server before the page load (= server side language), the result of the PHP process is sent through HTTP to your browser, where client side language belong (Javascript). If you want to communicate to PHP through Javascript you need to consider using AJAX. Tell us more about what you want to achieve here so we can point you in the right direction. – vard May 18 '15 at 14:14
  • @vard php values are stored into array and I need to take one value by select. I need to do this on client side – Klapsius May 18 '15 at 14:19
  • So if I understand right, you want to build a select based on a PHP Array, then update that select using Javascript ? – vard May 18 '15 at 14:22
  • i recommend to you learn more about application layers – daremachine May 18 '15 at 14:25
  • @vard php values already stored in array. I would like to tell javascript what value take back. – Klapsius May 18 '15 at 14:28
  • possible duplicate of [How to pass JavaScript variables to PHP?](http://stackoverflow.com/questions/1917576/how-to-pass-javascript-variables-to-php) – psiyumm May 18 '15 at 14:28
  • If you want to get your PHP array in Javascript, you could use `each` function of jQuery to loop through the options of the select you built from php. See https://api.jquery.com/each/ – vard May 18 '15 at 14:39
  • Please rephrase your question so that it's clear what you need. What is the use of `temp` and `value` variables? Perhaps you want to assign a PHP value to a JS variable? – adriann May 18 '15 at 15:12

1 Answers1

2

Its impossible, the main reason is that Php is rendered server side ( so before it arrives to the client computer), while JavaScript is rendered in the browser ( so in the client PC).

Depending from your requirement, you may use an ajax request to get the info and update the dom with javascript, or use GET or POST request with the js variable required so that PHP will have it before it renders the page.

If you wold give me more information, I may probably help you, but the question is not specific enough

Zelig880
  • 500
  • 4
  • 6
  • [It's not hard to earn enough rep to make comments.](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead) – Jay Blanchard May 18 '15 at 14:38
  • My variable temp returns only values 1, 2 , 3 , 4 and I wauld like integrate these values into php code and tell script exactly what array element pick up – Klapsius May 18 '15 at 14:46