PHP: Returning Data to a Web page - php

I have an application that returns data to a PHP/HTML file but the issue is it can call my application many times per page load:
An example HTML file with my calls only asking for images:
<img border="0" alt="" src="http://mydomain.com/test/index.php?element=1"></img>
</br>
<img border="0" alt="" src="http://mydomain.com/test/index.php?element=2"></img>
</br>
<img border="0" alt="" src="http://mydomain.com/test/index.php?element=3"></img>
</br>
This works fine every time and all of the images are returned, however my trouble starts when I want to return text/html.
The server side code works fine:
header('Content-type: text/html');
ob_start();
echo $filepath;
ob_flush();
ob_end_clean();
$filepath contains some text or HTML such as, "Check out our <strong>NEW</strong> offers!"
All I want is to return the HTML text and allow it to be embedded within the page so that if they want:
<h1>Check out our <strong>NEW</strong> offers!</h1>
or
<p>Check out our <strong>NEW</strong> offers!</p>
It will work fine.
The problem is, which HTML Tag should I use to call for the data in the same way I use <img> for images?
I have tried <object>, `
` etc... but these are no good as all I want is the raw HTML data returned.
Using the Javascript load() is not good as it relies on the web page builder creating a function call for every time they want to get data.

You can't do it with HTML, you could use jQuery + AJAX.

You have to use Ajax. you cannot do it in html.

You can output it with PHP and then if you update the file it will update site wide without making changes everywhere. Is there a reason you want to do it this way and not just insert the code manually? This could cause problems with the caching of your pages depending on how dynamic they are.
newOffers.php
<?php
echo '<h1>Check out our <strong>NEW</strong> offers!</h1>';
?>
Then in your site source:
<p>Blah blah blah...</p>
<?php include 'newOffers.php'; ?>
<p>Click our new offers link above</p>

You can use an <iframe> for this, but this is not really ideal as the resizing is not done automatically.
Alternatively you can indeed use Ajax to change the code.
Either way: think about your reason for wanting to include the HTML like this instead of just returning this html code with the original return?

Related

PHP Snipped not rendering in iframe

I'm trying to open the user_url within an iframe on a private user page in Wordpress.
I put the following code into a snippet:
<?php echo get_the_author_meta('user_url',$userID); ?>
When I use the short code on the user page it does display the user URL, I can also use it in an hyperlink like this:
link text
But when I put the short code into an Iframe the short code does not change into the URL anymore.
<iframe src="[cmruncode name='dash']" height="300" width="300" title="Iframe"></iframe>
Does anyone knows why this is not working and know a solution?
Thanks Peter
If you are using WordPress shortcode then you have to call shotcode using function do_shortcode.
You can try this one
<iframe src="<?php echo do_shortcode( '[cmruncode name="dash"]' ); ?>" height="300" width="300" title="Iframe"></iframe>
And I'm not sure why anchort text display right output. That's weird.
Anyway if you used do_shortcode to output your shortcode then it will work every place.
This only works when I put it directly into the template, in that case no snippet-shortcode is needed at all. But the content needs to be hidden when the user is logged-out.
Normally the plugin "client portal" that I use should take care of this
but it has a content block that does not support PHP and also does not render the php snippet that I made.
Any suggestions?
The solution was to put the iframe code completely into a snippet.
Like this:
<iframe src="<?php echo the_author_meta('user_url',$userID); ?>" height="2400" width="100%" title="Iframe"></iframe>

How best to return HTML content from a PHP file

