laravel print a view without displaying it in browser - php

in my laravel project i need to take print out of a doctor's prescription. and for that i have already created the header part and saved in database. when the user clicks on the printer icon next to a div then the content inside that div needs to be printed with the saved header.
the method is used is: in the view part which contain the printer icon
<div class="AnswerBlock"><div class="NmaeDate">
<span class="name"><?php echo 'Prescription :'; ?></span><span style="float:right;"><img src="<?php echo asset('images/footericons/printout.png'); ?>" title="prescription1" alt="take print" width="20" height="20"></span>
<div class="answer">
<?php echo stripslashes($data1->prescription1);?>
</div>
</div>
</div>
in the user controller:
public function anyPrintPrescription($eid='',$no=''){
$drpres='';
$prs = DB::table('appoint')->where('id','=',$eid)->first();
if($no=='1'){
$drpres = $prs->prescription1;
}
elseif($no=='2'){
$drpres= $prs->prescription2;
}
elseif($no=='3'){
$drpres = $prs->prescription3;
}
$presheader = Admin::where('id','=',$prs->d_id)->pluck('prescription_header');
return View::make('con.printout_prescription')->with(array('presc'=>$drpres,'phead'=>$presheader));
}
and the view is :
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
.print:last-child {
page-break-after: auto;
}
</style>
</head>
<body onload="window.print()">
<div class="print"><?php echo $phead;?></div>
<div class="print"><?php echo $presc;?></div>
</body>
</html>
The output i'm getting is : when the print icon is clicked the printer option prompt window is opening. but at the same time the browser goes to the view url and displays it.
Is anyone knows how to make the window not displaying the view ?

This isn't really a Laravel or even a PHP question, it's a matter of what you can and can't do with a browser.
A browser can't print something without viewing it. The only thing that I can suggest is to open the view in a new window or tab (change the code that calls back to the view) and have it close after the print is done.

Related

Add html inside any tag without javascript

Guys, I have a problem. Is there a way to add html inside a tag without using javascript using only php anyway ?. Thank you very much for your help in advance.
For example, there is this code:
<?php
// This part is required here, because she comes another function.
// It's generate from php server, I need to show inside tag body, for example.
$code = "<h1 style='display:none' id='title'>My String</h1>";
echo $code;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div"></div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js'
integrity='sha512-b6lGn9+1aD2DgwZXuSY4BhhdrDURVzu7f/PASu4H1i5+CRpEalOOz/HNhgmxZTK9lObM1Q7ZG9jONPYz8klIMg=='
crossorigin='anonymous'></script>
<script>
$('#my-div').html($('#titulo').html());
</script>
</body>
</html>
The output is this in the source code:
In the browser, the output is this My String:
But, this manipulation is the gift, which uses javascript for this. I don't want it that way. This will not do, because the code will be shown at the top, before the <! DOCTYPE html> tag. Is it possible, on the server, to insert <h1 style ='display:none' id='title'> My String </h1> inside the boby tag, for example?
How I would like it to look:
Example 2
For example, I have this file with code:
file2.php
<?php
include "file2.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div">
I want to show "<h1>My String</h1>" here.
</div>
</body>
</html>
Yes you can do it as simple as this:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div">
<?php
$code = "<h1 style='display:none' id='title'>My String</h1>";
echo $code;
?>
</div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js'
integrity='sha512-b6lGn9+1aD2DgwZXuSY4BhhdrDURVzu7f/PASu4H1i5+CRpEalOOz/HNhgmxZTK9lObM1Q7ZG9jONPYz8klIMg=='
crossorigin='anonymous'></script>
</body>
</html>
PHP is a server side scripting language and will only parse PHP code on the server side.
Yes, you can place HTML code inside PHP variables like you have done, but that will get rendered into the client (your browser).
What you can do is place $code variable inside the target div, like this:
<div id="my-div"><?php echo $code; ?></div>
Give it a try

what does a "0" at the end of a raw http header mean (header("Location: http://www.sitename.com/0");)

