Wordpress not including my php file - php

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.

Related

load php file on load with jQuery

I have a simple php file that prints the date and time. I would like to load this file with jQuery so it shows the $date variable. Can't seem see why this is not working, any advice on why?
My php file:
<?php
date_default_timezone_set('GMT');
$date = date('d/m/Y H:i:s');
print $date;?>
My on page code:
<script>$(document).load(function(){
$('#time').load('http://www.my-website.co.uk/my-php-file.php');});</script>
<div id="time"></div>
I would remove the print... line from the php file, include the php file in your HTML page and add an echo for the $datevariable after the include line (i.e. no jquery at all):
<div id="time">
<?php
include "http://www.my-website.co.uk/my-php-file.php";
echo $date;
?>
</div>
code now working thanks for all the help:
<script>
$(document).ready(function(){
$('#time').load('http://www.my-website.co.uk/my-php-file.php');
});</script>
<div id="time"></div>

Adding javascript to php header

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

Loading an external PHP file with an internal PHP include function by using jQuery's 'load()'

I have a site under development with a "News" section and an "Older News" section. The news are stored individually in external PHP files which are loaded by the server by PHP (the server counts the number of PHP files and displays the last one – the current news – in the "News" sections and all the others in the "Older News" section:
<!-- ####### NEWS ####### -->
<div id="news">
<h2>NEWS</h2>
<div>
<a id="showoldnews" href="#news">OLDER</a>
</div>
<?php $directory = "assets/news/"; if (glob($directory . "*.php") != false) { $newscount = count(glob($directory . "*.php")); } else {} ?>
<?php include('assets/news/news' . $newscount . '.php'); ?>
<!-- ####### OLDER NEWS ####### -->
<div id="oldnews">
<h2>OLDER NEWS</h2>
<?php
$newscount = $newscount-1;
while ($newscount>0) {
include('assets/news/news' . $newscount .'.php');
--$newscount;
}
?>
</div>
The "Older News" section is initially hidden and only made visible by a jQuery trigger:
// Show old NEWS
$('a#showoldnews').click(function () {
$('#oldnews').show(400);
});
But I am expecting problems in long-term: since all news are loaded in the first place, this means that with more and more news coming up the website will be loading slower. And even if it's "acceptable", it's still not the ideal solution.
I was thinking on using jQuery.load() to load an external PHP file that would then load the old news only when the user asks for it.
The problem is that I don't know if its possible to load the following script by using jQuery.load() after the user clicks on a link:
<?php
$directory = "assets/news/";
if (glob($directory . "*.php") != false) {
$newscount = count(glob($directory . "*.php"));
}
else {
}
?>
<?php
include('assets/news/news' . $newscount . '.php');
?>
It this approach acceptable? I have tried, but it didn't work at all (I could load static content with the load() function, but I was not able to make the server process the PHP script. I don't know if it's possible to make the server process the PHP code, because PHP processing is done on the the server side before the website begins loading... which is not the case.
UPDATE #1
I have the following that loads the 'load.php' file from the server:
<script type="text/javascript">
$('#oldnews').load('load.php');
</script>
The content of the load.php file is:
<div class="section" id="oldnews">
<h2>OLDER NEWS</h2>
<div class="topLink">
TOP
</div>
<div class="topLink2">
<a id="hideoldnews" href="#news">HIDE</a>
</div>
<?php
$newscount = $newscount-1;
while ($newscount>0) {
include('assets/news/news' . $newscount .'.php');
--$newscount;
}
?>
</div>
The "TOP" and "HIDE" links appear without any problems. However, the PHP block seems to not be processed at all...
PHP can be run before jQuery grabs the content... Here is what I setup and tested...
test.html
<!DOCTYPE HTML>
<html>
<head>
<script type="text/JavaScript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<div id="feeds"><b>45</b> feeds found.</div>
<script type="text/JavaScript">
$("#feeds").load("go.php");
</script>
</body>
</html>
go.php
<?php
echo 'Hi';
?>
And the result is that #feeds .innerHTML is "Hi"... I would love more information/code in replicating the situation. It might help to take a peek at the jQuery load API documentation.
As #joseph told its correct and helped me, but in mycase i wanted to hide the content of the External Php file,
Below is my code changes which had helped me !
<!DOCTYPE HTML>
<html>
<head>
<script type="text/JavaScript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<b>Db file result</b>
<div id="feeds" hidden=""></div>
<script type="text/JavaScript">
$("#feeds").load("db.php");
</script>
</body>
</html>
My Php file had database connection so i had to hide it,
db.php
<?php
session_start();
$db = pg_connect("host=ec2-xxx-x0-xxx-xx.compute-1.amazonaws.com port=5432 dbname=dxxxxxxxxx user=vxxxxxxxxxx password=1xxxxxxxxxxxxxxxxxxxxxxxxxxxxd");
pg_select($db, 'post_log', $_POST);
$query=pg_query("(SELECT id,name, FROM organization WHERE is_active = 'true' AND account_token = '".$_SESSION['account_token']."');
$json=array();
while ($student = pg_fetch_array($query)) {
$json[$student["is_user"]."-".$student["id"]] = $student["name"]."-".$student['pan'];
}
$textval = json_encode($json);
$foo = "var peoplenames=" . $textval;
file_put_contents('autocomplete-Files/'.$_SESSION['account_token'].'.js', $foo);
echo "<script>location='filename.php'</script>";
?>

Get PHP script result from a page with HTML content by AJAX

I have a PHP page with HTML content in it. Now I run some PHP codes between HTMLs and I get some results. The fact is, when I try to get a respond from this page by AJAX it'll show me the whole page content and plus the result I was looking for. What can I do to prevent the page from writing extra content. I know whenever something get printed on page it'll go as respond to the AJAX call but I want a way to somehow fix this.
My page file (name: page.php):
<?php echo $Content->GetData('HEADER'); ?>
<div id="Content">
<div id="Page">
<?php if($Content->GetData('PAGE','IS_TRUE')) : ?>
<?php if(NULL !== $Content->GetData('PAGE','TITLE')) : ?>
<?php echo $Content->GetPlugins("Page:" . $Content->GetData('PAGE','ID') . ",BeforeTitle"); ?>
<div id="Title" dir="rtl">
<?php echo $Content->GetData('PAGE','TITLE'); ?>
</div>
<?php echo $Content->GetPlugins("Page:" . $Content->GetData('PAGE','ID') . ",AfterTitle"); ?>
<?php endif; ?>
<div id="Content" dir="rtl">
<div style="float: right; width: 966px; padding: 6px">
<?php echo $Content->GetPlugins("Page:" . $Content->GetData('PAGE','ID') . ",BeforeContent"); ?>
<?php echo $Content->GetData('PAGE','CONTENT'); ?>
<?php echo $Content->GetPlugins("Page:" . $Content->GetData('PAGE','ID') . ",AfterContent"); ?>
</div>
</div>
<?php else : ?>
<div id="Content" dir="rtl">
<div style="float: right; width: 966px; padding: 6px">
There is no page like this in our archives.
</div>
</div>
<?php endif; ?>
</div>
</div>
<?php echo $Content->GetData('FOOTER'); ?>
If I write an address in my browser like this localhost/cload/blog?action=rate it'll go through my redirection list and show the page.php with blog plugin loaded. The problem is I want to call my blog plugin by AJAX through this address but it will first render the page data.
Sorry if this is messy.
I'd suggest modifying page.php to be some functions, primarily a data processing function, and then an output function. Then, when you load the page, put a check in for whether it's an AJAX request, and if so, echo the data you want as JSON, otherwise render the page using the output function.
Alternatively, you could create a second, separate page, but that could be more difficult to maintain than a single file.
Yes, you should check whether your request is a ajax request, if it is so you should change your response to return only the result whatever want.
This php code will give an rough idea on this.
if($request->isXmlHttpRequest()){
return new Response(json_encode($data_array));
}
Hope this will help.

trying to .load() a php file, and display html inside

I'm trying to display the html inside of a php file i have. I'm using .load('file.php') to grab it and show it in a hidden div appended via jquery. But the problem is, it wont display anything past a php line in the file. Like this:
<div id="content">
<h1> Welcome </h1>
<form action="<?php activity_post_form_action()?> "method="post" id="whats-new-form" name="whats-new-form">
Everything below the form wont show, but the <h1> Welcome </h1> will show.
All of the html and such is valid, meaning I closed all of my tags and such. It just halts after it sees an php line. I removed the php from the form, and more showed up until the next php line.
Its definitely something i did wrong...lol, first rule to coding: EVERYTHING is your fault :)
Any ideas what i did wrong?
also, if you want a look at my jquery line :
jQuery('#window').load('file.php');
This is the content of the file.php:
<div id="content">
<div class="raberu"><h6>post update</h6></div>
<h1> Welcome </h1>
<form action="<?php bp_activity_post_form_action()?>" method="post" id="whats-new-form" name="whats-new-form">
<?php do_action( 'bp_before_activity_post_form' )?>
<div class="content-body">
<div class="top-content">
<h5>
<?php if ( bp_is_group() ) : ?>
<?php printf( __( "What's new in %s, %s?", 'buddypress' ), bp_get_group_name(), bp_get_user_firstname() ) ?>
<?php else : ?>
<?php printf( __( "So, what's up %s?", 'buddypress' ), bp_get_user_firstname() ) ?>
<?php endif; ?>
</h5>
</div>
<div class="avatar"><img src="<?php bp_loggedin_user_avatar('html=false');?>"/></div>
<div class="foremu">
<div id="whats-new-textarea">
<span class="arato">Click here to start typing</span>
<textarea name="whats-new" id="whats-new">
</textarea>
</div>
<div id="whats-new-options">
<div id="whats-new-submit">
<span class="ajax-loader"></span>
<input type="submit" name="aw-whats-new-submit" id="aw-whats-new-submit" value="<?php _e( 'Post Update', 'buddypress' )?>"/>
<input type="button" class="cancel" value="cancel"/>
</div>
</div>
</div>
And when I check my error counsel, I get This : PHP Fatal error: Call to undefined function and it says this for each line of php code that works just fine if I load that page as a template file via wordpress/php...
EDIT**
Ok, I found this, I just have to figure out how to work with it:
When you post your ajax call from javascript using jQuery, you can define the action
which will determin which function to run in your PHP component code.
*
Here's an example:
*
In Javascript we can post an action with some parameters via jQuery:
jQuery.post( ajaxurl, {
action: 'my_example_action',
'cookie': encodeURIComponent(document.cookie),
'parameter_1': 'some_value'
}, function(response) { ... } );
*
Notice the action 'my_example_action', this is the part that will hook into the wp_ajax action.
You will need to add an add_action( 'wp_ajax_my_example_action', 'the_function_to_run' ); so that
your function will run when this action is fired.
You'll be able to access any of the parameters passed using the $_POST variable.
So if anyone is having trouble implementing php functions via ajax in wordpress, buddypress etc. Maybe this will help :)
<form action="<?php activity_post_form_action()?> "method="post" id="whats-new-form" name="whats-new-form">
You have a space after your closing PHP tag. so your action might be showing up like this:
<form action="whatever.php ">
However, I don't think that is your issue.
Your jQuery syntax looks correct. (http://api.jquery.com/load/)
An easy way to double check for Javascript errors you can use Firebug for Firefox (https://addons.mozilla.org/en-US/firefox/addon/1843/)
It could have to do with running on a local server. Do you know the details of your server setup?
EDIT:
If call to undefined function is an error you are getting, it is likely that the standalone PHP file is failing to call functions that are included in the template.
When you include that PHP file on a template page it works fine. HOWEVER, if you call a PHP file via AJAX, it cannot access functions on the page you loaded from.
ajaxphpfile.php
whatever();
Example1.php
function whatever() {
echo "hello!";
}
include('ajaxphp.file.php');
Example2.php
function whatever() {
echo "hello!";
}
jQuery('#window').load('ajaxphpfile.php');
Example2.php will throw an error when loaded using .load() because it can't access the whatever() function. You are trying to call wordpress functions in a standalone php file when you have not included them. Does that make sense?

Categories