I've been asked to provide some tricky printing functionality to a dynamicly generated page. The page is to display each record along with a checkbox. Then one print button at the bottom of the page. The idea is to let the user check the box next to each record they want to print, then when they click the print button at the bottom of the page, only those records that were selected will be printed.
I'm assuming some combination of CSS and Javascript will be required, but I really don't know. Any ideas? Thanks in advance!
PHP solution
If you have something like
<form method="post" action="print.php">
<p>Select the records to be printed</p>
<p>
<input type="checkbox" name="records[]" value="1"><div class="record">Record 1</div><br>
<input type="checkbox" name="records[]" value="2"><div class="record">Record 2</div><br>
<input type="checkbox" name="records[]" value="3"><div class="record">Record 3</div>
</p>
</form>
You will automatically get an array $_POST['records'] with all the values of the selected records in it in your php script print.php. (Note that this only works if the name of all the checkboxes is the same and ends with [])
The php script could then do something like:
<?php
foreach ($record in $_POST['records']) {
// fetch the record with id $record from the database (or similar)
// print this record to the page
}
echo "<script>window.print();</script>"; // or put that into the onload attribute of the body tag
?>
JavaScript and CSS
An other valid possibility would be to do the whole thing via javascript. Therefore you would have to check every checkbox if it's checked when one clicks the print button and then removes or hides all the records that were not checked.
A helpful thingy is also the css property media with which you can define extra styles that only apply to certain devices. This code sould be able to do everything without any javascript except an attribute onclick="window.print()" in your print button:
#media print {
input{
display: none; /* hides all input elements for the printer */
}
.record {
display: none; /* hide all records by default */
}
input[checked] + .record, input:checked + div.record {
/* display all that are immediately preceeded by a input with checked attribute */
display: block;
}
}
Note that I tested it only in Safari (where the pseudoclass :checked leads to the wanted result and not the attribute selector) and only will work for newer versions of browsers with css3 and selector support.
Create a print.php to which you post the form (with selected checkboxes) - the page will display only the selected content exactly as you want it to look in the print and then call javascript window.print() onload.
Related
I am attempting to display a confirmation box, using Jquery, after a user has clicked a delete link. When the delete link is clicked it sends the post_id to the url. As long as this is set and it isn't empty I trigger the animation which will display the hidden confirmation box.
This is what I have so far which isn't currenlty working:
// if admin wants to delete a post check for post_id
if(isset($_GET['post_id']) && !empty($_GET['post_id'])){
$delete_id = (int)$_GET['post_id'];
$animate = true;
echo '<script type="text/javascript">';
echo 'var animate = '.$animate;
echo '</script>';
}
The GET variable is set correctly.
In my jquery file I have:
$(document).ready(function(){
if(animate == true){
$("#delete_confirm").fadeIn('3000','swing');
}
});
and the confirmatino box:
<div id="delete_confirm">
<p>Please confirm you want to delete this post.</p>
<input type="button" id="delete" name="delete" value="confirm" />
</div>
Which has display:none; set in the style sheet.
Why does it not display when the animate variable is set to true?
Thanks
UPDATE:
Try this, tested and working:
<?php
if(isset($_GET['post_id']) && !empty($_GET['post_id'])){
$delete_id = (int)$_GET['post_id'];
$animate = true;
}
?>
<html>
<head>
<title>Simulation</title>
</head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
var prop = {
animate: "<?php echo $animate; ?>"
};
$(document).ready(function(){
if(prop.animate == 1){
$("#delete_confirm").fadeIn('3000','swing');
}
});
</script>
<body>
<div id="delete_confirm">
<p>Please confirm you want to delete this post.</p>
<input type="button" id="delete" name="delete" value="confirm" />
</div>
</body>
</html>
You should unset the display:none; property on the div and either call .fadeOut(0) on page load, or set its opacity:0.0; in the CSS file.
Additionally, instead of including all of this server side code injection stuff, you could handle the GET parameter check on the client side by using the window.location object's search property, then detect if a particular parameter is set with a regex match. That way you don't have to use any inline JavaScript.
var matches = window.location.search.match(/post_id/g);
if(matches && matches.length > 0)
{
// do animation
} else {
// do something else
}
Something like this should work, and personally I like doing this more than injecting script tags and inline JavaScript.
The php checks is the GET variable is set, if it is it sets $animate
to true. After this the Jquery does the check on the animate variable,
if its set to true then the fadein should happen.
For me this is very confusing. Let me try to research this a little bit more.
You have a page with the confirmation box.
When user press delete button the confirmation box (<div id="delete_confirm">) is show using jQuery script.
User press the 'delete' button on the page. What happens here? There are two options:
You do AJAX request.
You do HTTP GET request.
Based on your comments and the code I can guess that you do HTTP GET request. In this case you reload the page. Yes, PHP gets the proper value in $_GET['post_id'] and the post gets deleted.
But you get completely new HTML page. And on this page the div <div id="delete_confirm"> is invisible from the very beginning. This is why it is not shown and hidden.
When you look at the source code you can find you JS script <script type="text/javascript">... and I beg that the code $(document).ready(function(){... works too. But you get it on newly loaded HTML page.
Please correct me if I get you wrong here.
I think that you want to hide the confirmation box you might want to use AJAX. But then you might also want to remove the post from the page by altering the page.
Quoting the jQuery documentation: "The .fadeIn() method animates the opacity of the matched elements."
What .fadeIn() will do, effectively, is change the css opacity of an element from 0 to 1.
You've stated that in the CSS your element has display: none; so all you're actually doing is changing the opacity from 1 (as assuming you've not set that in the CSS) to 1 whilst leaving the display set to none ... so it'll never be displayed.
This seems to hold true for .animate() which doesn't allow you to "animate" the display type (from "none" to "block" - you'd use opacity)... but not for .fadeIn() / .fadeOut() which do allow you to fade a block in/out using the CSS display property.
My bad, I was confusing the two.
I have a PHP script that queries a database and returns the rows as li elements to a div in a jQuery Dialog. It does this by building an array in the PHP while loop that processes the query row responses. So far, so good. It displays a set of rows inside the Dialog in a div with an id=dialogResponse . It is the first two parts (2 rows of code) of the array instruction below. BTW, it is just li elements - there is no ul.
Now, I want to put a jQuery Button in each li response to give the user an action choice to inactivate/pause that posting. I'm pretty new at this, and I can't get a button to appear or anything to work. I get an Internal Server Error message so I can't tell if it is a selector problem, php syntax, or something else.
How do I get a jQuery button at the end of each li that will be the trigger point for a function that takes action on that row in the database? The event.preventDefault is a placeholder for a future function that will do a MySQL UPDATE on the selected streetAddress and city. Here's the code.
$messages[] = "<li>
$storedStreetAddress, $storedCity
"<script type="text/javascript"> $(function() { $( "#dialogResponse li")
.button( label: Pause posting).click(function( event ) event.preventDefault();});});
</script>"
</li>";
Please be specific and code helps a lot. I don't follow general instructions well in this area.
Why does the <script> have to be printed in with the PHP? Why not have it included with in a JS file? For the button, why not create a standard HTML [or whatever your preference] in each <li> and have jQuery target them for the actions? - You will need an <a>, <input>, <button>, etc. for the .button()
On one of my pages in my website(coded in php), i'm trying to add 2 (even more) print buttons each one printing a portion of the webpage. For example, on a page there is a table with some values and 2 graphs underneath it. I would like to add 2 print buttons, one button printing the table and the other one printing both the graphs. I've found this Example but could not understand clearly. Any help or examples would help me.
Thanks in advance.
This is html/javascript code that will launch the browser's Print dialog when clicked.
<button onClick="window.print()">Print this page</button>
If your table is inside a div with id='printTable' use:
Click to print table
EDIT: Here is the function "printContent()"
<script type="text/javascript">
<!--
function printContent(id){
str=document.getElementById(id).innerHTML
newwin=window.open('','printwin','left=100,top=100,width=400,height=400')
newwin.document.write('<HTML>\n<HEAD>\n')
newwin.document.write('<TITLE>Print Page</TITLE>\n')
newwin.document.write('<script>\n')
newwin.document.write('function chkstate(){\n')
newwin.document.write('if(document.readyState=="complete"){\n')
newwin.document.write('window.close()\n')
newwin.document.write('}\n')
newwin.document.write('else{\n')
newwin.document.write('setTimeout("chkstate()",2000)\n')
newwin.document.write('}\n')
newwin.document.write('}\n')
newwin.document.write('function print_win(){\n')
newwin.document.write('window.print();\n')
newwin.document.write('chkstate();\n')
newwin.document.write('}\n')
newwin.document.write('<\/script>\n')
newwin.document.write('</HEAD>\n')
newwin.document.write('<BODY onload="print_win()">\n')
newwin.document.write(str)
newwin.document.write('</BODY>\n')
newwin.document.write('</HTML>\n')
newwin.document.close()
}
//-->
</script>
A simple idea is to create some previous css printing rules ad hoc, e.g.
.printtable * { display : none; }
.printtable table { display : block; }
and
.printtableandgraph * { display : none; }
.printtableandgraph img { display : block; }
then the two buttons should add a .printtable and a .printtableandgraph class for the body element (or other element containing your table and graphs) just before window.print() statement
(if user click twice or more on the same print button check previously if the element has already set that class name)
Print Button
<button onClick="window.print()">Print this page</button>`
Use This code for print any webpage there is only one draw back its print all the button and menu bar.
To print a portion of the page, you can use a print.css and the print button for a quick and simple approach.
Set up print.css file and include:
body {visibility:hidden;}
.print {visibility:visible;}
Add the .print class to the appropriate portions of your page.
Add button:
<button onClick="window.print()">Print this page ยป</button>
Print results are neat and clean.
I have searched before posting but none of the posts are that relevant (saying that i am new to Jquery).
My problem is when i click the "add friend" button it wont fire upon a single click but it needs a double click.
These are the relevant sections of code:
Setting the div:
$fField = <div style="display:inline; border:#CCC 1px solid; padding:5px; background-color:#E4E4E4; color#999; font-size:11px;">
Add Friend
</div>;
Javascript:
function friendArea(x){
//alert(x);
if($('#'+x).is(":hidden")){
$('#'+x).slideDown(200);
} else {
$('#'+x).hide();
}
$('.friendSlide').hide();
}
the message to display (the one which needs 2 clicks to be displayed)
<div class="friendSlide" id="addFriend">Are you sure?</div>
Any help would be much appreciated! Oh and I'm using the latest version of jQuery.
Hmm, well, it looks like you are showing and hiding your div at the same time:
HTML:
<div class="friendSlide" id="addFriend">Are you sure?</div>
JS:
function friendArea(x){
//alert(x);
if($('#'+x).is(":hidden")){
$('#'+x).slideDown(200);
} else {
$('#'+x).hide();
}
$('.friendSlide').hide();
}
You call friendArea('addFriend'), which is supposed to slideDown your div, but at the end of the function you hide .friendSlide, of which your div is also a class. You basically hide it every time. This can't be what you want.
Comment out that line and it works: http://jsfiddle.net/Ggdhp/
Of course, you might have had a different reason for that, but I think you are getting some divs and classes mixed up.
Note:
I would also suggest that you don't use inline css or javascript. It always makes debug much more difficult, and you miss typos as you end up having to put everything on a single line and escape quotes. For instance, color#999; in your div style is missing a colon.
I'm not entirely sure that's the relevant part of the code, but if you want to learn how to bind functions to click/doubleclick/etc:
http://api.jquery.com/category/events/mouse-events/
I assume you have a button/link somewhere that has something to the extent of:
Add Friend
What you would want to do instead is give that button a class indicating that it's an add friend button, and use jQuery to add the double click function to it.
Add Friend
$('.addFriend-button').click( function(e) {
friendArea( $(this).data('friendName') );
} );
This will cause your function to fire when the button is double clicked, not single clicked. It also allows you to only have to write 1 function, and you can pass the relevant data in using the data-* attributes on the link element.
if you want to run any code on double click over an element use jquery like this,
$('#target').dblclick(function() {
alert('Handler for .click() called.');
});
and one more thing use jquery toggle to display and hide elements like below, On your function
if you are using class name of selector use the selector like below (use .) in case of id use # with id name like above code
$('.target').toggle();
if double clicks works for you to use below code, it converts the single click to double click by jquery. Hopes this will help you
$('#target').click(function() {
$('#target').dblclick();
});
I am trying to create a list of links that would make up the elements of a form (will be used as a search feature).
Basically, each link in the list represents a search category, so as users click on a link, their search results will be filtered.
I would need a category value to be able to be passed whenever someone clicks on one of those category links, and also have each link act as the form submit as well. Is that possible?
(There may be easier ways to accomplish this, however with the CMS and search module I'm using, this will have to do.)
Are you trying to pass values via both POST and GET? I am not sure that that would work. Why not just used hidden form fields? In your form, add a number of <input type="hidden" name="foo" value="bar"> and each one of them will be passed back to the server along with all the regular form fields.
Note: Hidden form fields can be read and edited by a sufficiently tech savy user (it is not too hard), but this would be the case with any variables that you are passing between the server and client, even cookies.
If you give the links an internal data-category attribute like this:
Foo
then you can do with jQuery:
$('a[data-category]').click(function() {
filterResults($(this).data('category')); // call the function that filters
// results according to chosen
// category
$('#theform').submit();
}
You can do something along the lines of...
Puma
and use the a script like...
$('a.category').click(function(e) {
$('#someForm input[name=category]').val( $(this).text() );
$('#someForm').submit();
});`
I'm not 100% sure what you intend to do with the data from the link, or even which part of it you need, but I hope this helps.
<form name="myForm">
link
</form>
<script>
function submitMyForm(link){
var mylinkText = link.text;
var myLinkHref = link.href;
document.myForm.submit();
}
</script>
If you need the link clicked to come across as part of your form data, perhaps you could put it in a hidden field on the form... something like:
document.myForm.myHiddenLinkField.value = mylinkText;
Since the jQuery tag has been removed, here's an example of doing this form submit with pure javascript:
http://www.neubreed.com.au/blog/2010/07/submit_form_anchor_tag_using_javascript_and_supply_action
You can create an extra hidden input like in the example to set your category:
function submitForm(id, category){
var myform = document.getElementById(id);
if (document.createElement) {
input = document.createElement('input');
input.type = 'hidden';
input.name = 'category';
input.value = category;
myform.appendChild(input);
}
myform.submit();
return false;
}
Then, in your form:
click
You could work with a hidden field.
<form id="cuteform" action="thedestiny.php">
<input type="hidden" id="filter"/>
</form>
<a onclick="javascript:linkaction(this);" id="linkA">blablabla</a>
<a onclick="javascript:linkaction(this);" id="linkB">blablabla</a>
<script>
function linkaction(link) {
// get the field
var f = document.getElementById("filter");
// then you could use some of the link element's property like id or innerHTML
f.value = link.id;
// or you could use a switch structure
switch (link.id) {
case "linkA": f.value = "the huge filter string"; break;
case "linkB": f.value = "another stuff"; break;
}
// submit the form
document.getElementById("cuteform").submit();
}
</script>
I'd be inclined to style an actual form button as a link using CSS.
So essentially for each "link":
<form method="get">
<input type="submit" class="form-link">
</form>
For the CSS (off the top of my head) something like this should do it:
.form-link {
background-color: transparent;
border: none;
text-decoration: underline;
color: #00f;
}
That way it isn't dependent on JavaScript to work and users without it will still be able to make use of your site. Additionally, inline event handlers (onclick) etc shouldn't be used as they mix semantics with behaviour. If the functionality isn't core to the experience and (perhaps therefore doesn't require a base non-JavaScript version) then write the links into the page with JavaScript from an external JavaScript include. You won't then have a bunch of dead links in the page for users that visit your site with JavaScript disabled or from a non-JavaScript capable device.