The index.php file in the html document root of a web server contains:
<?php
header("Location: http://www.sitename.com/0");
exit();
?>
...
(see below for full listing but I think nothing gets done b/c the exit())
There is no file or directory in the document root named "0"
Can anyone tell me what it's doing / why it would be used?
When I enter http:/www.sitename.com/ in my browser address bar, the site loads and the address bar displays:
www.sitename.com/0
if I click on a button, a new page loads and the address bar displays something like:
www.sitename.com/0/index.php?toDo=sometext
and it I type in a file name that doesn't exist like this:
www.sitename.com/0/file_not_there.php
it doesn't complain, it just goes too the main page (and leaves www.sitename.com/0/file_not_there.php displaying in the address bar).
Any assistance you can give in helping me understand what's going on is appreciated.
full listing of public_html/index.php:
<?php
header("Location: http://www.sitename.com/0");
exit();
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Site Name</title>
<style type="text/css">
<!--
.style1 {font-family: Arial, Helvetica, sans-serif}
-->
</style>
</head>
<body>
<div align="center">
<p> </p>
<p><img src="/somedir/images/logo.png" /></p>
<p class="style1"><strong>Welcome</strong><br />
</p>
</div>
</body>
</html>

How to place output from an external php file to a specific place on the page

I have looked at other posts here regarding similar questions but cannot find an answer.
Edit I have been playing with this and I want to alter my question somewhat.
I am having a problem placing the output that is echoed from an external php file.
Here is some example code to demonstrate my problem - this is my main file writephp.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test Php </title>
<link rel="stylesheet" href="writephp.css">
</head>
<body>
<div class="allContent">
<?php for($i=0; $i<3; $i++): ?>
<div class="commentBox">
<p> Just a box</p>
</div>
<?php endfor; ?>
Php output
</div>
</body>
</html>
Now I can center this with css:
.allContent {
width: 400px;
margin-left: auto;
margin-right: auto;
font-family: Arial;
}
.commentBox {
border: 1px solid black;
width: 400px;
padding: 0px 5px 5px 5px;
margin-top: 5px;
}
So in the html file the php writes out boxes which are centered in the page since the php loop is within the "allConent" div.
The anchor envokes an external php file which will echo some more boxes.
That file looks like this and is called write.php (writephp.php is the main file this is called from):
<?php
if (isset($_GET['outputHtml']))
{
echo "<p class='allContent commentBox'>" . "This is from external PHP" . "</p>";
echo "<p class='allContent commentBox'>" . "This is also from external PHP" . "</p>";
include 'writephp.php';
}
But the echoed output from the external php is above the output from the main file -in fact it is placed before the <!doctype html> tag , obviously not good.
the page source looks like this:
<p class='commentBox allContent'>This is from external PHP</p
><p class='allContent commentBox'>This is also from external PHP</p>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test Php </title>
<link rel="stylesheet" href="writephp.css">
</head>
<body>
<div class="allContent">
<div class="commentBox">
<p> Just a box</p>
</div>
<div class="commentBox">
<p> Just a box</p>
</div>
<div class="commentBox">
<p> Just a box</p>
</div>
Php output
</div>
</body>
In general my question would be "how do I place html coming from an external php file anywhere I want on the page? "
<?php
if (isset($_GET['outputHtml']))
{
echo "<p class='commentBox'>" . "This is from external PHP" . "</p>";
echo "<p class='commentBox'>" . "This is also from external PHP" . "</p>";
include 'writephp.php';
}
This in your write.php is not correct, see writephp.php is a complete HTML page with <html> <head> and <body> tags and you are including it after echoing some <p> tags, that won't make your new boxes appear magically inside the body of the page, you would be creating some invalid markup with the two <p> at the beginning of the file followed by the <!DOCTYPE><html> etc...
EDIT
Regarding the update to your question that now reads "How place output from an external php file to a specific place on the page"
As #Robert Seddon-Smith correctly noted in his answer, using include will make the content appear exactly at the point when the call is made, so if you wanted to include your new boxes in your main file then the call to include should be backwards, that is, you should include the content inside the main file, you can use this example to test it:
Modify write.php to just echo the boxes when the $_GET variable is present
<?php
if (isset($_GET['outputHtml']))
{
echo "<div class='commentBox'>" . "This is from external PHP" . "</div>";
echo "<div class='commentBox'>" . "This is also from external PHP" . "</div>";
}
?>
And make the include call in your main file writephp.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test Php </title>
<link rel="stylesheet" href="writephp.css">
</head>
<body>
<div id="allContent">
<?php for($i=0; $i<3; $i++): ?>
<div class="commentBox">
<p> Just a box</p>
</div>
<?php endfor; ?>
<?php include 'write.php'?>
Php output
</div>
</body>
</html>
This will include your two new boxes inside the container when you click in the link.
Also you will note that there is nothing special about styling content from an included file, that is because in the end all the markup is rendered as a single HTML page, it doesn't matter if the content is echoed from the main page or the included file, it will obey any rules in your CSS
The problem is that you are not producing the correct content with write.php, probably due to a misunderstanding of what includes actually do.
All the include() function does is take the contents of the included file and parse it as PHP at the point in the file you include it. It has almost the same effect as cutting and pasting the file contents in the same place. Only put in your included file what you might type where it is to be put.
I would rewrite the lot properly so that write.php contains only a function that generates a string of html for your commment boxes:
<?PHP
function commentbox()
{
$output="";
for($i=0; $i<3; $i++)
$output.="<div class='commentBox' id='cb_$i'><p> Just a box</p></div>";
return $output;
}
?>
you include() this at the top of your php file then call its output wherever required by:
echo commentbox();
or, better:
create a footer.php file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test Php </title>
<link rel="stylesheet" href="writephp.css">
</head>
<body>
<div id="allContent">
<?PHP echo $content;?>
</div>
</body>
</html>
which you can use with all your projects very easily.
This is used this way:
<?PHP
include("write.php");
$content="whatever";
$content.=commentbox();
$content.="whateverelse";
include("footer.php");
?>
Even though I have deliberately gone to no trouble to format the above code, it is very easy to find where the content is being created and to debug it. Mixed PHP and HTML code is a nightmare to debug even with code-context-colouring. It is also very difficult to find the bit you want to borrow and put somewhere else later.
Firstly, you shouldn't be using the ID "allContent" on two separate elements. IDs are supposed to be unique. Classes can be used for styling multiple elements.
Why not change it to the code below? Is there a reason you want them to be different?
echo '<div class="commentBox"><p>' . 'This is from external PHP' . '</p></div>';
echo '<div class="commentBox"><p>' . 'This is also from external PHP' . '</p></div>';

