Jump to content

How to Update a Javascript Variable from PHP files


anointing

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.