2

Hi I am working on a website for uni for an online store. I am using NOP Design freecart and everything works fine.

I have the menu and header in PHP which work fine but I can't get my javascript which displays items in cart and total in the php menu

PHP Header

    <h1 id="logo"><a href="index.html">Pet Accessories</a></h1> 

    <!-- Cart -->
    <div id="cart">
        <a href="managecart.html" class="cart-link" >Your Shopping Cart</a>
        <br>


    </div>
    <!-- End Cart -->

    <!-- Navigation -->
    <div id="navigation">
        <ul>
            <li><a href="index.html" class="active">Home</a></li>
            <li><a href="#">About Us</a></li>
            <li><a href="#">Contact</a></li>
        </ul>
    </div>
    <!-- End Navigation -->

The javascript I want to use

scart.js file

if ( Cart_is_empty()) {
document.write('Your cart is empty.');
   } else {
      document.write('In your cart:<p>');
      Print_total_products(true);
      document.write(', ');
      Print_total(true);
      document.write('<p>Applicable shipping and taxes extra.<p>');
      document.write('<a href="managecart.html">View Cart</a>');

}

If I paste that code into my index.html file is displays correctly but when I paste it into the .php file it doesnt work

I've tried pasting this <?php echo "<script type=\"text/javascript\" src=\"/js/scart.js\"></script>; ?> )) into the header.php file but that doesn't work either.

Any ideas on what I should be doing? The shopping cart code I have used from a site so it is quite tricky for me to get my head around.

Any help would be appreciated, thanks

3
  • 2
    Piece of advice: ignore what they teach you at uni and instead pick up a good book on JS and/or visit sites like this one where you can learn to code with proper techniques. ;) Commented May 28, 2013 at 9:41
  • Why wrap it in php tags anyway if it's just a template file? Commented May 28, 2013 at 9:45
  • 1
    Show us the outputted header section where the jscript file is supposed to be Commented May 28, 2013 at 9:47

2 Answers 2

1

In your example you have posted

<?php echo "<script type=\"text/javascript\" src=\"/js/scart.js\"></script>; ?> ))

This is not valid PHP. You never close the quotes. Besides, the )) seem misplaced as well. As other people say, try this:

<?php echo '<script type="text/javascript" src="/js/scart.js"></script>'; ?>

As some of the comments point out you should have a particular reason to place this in PHP as opposed to directly in the HTML, like so:

<head>
    <script type="text/javascript" src="/js/scart.js"></script>
    <?php
        //more php...
    ?>
</head>

There is no reason to wrap it in PHP if you don't add it programatically.

Sign up to request clarification or add additional context in comments.

3 Comments

The reason that I wanted it in the header of my site template is so that you can see the items and total cost no matter what page you are on the site. The assignment brief wanted us to use PHP for the left menu and header.
I wanted it like this so that you can see the cart items and cost no matter what page you are on within the site
Thanks, it's now just a formatting problem to get it to appear in the right spot
0

Try this: <?php echo "<script type='text/javascript' src='/js/scart.js'></script>"; ?>

be sure the file path is right

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.