Fold/unfold only the div I select instead of all?

Greetings,
I am a newbie who just started with jQuery. I am building a basic PHP app for our team that displays a list of open helpdesk tickets.
To avoid cluttering the screen, I only want to display the title of the ticket and if clicked, its description.
Here's my code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Animate my Div</title>
<style type="text/css" media="screen">
a { text-decoration: none; }
#expand { background-color: #fff; }
#mydiv { display: none; }
</style>
<script src="jquery-1.3.2.js" type="text/javascript"></script>
<script type="text/javascript"> $(document).ready(function(){
$("a").click(function(){ $("div").slideToggle(1000); });
}); </script>
</head>
<body>
<?php
$con = mysql_connect("korg", "joe", "bob");
if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db("wfr11", $con);
$result = mysql_query(" select title, description from webcases");
while ($row = mysql_fetch_array($result)) {
?>
<p><?php echo $row['title']; ?></p>
<div id="mydiv"><?php echo $row['description']; ?></div>
<?php
} mysql_close($con); ?>
</body>
</html>
Right now, my code works but unfolds all the divs for all open tickets, regardless of what ticket name I clicked.
Ideally, I'd like to show the description of for the ticket name I clicked. Not all of them. Is it possible?
Thanks for your help.
Entirely possible, yes.
First issue you have is that you are repeating an ID in the loop, HTML IDs must be unique. I would suggest using a class there. Wrap the two components together in a DIV to make them in to a combined item that can be dealt with individually as well
<?php
while ($row = mysql_fetch_array($result)) {
?>
<div class="entry">
<p><?php echo $row['title']; ?></p>
<div class="description"><?php echo $row['description']; ?></div>
</div>
<?php
}
?>
Then, you need to make your jQuery select just the adjacent description DIV.
$(document).ready(function(){
$(".entry a").click(function() {
$(this).parents('.entry').find('.description').slideToggle(1000);
});
});
Remember to use htmlentities() on your echo's from the database if they are not already in proper HTML form.
Yep, you're after a toggle. Maybe use an id to select the divs you want rather than just the plain div.
$(".link_class").click(function(){ $("#my_div").slideToggle(1000); });
Target particular divs by assigning a class to the link.

$_GET loading content before head tag instead of in specified div

