kirkh34 Posted November 26, 2010 Share Posted November 26, 2010 hello im trying to change a variable set outside of a function and calling the function with an onchange... i'm having problems getting the variable to change <script type="text/javascript"> var price = Number('<?php echo $price; ?>'); function addtwo() { if(document.add.size.value == "2xl") { price = price + 2; } } </script> Quote Link to comment https://forums.phpfreaks.com/topic/219897-changing-javascript-global-variable-inside-function/ Share on other sites More sharing options...
mdgeus Posted November 26, 2010 Share Posted November 26, 2010 Why don't you parse the value with the onchange onChange="addtwo('<?php echo $price; ?>');" <script type="text/javascript"> function addtwo(p) { if(document.add.size.value == "2xl") { p = price; price = price + 2; } } Not tested it but I think that works better. </script> Quote Link to comment https://forums.phpfreaks.com/topic/219897-changing-javascript-global-variable-inside-function/#findComment-1139910 Share on other sites More sharing options...
.josh Posted November 26, 2010 Share Posted November 26, 2010 are you sure it's the variable? There's no reason the variable shouldn't be incremented. I think the more likely problem is that the condition fails or the function fails to be called. put an alert(price) into your function, outside of the condition. If it fails to alert, your function is failing to be called. If it does alert, put the alert inside your condition. If it fails to be called, your condition is failing. Quote Link to comment https://forums.phpfreaks.com/topic/219897-changing-javascript-global-variable-inside-function/#findComment-1140008 Share on other sites More sharing options...
kirkh34 Posted November 26, 2010 Author Share Posted November 26, 2010 okay the alert works... and it shows the incremented variable... but i guess i'm not displaying it right? here's my code echo "<tr>"; echo "<td style='text-align:right;padding-right:10px;' id='certain'>Price:</td><td style='font-size:16px;padding-left:5px;color:#fff;'>$<script type='text/javascript'>document.write(price);</script></td>"; echo "</tr>"; echo "<tr>"; echo "<td style='text-align:right;padding-right:10px;' id='certain'>Size:</td> <td style='padding-left:5px;'> <select class='size' name='size' onChange=\"addtwo();\"> <option value='s'>S</option> <option value='m'>M</option> <option value='l' SELECTED>L</option> <option value='xl'>XL</option> <option value='2xl'>2XL (Add $2.00)</option> </select> </td>"; echo "</tr>"; Quote Link to comment https://forums.phpfreaks.com/topic/219897-changing-javascript-global-variable-inside-function/#findComment-1140045 Share on other sites More sharing options...
kirkh34 Posted November 26, 2010 Author Share Posted November 26, 2010 i did notice... with this function ...everytime you onchange it adds 2... i would not like to have it continue to add over and over in case someone selects it over and over.... as you can see in the html there are various sizes and if "2xl" is selected i would like the price to display 2 dollars more than the original price and if switched back then the price goes back as well....i'm thinking maybe the function needs to be redone...any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/219897-changing-javascript-global-variable-inside-function/#findComment-1140055 Share on other sites More sharing options...
haku Posted November 27, 2010 Share Posted November 27, 2010 Suggestion? Maybe learn to organize your thoughts into something understandable? It seems like you typed out your thoughts without actually looking at them to see if they mad sense to anyone else. Quote Link to comment https://forums.phpfreaks.com/topic/219897-changing-javascript-global-variable-inside-function/#findComment-1140167 Share on other sites More sharing options...
kirkh34 Posted November 27, 2010 Author Share Posted November 27, 2010 well haku let me explain my thoughts... what is it about my question that is not understandable? is it the part where i use php to echo out the html?... listen... i come onto these forums because there are intelligent people who like helping people who don't understand programming at your genius level yet... it's not like my situation is complicated.... ... i have a simple select with sizes that if you look closely... the XL size needs to add $2.00 to the price when selected....this seems like it could be accomplished simply but I can't figure it out...which brings me to the forum i'm not asking anyone to write me a whole new working function or anything...i came here for ADVICE...like "yeah you could try doing this or this..." and if it's something I don't understand yet I can look it up....i appreciate everyones responses that gave their time and i appreciate the ones who chose to waste my time to not post... Quote Link to comment https://forums.phpfreaks.com/topic/219897-changing-javascript-global-variable-inside-function/#findComment-1140226 Share on other sites More sharing options...
haku Posted November 28, 2010 Share Posted November 28, 2010 My problem wasn't with your coding or level whatsoever, it was with the fact that I can't understand what you are saying in your posts. They are like a stream of thoughts without a filter. Quote Link to comment https://forums.phpfreaks.com/topic/219897-changing-javascript-global-variable-inside-function/#findComment-1140582 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.