anointing Posted September 8, 2008 Share Posted September 8, 2008 Hi, I want to access a javascript variable (cart_empty) and update it from a php shopping cart file. The variable is located in an external javascript file (checkout.js) located in a folder within the root directory. All php files are also located in the root directory. Subfolder Html files have access to the javascript file via: <script language="javascript" type="text/javascript" src="../js_files/checkout.js"></script> The value of the variable cart_empty determines whether or not the html files display option buttons to "view cart" and "checkout". I want to update this variable from viewcart.php in the root directory. In viewcart.php, shoppers have the option to continue shopping or checkout. If they opt to continue shopping, they will be transferred to html files. The problem is all attemps to access the javascript variable from within viewcart.php have failed. I created a function in checkout.js called emptyCart() which alters the value of the variable cart_empty and tried calling this function from viewcart.php. This was tried from within the viewcart.php file by: 1) first declaring the external javascript file itself <script language="javascript" type="text/javascript" src="/js_files/checkout.js"></script> 2) secondly, by cahlling te function that updates the cart_empty variable <script language="javascript" type="text/javascript">emptyCart();</script> This does not have any impact at all. Any ideas how best to resolve this problem? Quote Link to comment Share on other sites More sharing options...
lemmin Posted September 8, 2008 Share Posted September 8, 2008 Assuming you need to know the value of the variable in PHP, send it through POST or GET to viewcart.php and to change the value, just echo out "<script type="text/javascript">variable = 'new value'</script>"; Assuming "variable" is in the global scope of your javascript, it should let you change it from anywhere. Quote Link to comment Share on other sites More sharing options...
anointing Posted September 9, 2008 Author Share Posted September 9, 2008 Hi As suggested, I have added the following code to viewcart.php echo "<script type='text/javascript' src='js_files/checkout.js'>cart_empty='no';</script>"; where cart_empty is the javascript global variable I wish to update. But this variable is still not updating. It appears viewcart.php is still not accessing that variable as it should. The variable is within checkout.js javasrcipt file, located in a subfolder (js_files) in the root directory. Viewcart.php is also in the root directory so the path: src='js_files/checkout.js' is correct. Quote Link to comment 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.