The problem is that when the content loads it displays before the tag before i added the <!DOCTYPE>, <html> <head></head> in the index.php file NOW $content loads before the Body tag... when it should beloaded in the "contents" div.
(1 I'm trying to get file_get_contents to load the links ?=about ?=services etc into to body.tpl in the contents div i have specified with #CONTENTS#
(2 The Dir Tree is as follows
htdocs>
classes> file.class.php
contents> where i want #CONTENTS# (file_get_contents) to grab data from
images> content (changing images)
templates> where the templates are hosted
clean>main template (Files are header.tpl, body.tpl, footer.tpl, styles.css, menu_style.css, and the images folder for images relating to the template itself.)
other templates>(to come)
/* file.class.php */
<?php
$file = new file();
class file{
var $path = "templates/clean";
var $ext = "tpl";
function loadfile($filename){
return file_get_contents($this->path . "/" . $filename . "." . $this->ext);
}
function setcontent($content,$newcontent,$vartoreplace='#CONTENT#'){
$val = str_replace($vartoreplace,$newcontent,$content);
return $val;
}
function p($content) {
$v = $content;
$v = str_replace('#CONTENT#','',$v);
print $v;
}
}
if(!isset($_GET['page'])){
ob_start();
// if not, lets load our index page(you can change home.php to whatever you want:
include("main.txt");
// else $_GET['page'] was set so lets do stuff:
} else {
// lets first check if the file exists:
if(file_exists($_GET['page'].'.txt')){
// and lets include that then:
include($_GET['page'].'.txt');
$content = ob_get_contents();
ob_end_clean();
// sorry mate, could not find it:
} else {
echo 'Sorry, could not find <strong>' . $_GET['page'] .'.txt</strong>';
}
}
?>
if some one could trim that down so it JUST is the template required code and file get contents.
/* index.php */
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<meta name="robots" content="index,follow"/>
<meta name="distribution" content="global"/>
<meta name="description" content=""/>
<meta name="keywords" content=""/>
<link href="templates/clean/style.css" rel="stylesheet" type="text/css" media="screen" />
<link rel="stylesheet" href="templates/clean/menu_style.css" type="text/css" />
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
</head>
<body> //everything before this USED to be in header.tpl but i moved it here until fix so that w3c validator would validate my code
<?php
include('classes/file.class.php');
// load the templates
$header = $file->loadfile('header');
$body = $file->loadfile('body');
$footer = $file->loadfile('footer');
// fill body.tpl #CONTENT# slot with $content
$body = $file->setcontent($body, $content);
// cleanup and output the full page
$file->p($header . $body . $footer);
?>
/* header.tpl */
<div id="header">
<div id="logo"><img src="images/logo.png" border="0" alt=""/></div>
<div id="menuo">
<div class="menu">
<ul id="menu">
<li>Home</li>
<li>About Us</li>
<li>Services
<ul>
<li>InStore Repairs</li>
<li>InHome Repairs</li>
<li>Website Design</li>
<li>Comming Soon.</li>
</ul>
</li>
<li>Products
<ul>
<li>Computer Hardware</li>
<li>Monitor's</li>
<li>Laptop + Netbooks</li>
<li>Comming Soon.</li>
</ul>
</li>
<li>Contact</li>
</ul>
</div>
</div>
</div>
<div id="headerf">
</div>
/* body.tpl */
<div id="bodys">
<div id="bodt"></div>
<div id="bodm">
<div id="contents">
#CONTENT#
</div>
<div id="bodb"></div>
</div>
</div>
/* footer.tpl */
<div id="footer">
<div style="position:absolute; top:4px; left:4px;"><img src="images/ff.png" alt="ok"></div> <div style="position:absolute; top:4px; right:5px; color:#FFFFFF;">©2010 Company Name</div>
</div>
</body>
</html>
My Firefox Showing The Problem In Action
Edited answer:
I'd originally assumed that the p() method was an accidental method you were calling, but that appears (according to the update you made to fix the fact that both pages were the same) to be a red herring. The issue is similar maybe though.
When you:
include('classes/file.class.php'); as part of your index.php, one of the things it does is define the methods and then run a few if statements, as part of this change it includes a .txt file. (e.g. include("main.txt");).
My guess is what you are seeing is the contents of this include file being displayed to screen. What's the point of them being included? Can you just remove them? Should you really just be reading the content to a variable instead of including it (and effectively echo'ing/printing it to screen)?
Try removing them and seeing if that solves your issue - but otherwise, check to make sure your template files contain what you think they contain. :)
I'm a little confused, as your index.php code is the same as your file.class.php code (is that a mistake?). But if I had to guess your problem might be in the print line of the $file->p() function:
function p($content) {
$v = $content;
$v = str_replace('#CONTENT#','',$v);
print $v;
}
This function is fine, depending on when you call it. If you haven't output anything to the screen yet, and you call p() and pass it the contents of body.tpl, it could result in the incorrect HTML output you're getting.
get firebug and check the styles applied to the content div and the text. it seems like a position problem.

Categories