Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2005
    Posts
    43
    Plugin Contributions
    0

    Default Connect to database and variables in a javascript

    Hi,
    Iīm implementing some external javascript code in our store to track user behaviour. But I cannot get the values of the variables to be shown.

    Iīm putting the code in a new file in includes/modules/pages/product_info/jscript_track.php
    and in another javascript in a new file in ../modules/pages/shopping_cart/jscript_trackcart.php
    The javascript is shown correctly, but the variables donīt show anything.

    The variables I would like to show in the javascript on the productpage:
    Product name, product model and product price.
    I tried the following parts of code to show them: <?php echo $products_name ?>, <?php echo $products_model ?> and <?php echo $products_price ?> but this didnīt work. I suppose the connection with the database and a query is missing.

    The variables I would like to show in the javascrip on the shoppingcartpage:
    Product ID, Quantity, Price of the product in the cart. If there are more products in the cart also the same data of the other products.

    How do I connect to the database, from these files? And can anyone help me with how to put the sql query?

    Thanks,

  2. #2
    Join Date
    Jan 2004
    Posts
    66,446
    Plugin Contributions
    81

    Default Re: Connect to database and variables in a javascript

    You said you're using v1.3.8a. But, that old version from 2006 contains some serious security flaws which were corrected in a newer version.

    THE FIRST THING YOU SHOULD DO IS UPGRADE TO A MODERN VERSION OF ZEN CART !!!!!!

    After you have done that, then you can continue your customizations as needed.
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  3. #3
    Join Date
    May 2013
    Posts
    38
    Plugin Contributions
    0

    Default Re: Connect to database and variables in a javascript

    Quote Originally Posted by carol View Post
    Iīm putting the code in a new file in includes/modules/pages/product_info/jscript_track.php
    and in another javascript in a new file in ../modules/pages/shopping_cart/jscript_trackcart.php
    I'm pretty sure php and Javascript don't "work" with each other. So, if you're trying to load a Javascript script from a php file, I don't know that it will work.

    What are you trying to echo out here:

    Quote Originally Posted by carol View Post
    I tried the following parts of code to show them: <?php echo $products_name ?>, <?php echo $products_model ?> and <?php echo $products_price ?> but this didnīt work. I suppose the connection with the database and a query is missing.
    If you're working with Javascript, why are you trying to echo out php variables?

    Also, I'm almost 100% positive that anything that's done in Javascript is plain-text and readable by the client. Connecting to a database from Javascript itself would be a huge security flaw. How about transferring JSON data to your secure server which would then handle the input and database connection?
    Last edited by allthingsidLeroy; 10 Jun 2013 at 10:25 PM.

  4. #4
    Join Date
    Jul 2005
    Location
    Upstate NY
    Posts
    22,010
    Plugin Contributions
    25

    Default Re: Connect to database and variables in a javascript

    I have used a jscript_xxx.php file to run a script in certain conditions (set by PHP tests), in the Ultimate Fade-in Slideshow mod.
    You would need to be accessing PHP variables that are available to the current page, and since the jscript will run before many other PHP files, you need to make sure those variables have been set before html_header.php runs.
    Last edited by gjh42; 11 Jun 2013 at 12:14 AM.

  5. #5
    Join Date
    Jul 2005
    Location
    Upstate NY
    Posts
    22,010
    Plugin Contributions
    25

    Default Re: Connect to database and variables in a javascript

    Yes, you could do some SQL queries in the PHP part of your file before the script if necessary to look up info. That would not be accessible from the javascript when it runs; only the resulting values of your lookups would be output with the js.
    Some of the info you want may be accessible from the $_SESSION or $_GET variables without going to the database.

    When you have updated and are ready to continue with customization, come back and ask for details on doing this.

  6. #6
    Join Date
    Nov 2005
    Posts
    43
    Plugin Contributions
    0

    Default Re: Connect to database and variables in a javascript

    Yes, I know I should update to the newest version of Zencart. Thatīs a big project and planned for september. However in the meantime we are connecting to an third party software and I would like to implement some customer tracking already.

    Any help, on how to display the variables in the javascript is appreciated!

  7. #7
    Join Date
    Jul 2005
    Location
    Upstate NY
    Posts
    22,010
    Plugin Contributions
    25

    Default Re: Connect to database and variables in a javascript

    Feeding the values into the javascript is trivial - for example:
    PHP Code:
    var mygallery=new fadeSlideShow({
        wrapperid: "fadeshow1", //ID of blank DIV on page to house Slideshow
        dimensions: [250, 180], //width/height of gallery in pixels. Should reflect dimensions of largest image
        imagearray: [
            ["includes/templates/your_template/images/yourimage0.jpg", "", "", "<?php echo FADESHOWTEXT_1_IMG00;?>"],
            ["includes/templates/your_template/images/yourimage1.jpg", "", "", "<?php echo FADESHOWTEXT_1_IMG01;?>"],
            ["includes/templates/your_template/images/yourimage2.jpg", "http://some-url.com", "_new", "<?php echo FADESHOWTEXT_1_IMG02;?>"],
    FADESHOWTEXT_1_IMG00 is a constant set in a language define file. A variable would be echoed in the same way. By the time the javascript runs, all it knows is that there is text in the array (or a link or product id, etc.)

    I would first do a debug echo of the variable you want to use, to see if it passes a correct value at execution (look at view source if the page doesn't finish loading). If it doesn't work, then you need to find the info another way. For the product info page, the product id will be passed in $_GET['products_id'] even if it is not yet available in $products_id. From that, you can use a ZC lookup function to get other info about the product. For the shopping cart, a lot of info is held in (I think) the cart object... I would have to research exactly how to access it. Someone else will probably have that ready to hand.

  8. #8
    Join Date
    Jan 2007
    Location
    Australia
    Posts
    6,167
    Plugin Contributions
    7

    Default Re: Connect to database and variables in a javascript

    Quote Originally Posted by allthingsidLeroy View Post
    I'm pretty sure php and Javascript don't "work" with each other.
    I'm pretty sure you are right.

    PHP is processed by the server.
    JavaScript is processed by the client.

    This makes a huge difference in what variables can be expected at any given time/place.

    Cheers
    Rod

  9. #9
    Join Date
    Jul 2005
    Location
    Upstate NY
    Posts
    22,010
    Plugin Contributions
    25

    Default Re: Connect to database and variables in a javascript

    Right - the javascript will be output to the browser with the values of the PHP variables at the instant when the file is run on the server; these will be static text/numbers/whatever when the js operates on them.

  10. #10
    Join Date
    Jan 2007
    Location
    Australia
    Posts
    6,167
    Plugin Contributions
    7

    Default Re: Connect to database and variables in a javascript

    Quote Originally Posted by gjh42 View Post
    Right - the javascript will be output to the browser with the values of the PHP variables at the instant when the file is run on the server; these will be static text/numbers/whatever when the js operates on them.
    Sounds good to me, as I'm sure it does to all of us that have dealt with PHP and JavaScript.

    It sometimes takes a while for this 'penny to drop' for the less experienced though, or for those with experience with one, but not the other.

    Even with experience, I often find myself having to take a step back and take another look at what it is I'm trying to achieve, and whether it be serverside (PHP) or client side (JavasScript).

    As you have correctly implied, a PHP serverside 'variable' becomes a client side 'static'. Conversely, a javascript 'variable' becomes a serverside 'static' when inserted as a POST/GET variable.

    At least that is the way *I* keep the expectations clear in my head (my head is a scary place to be though, so this may not work with everyone) <g>

    Cheers
    Rod

 

 

Similar Threads

  1. Replies: 3
    Last Post: 1 May 2010, 04:58 PM
  2. Dependent variables (javascript?)
    By travellers in forum Setting Up Categories, Products, Attributes
    Replies: 2
    Last Post: 26 Nov 2008, 06:20 PM
  3. Help! WAMP install no database connect and template design
    By dml311071 in forum Installing on a Windows Server
    Replies: 0
    Last Post: 24 Sep 2007, 02:24 PM
  4. Replies: 2
    Last Post: 29 Dec 2006, 04:52 AM
  5. Variables needed for Javascript - order tracking
    By smartmomma in forum Basic Configuration
    Replies: 0
    Last Post: 10 Sep 2006, 07:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg