Adding javascript to php header - php

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">Pet Accessories</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>Home</li>
<li>About Us</li>
<li>Contact</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('View Cart');
}
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

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.

Try this:
<?php echo "<script type='text/javascript' src='/js/scart.js'></script>"; ?>
be sure the file path is right

Related

Show banner only on archive pages using PHP

I have a banner I would like to only show on archive pages on my website. I can't figure out the php code to do so. Here is what I have so far (obviously I've removed a lot of the code to make this look cleaner and easier):
<header id="masthead" class="site-header navbar-static-top fixed-top <?php echo wp_bootstrap_starter_bg_class(); ?>" role="banner">
</header>
<?php if(is_archive()) ?>
<div class="free-shipping-banner">
<p>
FREE SHIPPING on all print orders within North America.
</p>
</div>
<?php endif; ?>
You can see it is my free-shipping-banner that I want only displayed on archive pages. I'm not getting any errors with this code, but it's still showing up on all other pages of my website. Does anyone know what I am doing wrong?
Here is your correct code. Please add brackets for if statement.
<header id="masthead" class="site-header navbar-static-top fixed-top <?php echo wp_bootstrap_starter_bg_class(); ?>" role="banner">
</header>
<?php if(is_archive()) { ?>
<div class="free-shipping-banner">
<p>
FREE SHIPPING on all print orders within North America.
</p>
</div>
<?php } ?>
The mistake in your code is that the if statement will only look for the next line. But when you place brackets; you tell the server where it ends. You used endif but you also forgot a colon at the end of if statement. The above code will work.

How and where to put Jquery into a fully PHP page?

I have attempted to put my script into a page I have created but can't seem to get the script to work correctly. It is a paginate script which links to the "newsblocks" class. Where do I place the script to make it work?
<?php
/*
Template Name: [Newspage]
*/
get_header();?>
<div id="main" class="defaultContentWidth">
<div id="wrapper-row">
<div id="primary">
<div id="content">
<?php the_breadcrumb();?>
<h1 class="entry-title"><?php the_title();?></h1>
<?php if(have_posts()) :
while(have_posts()) : the_post();?>
<div class="newscont"><?php the_content();?></div>
<?php endwhile;
endif;?>
<script type="text/javascript" src="<?php bloginfo('template_directory');?>/javascript/jquery.pajinate.js"></script>
<div class="contnewslist">
<ul class="newsblocks">
<li>
<h2>NEWSLETTER 1</h2>
<i>November 01, 2014</i>
<p>This is the November edition of the newsletter for The Dry Cleaner App. Featuring in this newsletter is ...</p>
<input type="submit" value="Download" id="submit" class="newsdownload">
</li>
For Optimizing the performance of a webpage it's always recommended to put the Javascript file to your footer.
So load your script files in your footer file.
In wordpress you've a footer.php
Change
<script type="text/javascript" src="<?php bloginfo('template_directory');?>/javascript/jquery.pajinate.js"></script>
TO
<script type="text/javascript" src="<?php echo echo get_template_directory(); ?>/javascript/jquery.pajinate.js"></script>
try to add js with get_template_directory_uri()
<script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/javascript/jquery.pajinate.js"></script>
and add js to your theme footer.php or in same page footer or where you need
also i think typo here :- jquery.pajinate.js should be jquery.paginate.js
I'm thinking <?php bloginfo('template_directory');?> should be changed to <?php echo bloginfo('template_directory');?> but since you didn't gave us the definition of bloginfo() function we can't really do much..

Wordpress not including my php file

This is my code that I have calling my php file:
<section style="width:100%; height:120px; clear:both;" >
<section class="campaign_statistics" style="background-color:#EFEFEF;">
<?php include('progress_chart.php'); ?>
</section>
</section>
However, nothing displays on the page when I call it, and when I view the page source this is what I see:
<section style="width:100%; height:120px; clear:both;" >
<section class="campaign_statistics" style="background-color:#EFEFEF;">
</section>
</section>
Everything but my php include. All of this was working fine on friday. I go and check my site this morning and it's not displaying what's in that php file. Is there a problem with wordpress? I've gone over all my code and can't find any errors and no changes were made over the weekend.
Here is a portion of the contents of my php file:
<?php
if ($blog_id == 1)
echo
'
<script>
var percent = String(totalProgress.getPercent());
document.write(totalProgress.toString());
</script>
'
;
if ($blog_id == 68)
echo
'
<script>
var percent = String(alumniProgress.getPercent());
document.write(alumniProgress.toString());
</script>
'
;
?>
etc... there's about 20 of these. They're javascript calls.
Either of these built-in wordpress functions should work instead of a plain include:
get_template_part ('progress_chart');
or
include locate_template('progress_chart.php');
http://codex.wordpress.org/Function_Reference/get_template_part
http://codex.wordpress.org/Function_Reference/locate_template
EDIT: Use "include locate_template()" if you want progress_chart to be able to access variables from the file n which it's being included. For some reason get_template_part doesn't allow that.

A anchor as submit button with value

