Note that there are some explanatory texts on larger screens.

plurals
  1. POPass and update php variable into javascript
    text
    copied!<p>Is there a way I can pass the php option value variable $option_value['price'] into javascript as a string, then each time a different option is chosen, the javascript 'updates' the string with the new value. I have tried with onchange() but must be doing something wrong. I am sure my mistake is because php is server side and javascript is client side, but I have no idea how to get the two to act how I want.</p> <pre><code>&lt;?php if ($options) { ?&gt; &lt;?php foreach ($options as $option) { ?&gt; &lt;?php if ($option['type'] == 'select') { ?&gt; &lt;span id="option-&lt;?php echo $option['product_option_id']; ?&gt;" class="option"&gt; &lt;select name="option[&lt;?php echo $option['product_option_id']; ?&gt;]" onchange="getIt(this)"&gt; &lt;?php foreach ($option['option_value'] as $option_value) { ?&gt; &lt;option value="&lt;?php echo $option_value['product_option_value_id']; ?&gt;"&gt;&lt;?php echo $option_value['name']; ?&gt; &lt;?php if ($option_value['price']) { ?&gt; &lt;?php $option_value['price']; ?&gt; &lt;?php } ?&gt; &lt;/option&gt; &lt;?php } ?&gt; &lt;/select&gt; &lt;/span&gt; </code></pre> <p>I have tried:</p> <pre><code>&lt;script type="text/javascript"&gt; function getIt(elm) { var val = elm.options[elm.selectedIndex].text; window.alert(val); } &lt;/script&gt; </code></pre> <p>and</p> <pre><code>&lt;script type="text/javascript"&gt; function getIt() { var val = "&lt;?php $option_value['price']; ?&gt;"; window.alert(val); } &lt;/script&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload