Welcome to WebmasterWorld Guest from 54.159.190.106

Forum Moderators: incrediBILL

Message Too Old, No Replies

Open in new tab/window

   
7:27 pm on Feb 16, 2012 (gmt 0)

10+ Year Member



Not sure if this post should be in php, javascript or here.

Here's the situation, I have a php/mysql search page, a record can be selected for further processing. The further processing are edit and save the record, and print. This is achieved using two "submit" buttons on the search page. The action for both goes to a page with the following.

<?php

if (isset($_POST['print']) && ($_POST['print'] =='Print form'))
{
$recordID = $_POST['recordID'];

header('Location: print_form.php?recordID='.$recordID);//send to print page//
} else {
if (isset($_POST['edit']) && ($_POST['edit'] =='Edit form'))
{
$recordID = $_POST['recordID'];

header('Location: customer_edit.php?recordID='.$recordID);//send to edit page//
}
}
?>


This works fine except I'd like the "print" action to open in a new tab/window. So I tried replacing the header() redirect with:


echo '<meta HTTP-EQUIV="REFRESH" content="0; url=print_form.php?recordID='.$recordID.' "target="_blank">';


I'd have thought that this would open the print page in a new tab but it doesn't, it opens with the correct record but in the same window. Have a made a typo anywhere or can anyone suggest a way to get the page in a new tab/window.
1:10 am on Feb 17, 2012 (gmt 0)

WebmasterWorld Administrator incredibill is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Try something like this:

<meta HTTP-EQUIV="REFRESH" CONTENT="0; URL=javascript:window.open('sample.htm','_top');">
2:26 pm on Feb 17, 2012 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



By the time you reach the server side code, it's too late. You've already left the page you were on.

One option would be to add an onsubmit handler to your form that would check to see which button was pushed. If it was the Print button, then (via JavaScript) set the target attribute on the form to be "_blank" before continuing.
6:04 pm on Feb 17, 2012 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Think about the logic. On a previous page, you submit a form.

You land on this "page" (the PHP code you posted.)

You figure out a way to do what you ask, and a new window/tab indeed does open.

What happens to THIS window/tab?

It's blank. Or maybe you can devise a way to repurpose it ("Your page has been printed") but seems like it may serve more to confuse.

I'd do something like this with your form:


<form action="whatever.php" method="post">
<input type="hidden" name="recordID" id="recordID" value="1234">
<p><input type="submit" id="editButton" value="Edit"></p>
<p><input type="submit" id="printButton" value="Print"></p>
</form>
<script type="text/javascript">
window.onload=function() {
var p = document.getElementById('printButton');
if (p) { p.onclick=function() { printWindow(this.form); };
};
//
function printWindow(frm) {
if (frm.recordID) {
var day=new Date();
var id=day.getTime();
var url = 'print_form.php?recordID='+frm.recordID;
window.open(url,id);
return false;
}
</script>

(Not copy and paste code, typed on the fly, may contain errors)

What this should do: if JS is enabled, it will open a new window for print_form.php. Return false on the function stops the form from submitting, so users stay on the same page under the new window/tab.

The bit with day/id in javascript assigns a semi-unique window name to the window which always insures the new window method actually opens a new window, instead of opening new content in an old "new window" which may or may not be hidden under the current window/tab. Other parameters are not needed, and when omitted should open a full viewport window/tab.

If JS is disabled, it goes ahead and submits to the script you posted and new window functionality is not present - which should be fine for non-js clients.
8:20 pm on Feb 17, 2012 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



@rocknbil, setting the target attribute would require less work, though, and would be easier to maintain. For example, suppose more inputs get added to the form? You would need to modify the JavaScript to pass those as well, whereas setting the target attribute would require no additional changes to the script. :)
9:47 am on Feb 18, 2012 (gmt 0)

10+ Year Member



Thanks for the reply and apologies for the delay in getting back, no internet for a couple of days.

I'll have a play around with them this weekend

Thanks again for your help
5:38 pm on Feb 20, 2012 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



setting the target attribute would require less work, though, and would be easier to maintain.


But is invalid for XHTML doctypes. Besides, you already suggested that, "enter option two." :-P

I can't imagine why a print of a record would need anything but the id to reference it, but I guess that could happen.
6:05 pm on Feb 20, 2012 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



But is invalid for XHTML doctypes

Setting it via external JavaScript would not affect the validity of the document, and would work even with XHTML.

My point was that you limit your JavaScript to affect behavior, without the need to recreate the underlying content and/or form submission process. In other words, keep it simple. :)

For example:

window.onload = function() {
var p = document.getElementById('printButton');
if (p) {
p.onclick = function() {
this.form.target = "_blank";
};
}
};


Ends up being less work, and there's no special maintenance required.
6:12 pm on Feb 20, 2012 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Actually, that last statement was not entirely correct. Other methods of submitting the form would be affected by this as well AFTER the printButton had been clicked. So there would be some maintenance required to reset the target attribute for those other submission methods:


window.onload = function() {
var e = document.getElementById('editButton'),
p = document.getElementById('printButton');
if (e) {
e.onclick = function () {
this.form.target = "";
}
}
if (p) {
p.onclick = function () {
this.form.target = "_blank";
};
}
};