This may seem like a duplicate question but I have googled and search stackoverflow how an a anchor tag would act like as a submit that would fit on the website that I am trying to do.
I have this code on my right panel navigation:
<div class="content clearfix">
<div class="left_column">
<div class="product_menu">
<? if($_SESSION['logged_in'] == 1) {
$customer_panel = '
<div class="customer_nav">
<h1 class="customer_nav_name">Hello ' . $_SESSION['fname'] . ',</h1>
<ul class="clearfix">
<li>View Profile</li>
<li>Update Information</li>
<li>
<!-- this is where i would like the signout button to act as a submit
i have left it plane to start from scratch -->
Sign Out
</li>
</ul>
</div>';
echo $customer_panel;
}?>
<h2><span>Product Categories</span></h2>
<ul id="prod_nav" class="clearfix">
<li class="top"><span>Processed Meat</span></li>
<li class="top"><span>Ready Made</span></li>
<li class="top"><span>Siomai & Siopao</span></li>
<li class="top"><span>English Pork Snacks</span></li>
</ul>
</div>
And here is my usual code when i use a submit button and not an anchor tag to make a POST method:
if(isset($_POST['signout'])) { // logout button
// Clear and destroy sessions and redirect user to home page url.
$_SESSION = array();
session_destroy();
// Redirect to where the site home page is located -- Eg: localhost
header('Location: http://localhost/');
}
I have read many of this questions but I don't know jQuery or how to achieve this using javascript.
I also tried using this solution:
<form id="signout" action="index.php" method="post">
Sign Out
<input type="hidden" name="signout" value="signout"/>
</form>
that I kinda found from here at stackoverflow.
The problem about this is that if I include the onclick="" , the whole page does not show. but if i remove it, the page comes back to normal. - it doesn't work inside an <li> element.
It might sound basic, but it would help me and other new starters to understand how to make it possible. Thank you very much. :)
Try this,
STEP 1 :
Put the following scripts in your html file.
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
<script type="text/javascript">
$(function() {
$('.signout-btn').click(function() {
$('#signout').submit();
});
})
</script>
STEP 2 :
Add an attribute class="signout-btn" to anchor tags, which you want to trigger the form submission.

Magento - cart not showing and checkout/cart page empty (doesn't show the .cart div)

Okay, so I'm a newbie to Magento and have been making lots of changes to get it working the way a client is wanting.
Obviously at install and early on, the shopping cart was working fine. I moved it up to the top right corner for a while w/ CSS and then I THOUGHT that I display:none'd it, as I just wanted to focus on other things for a while before moving on to styling it. Now I've done all those other things and want to deal with the cart. Trouble is - it is nowhere to be found and the display:none that I thought I'd used to turn it off is similarly missing. Maybe I never did it?
I can't find anything in the CSS that would have turned off the cart - I can't find a display:none on it or any parents. Most of the site is using a 2 column with left sidebar layout, and I know the shopping cart resides by default in the right. But when I turn back on the right sidebar - no cart. Furthermore, when I go to add a product to the cart, I am redirected to mydomain.com/checkout/cart as expected, but there is nothing on the page. My styling, menus, etc are present but the .col-main element where the full-page detailed cart should be is empty. Which makes me think again that the problem isn't CSS.
It's as if the site just isn't calling for the cart (I assume cart.phtml?). It is totally possible that I screwed something up while messing with other things (adding a CMS page menu at top, lots to do with resizing images, moving the category menu to the left sidebar). My php isn't good enough to find the culprit and I'm not even sure which file I should be looking at - checkout.phtml?
I realize this question has very little valuable information, but does anyone have any ideas as to where I should be looking? I can post the relevant code if I know which file to look in. The site is cart.oldfloridian.com. If you want to try to add a product, there is only one at the moment, at "ak starfish --> starfish women's"
Edit: so the /checkout/cart page is a 3column layout. Here is the 3columns.phtml code:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $this->getLang() ?>" lang="<?php echo $this->getLang() ?>">
<head>
<?php echo $this->getChildHtml('head') ?>
</head>
<body<?php echo $this->getBodyClass()?' class="'.$this->getBodyClass().'"':'' ?>>
<div id="cms-menu">
<?php $collection = Mage::getModel('cms/page')->getCollection()->addStoreFilter(Mage::app()->getStore()->getId());?>
<?php $collection->getSelect()
->where('is_active = 1'); ?>
<ul>
<?php foreach ($collection as $page): ?>
<?php $PageData = $page->getData(); ?>
<?php if($PageData['identifier']!='no-route') { ?>
<li>
<?php echo $PageData['title'] ?>
</li>
<?php } ?>
<?php endforeach; ?>
</ul>
</div>
<?php echo $this->getChildHtml('after_body_start') ?>
<div class="wrapper">
<?php echo $this->getChildHtml('global_notices') ?>
<div class="page">
<?php echo $this->getChildHtml('header') ?>
<div class="main-container col3-layout">
<div class="main">
<?php echo $this->getChildHtml('breadcrumbs') ?>
<div class="col-wrapper">
<div class="col-main">
<?php echo $this->getChildHtml('global_messages') ?>
<?php echo $this->getChildHtml('content') ?>
</div>
<div class="col-left sidebar"><?php echo $this->getChildHtml('left') ?></div>
</div>
<div class="col-right sidebar"><?php echo $this->getChildHtml('right') ?></div>
</div>
</div>
<?php echo $this->getChildHtml('footer') ?>
<?php echo $this->getChildHtml('before_body_end') ?>
</div>
</div>
<?php echo $this->getAbsoluteFooter() ?>
</body>
</html>
There does seem to be a bug in there somewhere - we had a similar issue where two devs in the team couldnt see the cart, 1 could - but it wasnt just the cart it was the whole customer section (login, cart, my account etc).
We tried enabling Template Path Hints but the only thing in the main section of the page was "frontend/default/default/template/catalog/msrp/popup.phtml". We re-installed most of magento - no luck
We tried pretty much everything and what fixed it was coping all of the theme files into a new theme and re-naming it. Only thing I can think of that it could have been was a hyphen in the theme name

Categories