load php file on load with jQuery - php

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>

Related

HTML being displayed as text

When I load the following code using php...
<?php <h1>Some text</h1> ?>
the tags are printed as text -
<h1>Some text</h1>
Any ideas?
Found the answer - see my answer.
You need to output the actual HTML code if you are placing any HTML code within PHP.
You can achieve this using echo().
<?php
echo("<h1>Some text</h1>");
?>
<h1><?php Some text ?></h1>
why dont you try this.
You must print it in order for it to appear the way you want it to
<?php echo"<h1>Some text</h1>" ?>
like this
Found the issue, the content-type was set as json.
<?php echo "<h1>Some text</h1>"; ?>
<?php echo "<h1>Some text</h1>"; ?>
if you want to show a variable
<?php
$_varTest=2;
echo "<h1>Show variable {$_varTest}</h1>"; ?>

Include php file in html page

I'm working with XML and PHP to populate a web form drop down box.
I have a html page
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<header>
<h1>Title</h1>
</header>
<form>
<div id ="select xml">
<?php include 'dropdown.php'; ?>
</div>
</form>
</body>
</html>
and I am hoping to include the following PHP to generate the actual box with the file names of the XML.
<?php
//echo(substr(glob("xml/*.xml")[0],4));
echo "<p>
<label>Select list</label><br>
<select id = \"selectxml\">
<option value'0'>--Please Select--</option>";
$count = count(glob("xml/*.xml"));
$files = glob("xml/*.xml");
for ($i = 0; $i < $count; $i++) {
//echo(substr($files[$i],4));
//echo "<br>";
$filename = (substr($files[$i],4));
echo "<option value=$i+1>$filename</option>";
}
echo "</select><br>
<br>
</p>";
?>
I'm aware the PHP isnt perfect, but it works.
The issue is that the Include HTML does not work when I run the page - any ideas?
Change the extension to .php and you will be okay .
When the page have .html extension, the web server doesn't recognize it as a PHP file, and you can't use PHP codes in it. and any PHP code, will be processed as a plain text .
I mean when you put :
<?php echo "hello" ?>
in a HMTL page, browser will show :
<?php echo "hello" ?>
But, if the page have .php extension, browser will show :
hello
Rename your html page from .html to .php
it;s impossible, but with jQuery.load function you can do this :
$("#select-xml").load("dropdown.php");

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.

link not working with jquery

Probably a very simple answer, but I cannot seem to find a working solution. I am creating links from a php search script and it generates links based on query. I have written a sample jQuery script to open a div based on a tag being clicked, but when I click link, nothing happen. I cannot see any errors in firebug and would appreciate somke help. Thank you.
UPDATE: Added html and changed mailLink from id to class.
jQuery
$("a").click(function(e) {
e.preventDefault();
$('.mailShow').fadeIn(1500).html('This is the mailShow div');
});
PHP
<?php
while ($row = mysql_fetch_assoc($rsd))
{?>
<div class="each_rec"><?php echo $row['name_usr'];?> <?php echo $row['idcode_usr'];?></div>
<?php
}
if($total==0){ echo '<div class="no-rec">No Record Found !</div>';}
?>
HTML
<div id="content">
<div class="search-background">
<label><img src="loader.gif" alt="" /></label>
</div>
<div id="sub_cont">
<div class="mailShow"></div>
</div>
</div>
Generated HTML from firebug
<div class="each_rec">Demo User DEMO</div>
In jquery you have .mailShow and in PHP you use id="mailLink". You should change .mailShow to #mailLink.
JSFiddle for testing.
JSFIDDLE
Try this code:
$(document).ready(function () {
$(document).on("click","a",function(e) {
e.preventDefault();
$('.mailShow').fadeIn(1500).html('This is the mailShow div');
});
});
<?php
while ($row = mysql_fetch_assoc($rsd))
{?>
<div class="each_rec"><?php echo $row['name_usr'];?> <?php echo $row['idcode_usr'];?></div>
<?php
}
if($total==0){ echo '<div class="no-rec">No Record Found !</div>';}
?>
I see two probable issues:
1. You didn't use $(document).ready(); and put your code outside of it. Then you need to put the above code to docyument ready.
2. You inseert links dinamycaly - from ajax query. Thus you need to use jquery delegates instead.

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.

Categories