I have an application where I need to serve data back to a web page from a PHP file.
I can do this easily for images as I use the<IMG> tag with the 'src' being the URL to the PHP page and then using readfile() to get the image to return. It works a great.
However I am having an issues trying to return straight text with HTML tags embedded.
I can send the data OK, using:
header('Content-type: text/html');
ob_start();
echo $data;
ob_flush();
ob_end_clean();
$data contains a string of HTML, for example: "We have 100's of <strong>GENUINE</strong> Men"
but I am struggling to find the right HTML TAG to use in the first place, for example:
For images I use:
<img border="0" alt="" src="http://mydomain.com/test/index.php?element=2"></img>
at the moment for text I have used:
<object id="page" type="text/html" data="http://mydomain.com/test/index.php?element=1">
</object>
but the problem with the object is that although it receives the text in HTML format fine the <object> tag is not good to use as I have to set up a class for width etc.
I really need a way of the browser simply rendering what I send, with nothing else.
Set your header before outputting, by default if no Content-Type header is specified, PHP will send it as text/html
Override it using:
// Headers are always set before any output
header("Content-Type:text/plain");
ob_start();
echo $data;
ob_flush();
ob_end_clean();
Ok, I don't know why don't you use PHP itself to include your HTML to the original HTML file, but how about an iframe?
<iframe src="./something/to/include.html?element=potassium"></iframe>
Or, if server supports it, you can use SSI include:
<!--#include file="./something/to/include.html?element=potassium" -->
Or, if you want to use jQuery (or you're using jQuery anyway), you could do something like this:
<script type="text/javascript" src="jquery.js"></script>
<script>
$(function(){
$("#whereToPutIt").load("./something/to/include.html?element=potassium");
});
</script>

How to embed a graph (jpgraph) in a web-page

I am using this script which is one of the examples provided by jpgraph itself. When I put this on a web-page (blank) by itself, it's drawing the graph. But when I embed the code in already existing web-page (with some content), it ain't drawing a graph.
GD is already enabled according to phpinfo(). Iam using jpgraph 3.5.0b1.
The problem is that you are mixing HTML/text output with image output.
Any time you have a PHP script generate graphical content you have to handle the output differently than normal HTML or text.
There are a few routes, I'll cover them briefly here.
Save the output to a file and use that filename in your HTML
//replace this line:
// Display the graph
//$graph->Stroke();
// with these lines:
// Default is PNG so use ".png" as suffix
$fileName = "/tmp/imagefile.png";
$graph->img->Stream($fileName);
.. then use $filename in an image tag, like this (for example):
print '<img src="'.$filename.'" />';
Create a standalone PHP script that will output the graphic
You can use the example script as-is, alone in a file called graph_render_script.php. Then, in your HTML, you use that script as a source:
<img src="graph_render_script.php" />
Output base-64 encoded data
Another route is to use base-64 encoded image data. This is relatively simple to do:
print '<img src="data:image/png;base64,'.base64_encode($graph->Stroke()).'" />';
As always, the documentation should be your guide!
Documentation
http://jpgraph.net/download/manuals/chunkhtml/ch05s05.html
base64_encode - http://php.net/manual/en/function.base64-encode.php
This worked for me:
putting the php code that generates the image in a file...Then on my html page I do this:
<img src="graph.php" >
embedding the graph inline is indeed possible. You'll have to use output buffering to capture the image data, then base64 encode that data, then use that base64-encoded string as the source in an <img>.
Try something like this:
$img = $graph->Stroke(_IMG_HANDLER);
ob_start();
imagepng($img);
$imageData = ob_get_contents();
ob_end_clean();
?><html>
<head>
<title>JpGraph Inline Image Example</title>
</head>
<body>
<h1>JpGraph Inline Image Example</h1>
<img src="data:image/png;base64,<?php echo(base64_encode($imageData)); ?>" />
</body>
</html>
ceejayoz made an excellent point in that this method is almost never what you want. I do not recommend embedding the image data like this unless you have a good reason to do so and understand the downsides, i.e. older browsers lack support for it, and the page size is dramatically increased (not only from the image data but the fact the image data is base64 encoded, which balloons the length). I've used this method in the field myself on a project last year, but it was only because the client refused to make a second request for the image.
But when I embed the code in already existing web-page (with some content), it ain't drawing a graph.
You can't do that - you can't output an image as raw binary data within a page.
You need to put the code that generates the graph in a separate file, and use an image tag.
<img src="path/to/jpgraph/chart.php" />
The graph needs to be on its own page, you can't embed it. It outputs a raw JPG and you need to have no other content sent and have the proper headers to tell the browser it's a JPG. To embed the graph you'd make a different page called stats.php for example, and on that page you'd make an image tag pointing to the stand alone graph.
<img src=graph.php>
I've had this problem many times, I've noticed it happens when you have require() or include() in your Chart's script and those scripts have Data Base connections or special configurations.
I've solved this problem separating the data retrieving and the Chart drawing, passing parameters to the script or using SESSIONS to get the values.
Example of Embed image Chart in your PHP or HTML file:
<img src="linear_graph_customer.php?values=1,2,3,4|1,2,3,4|1,2,3,4&title=CHART&width=500&height=300" width="500" height="300" class="img" />
Regards.

Loading php pages with jquery

Loading an external php file with jquery is actually easy. I simply use the load() event.
Like this:
$("#someelement").load('somepage.php');
Typically it's just fine, until I use the image manipulation functions.
Here's what I put inside somepage.php
<?php
$img = 'tmp/someimage.png';
$img = imagecreatefrompng($img);
header('Content-type: image/png');
imagepng($img);
?>
When somepage.php is loaded, I get messy code back. (which I'm not sure what it would be considered)
I'm pretty sure there's a limitation to loading complicated image functions, but I thought I'd ask if there's a workaround.
As #onteria_ pointed out, .load() isn't for image data. It's for HTML.
You will have to create an <img /> tag, set its src= attribute, and then append it to your element:
$('body').append($('<img id="someelement" />').attr('src', 'somepage.php'));
If you want to print an image, just append <img src="somepage.php" /> where you want with jQuery.
Since you are trying to load the file like a text, jQuery trying to get the contents of it. Loading an executed php file that echoes texts is one thing, printing images is another.

Putting together a js file to use in html

So I have found this code here
This basically lets me pull some data from a php file and place it into a div using jQuery. So everything from the tutorial works fine, but because I am thinking of putting together about 9-10 different links via that code, I thought I put all the function code in a js file and just call the function in html. Just to clarify, I have very minimal understanding of javascript, though I've experience with php.
In a new js file called links.js, I have put 2 different functions which I would like to call from my html
function getshow1() {
$('#sidebar').load('show1.php');
}
function getshow2() {
$('#sidebar').load('show2.php');
}
then in my html, I src the links.js and for 2 images, I've put this,
<img src="image1.jpg" />
<img src="image2.jpg" />
and of course #sidebar doesn't display anything. How could I format the js file so I can just use the above html code to link different php files?
also, if there is a better way of pulling html code from a external file into a div, do let me know!
Thanks!
html:
<img src="image1.jpg" />
<img src="image2.jpg" />
jQuery:
$(document).ready(function(){
$('#show1').click(function(){
$('#sidebar').load('show1.php');
});
$('#show2').click(function(){
$('#sidebar').load('show2.php');
});
});
edit: added document ready function

Categories