setTimeout("window.close()", 100) not working in Firefox - javascript

I am a learner on HTML, PHP, Javascript.
I am currently working on a barcode scanning system. The steps are simple, scan in an order number, the system(form) will then generate another sequence number and print it out along with the scanned order number right away so that the warehouse worker can stick the print out on the scanned parcel.
Same steps keep on until the whole log of cargo done.
We have created a simplified program, and test it. If it works then we can follow the same logic to create the actual form.
Up to this point, we have successfully scan in the bar code, the form then jump into another form by using windows.open. Inside this pop up windows, it is a PDF document which's created by FPDF. By enable FIREFOX silence print feature. The pop up form can printout the label automatically. Everything good except we cannot close the popup windows. We have already drilled out every possibilities and search thru the net...couldn't find anything.
We did turn on the Firefox option dom.allow_scripts_to_close_windows to TRUE
first form
<?PHP
session_start();
if (isset($_POST['submit'])){
$_SESSION['CUSTNO']=$_REQUEST['CUSTNO'];
$_SESSION['ORDERNO']=$_REQUEST['ORDERNO'] ;
//header('Location:TESTwai.php');
echo "myFunction();";
}
?>
<html>
<body>
<form action="" method="post" name="form1"><br>
<input type="text" id="CUSTNO" name="CUSTNO" style="font-size: 24pt"/>
<input type="text" id="ORDERNO" name="ORDERNO" style="font-size: 24pt"/>
<br>
<input name="submit" type="submit" value="SUBMIT" onclick="return myFunction()"></form>
</body>
</html>
<script>
function myFunction() {
//window.open("ex.php","", "width=200, height=100");
window.open("ex.php","", "width=1000, height=1000");
myWindow.close(); // Closes the new window
}
</script>
Second pop up window form
I have put the setTimeout, close.window inside or out PHP script and still no luck.
<?php
require('pdf_js.php');
class PDF_AutoPrint extends PDF_JavaScript
{
function AutoPrint($dialog=false)
{
//Open the print dialog or start printing immediately on the standard printer
//$param=($dialog ? 'true' : 'false');
//echo $param;
//$script="print($param);";
$script="print(false);";
$this->IncludeJS($script);
}
}
$pdf=new PDF_AutoPrint();
$pdf->AddPage();
$pdf->SetFont('Arial','',20);
$pdf->Text(90, 50, 'Print me!');
//Open the print dialog
$pdf->AutoPrint('false');
ob_end_clean();
$pdf->Output();
?>
<script>
setTimeout("window.close()", 100);
</script>
Please ignore those customer, orderno. They are just dummy input...
The actual testing is just
1) press the submit button
2) pop up windows
3) auto print the label out without any system prompt
4) close the pop up windows and get back to the first form (stuck here)

It is not a issue with setTimeout method. This is the issue with window.close() . It is not a good practice to close the window within itself.Still if you its very necessary you can try the below code:
window.open('','_parent',''); //fool the browser that it was opened with a script
window.close();
You can put this in a function and call it use in setTimeout .Helpful link

Related

Php code to modify a document element

I am trying to build a PHP webpage with the following behaviour:
1- A client access the webpage (that contains some buttons);
2- When the webpage is loaded, the PHP script opens a file stored on the server and, based on the information in this file, enables/disables some of the buttons, so that the client can see the webpage with the correct buttons enabled or disabled.
To enable/disable buttons, I know I can use javascript, while to read the file on the server I use PHP as stated above.
How do I put the two things together? Or should I use a PHP code equivalent to the following javascript line:
<script>document.getElementById("button1").disabled = true;</script>
At first I thought that inserting this line in the PHP code was the solution, but then I found out that this can't work for obvious reasons.
Thanks for the help!
Is it correct if I add the following javascript function in the head section of my webpage?
<script>
function enableButtons() {
<?php
if($state=="state1") {
echo 'document.getElementById("button1").disabled = true;';
}
else if($state=="state2") {
echo 'document.getElementById("button2").disabled = true;';
}
?>
}
</script>
I call the enableButtons() function when loading the page by using
<body onload="enableButtons()">
The php code above is just an example, the number of states and buttons is higher, that's why I would like to use this solution.
The common thing to do is to have php read the settings file, and echo the "disabled" attribute on the buttons before sending the output to the user browser. You can get more info about the attribute here here.
You do not need javascript.
Do something like this:
<button type="button" <?php if($state === 'state1') echo 'disabled'; ?>>Button text</button>
Usually you send to the client the buttons already disabled and use js to respond to any event that happens after sending the page, like selecting a combo box value..
You can omit the code, using an if sentence, or hide them using css. First approach is preferred.
Script
<script>
function isValid(f){
if(f.test.value==''){
alert('please enter name');
return false;
}else{
$(".bbutton").html("Processing please wait");
return true;
}
}
</script>
HTML
<form method="post" onsubmit="return isValid(this);">
<input type="hidden" name="te">
<input type="text" name="test">
<div class="bbutton">
<input type="submit" value="send">
</div>
</form>
When you submit the form then it will automatically hide the submit button to avoid pressing again and again, and you can redirect it to other page. May be this idea helpful.

Submitting a form and closing the window - the correct way to do it?

I am trying to submit a simple form that is in a pop-up dialog and then close the dialog.
The best article I've seen is Submit a form in a popup, and then close the popup but it seems to work intermittently for me. The page launched by the form is a PHP page that modifies a record in the database. I thought that once the request is sent the PHP page will execute, even if the launching window is closed. Apparently not though. Sometimes the table is updated, sometimes it isn't. It seems like if the SQL operation isn't fast enough the page will be closed and the process is killed.
Here's the code:
<form id="xlationform" action="updatexlation.php" method="post" onsubmit="return closeForm(this);">
Source: <br>
<textarea disabled name="sterm" rows=10 cols=50><?php echo $source ?></textarea><br><br>
Translation: <br>
<textarea name="xlt" rows=10 cols=50><?php echo $xlation ?></textarea><br><br>
<input type="hidden" name="id" value="<?php echo $termid ?>">
<input type="submit" value="Update">
</form>
<script>
function closeForm(f) {
f.submit();
window.close();
}
</script>
What's the best way of working this out? I want the window to be closed but the DB operation needs to complete first and I don't want to query the DB again if possible. Thanks for your help.
Do it in updatexlation.php like :
$e = mysqli_query(...) if ($e) { echo "<script>window.close();</script>"; }
But as mentioned above, avoid using popups.
The response page should simply have window.close in the onload event.

Can I force a page in history to be removed?

Re editing... this question has NOT been answered before!
I had understood that changing the contents of a current page with window.location replaced the cached version of the original page ( from the "last" history), so that you really couldn't go back with the browser BACK button. I had even seen this posted as a solution to preventing a malicious visitor from using the BACK button to to re-submit a mail form many times. But it is NOT workable because in the case of a mail form, the BACK button will just take the user back to the pre-POST version of the page.
So, I can use javascript to reset the form, disable the SUBMIT button, change to another page after success, or do whatever I want to the page. But its all for nothing if a simple click of the BACK button followed by SUBMIT causes the form to post again with just 2 clicks.
I know there are a lot of solutions to preventing malicious form resubmissions I can try, but I've had trouble getting them to work, and so I'd just like to know if removing the last history is a dead end. If there is a way, and it is pretty cross browser friendly, then I can just make it part of my scripted actions once my form is successfully processed, and my "thank you" page displays. Basically I'd want my "thank you" page's 'onload' event to either erase the last history, or in a browser compatible way disable the BACK button!
For what its worth, I've included code from simple test I've been working with. You can put some junk in the fields and hit submit. The vars are cleared in the PHP, the form fields are force cleared in javascript, and a new 'location' is invoked. Unfortunately, hitting BACK button will take you back to the "pre-posted" form, with all the strings you added still intact.
<!DOCTYPE HTML>
<html>
<head>
<title> Form Behavior Test</title>
</head>
<!--
<?php
$name = $email = $comments = "";
$formDone = false;
if ($_SERVER["REQUEST_METHOD"] == "POST" )
{
$formDone = true;
$name = $email = $comments = "";
}
?>
-->
<body >
<table border="1"><tr><td style ="text-align:right;" width=100%>
<form name="contactform" id="contactform" method="post" enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name" id="name" value="<?php echo $name;?>"><br>
Email: <input type="text" name="email" id="email"value="<?php echo $email;?>"><br>
<br>
<div align="center"> ---- <span class="error">*</span> Message ---- <br>
<textarea name="comments" id="comments" wrap="physical" cols="40" rows="10" ><?php echo $comments;?></textarea>
</div>
<input name="submit" id="submit" type="submit" value="Submit" >
</form>
</td></tr></table>
<script language="JavaScript">
if (<? echo ($formDone == true) ? 'true' : 'false'; ?>)
{
document.getElementById("name").value = "";
document.getElementById("email").value = "";
document.getElementById("comments").value = "";
document.getElementById("submit").value="Disabled";
document.getElementById("submit").disabled=true;
// substitute with a thank you page
window.location = "http://google.com";
}
</script>
</body>
</html>
After searching pretty exhaustively, I don't believe there is any way to remove a page from history, except on the very latest browsers that support newer HTML-5 history methods. I'm still open to solutions but at this point I think the easiest thing will be for me to set a cookie anytime a successful email is processed by my PHP code. Then, I can also make the PHP or a javascript snippet look for the cookie and if found, I can take all kinds of actions... wipe out all filled in fields (as they would be if the BACK button is pressed), block the email, politely inform the user that he/she must wait (until my cookie expires) to send another email though the form, etc.
I didn't want to do this originally because the BACK button doesn't actually re-load the page, it just displays it. If there were a universal browser compatible way to make pages reached by back buttons actually re-load, this would never have been a problem to begin with. So even with a cookie, my defensive actions couldn't activate until the SUBMIT button is pushed. I guess I can live with that. Also, even today, some people are paranoid about cookies and turn them off. But if I want to be adamant about it, I can just detect when I can't set a cookie, and inform the user that cookies are required to use my email form. If that's too big a deal, oh well!
Thanks to those that contributed. The fact is, the LACK of answers is really a very useful answer sometimes. When I post on any stackoverflow forum and don't get any answers pretty quickly, its a good red-flag that things are going to get convoluted really fast if I don't consider an alternate approach! :-)

Firefox pop-up PHP session variable not being sent correctly

Everything is working fine in Chrome, but not in FF. Here's my situation:
Parent page has a link, on click it creates a pop-up with a text field.
That information is stored in $_SESSION and is used to navigate (ie, "one" takes you to page one, "two" takes you to page two, etc.). In the pop-up, on submit three things happen:
The textfield info is stored in session.
The Parent page refreshes using text field data to tell it where to go.
The pop-up closes.
Keep in mind, this is working fine in Chrome. However, in FF I believe it is closing the pop-up window before the info is sent to parent window, thus losing $_SESSION data. I believe this because if I make it so the pop-up doesn't close, it works fine, so it's not a case of the pop-up not sending the data back to parent window.
SO...
I think my problem is in the order that my JavaScript is being executed, even though my ordering appears correct. And again, it functions in Chrome just fine.
Here's my code, can you solve it?
(All pages have $_SESSION in header, etc, it's not a syntax problem)
I believe Firefox is closing the pop-up before the $_SESSION data is being sent! I don't know why! What happens is my browser ends up in navredirect.php, with no an empty session. It's not getting the $_SESSION data, so the PHP if statement doesn't tell it do the meta redirect. PRINT_R yields nothing.
Parent page pop up call:
Go to Page
Code for pop up page. (enternav.php)
<head>
<script type="text/javascript">
function proceed(){
opener.location.reload(true);
window.opener.location.href='http://www.page.com/navredirect.php';
self.close();
}
</script>
</head>
<body>
<form method="post" onsubmit="proceed()" action="logscript.php">
<input type = "text" name="page" />
<input type ="submit" value="Go to page" />
</form>
</body>
Code for logscript.php (turns text-field into session data)
<? $_SESSION['pw'] = $_POST['page']; ?>
Code for navredirect.php (
<?
if($_SESSION['pw'] == "one") {
echo "<meta http-equiv=\"refresh\" content=\"0;URL=pwone.php\">";
}
elseif($_SESSION['pw'] == "two") {
echo "<meta http-equiv=\"refresh\" content=\"0;URL=pwtwo.php\">";
}
else {
echo "<meta http-equiv=\"refresh\" content=\"0;URL=wrongpw.php\">";
}
?>

How to close popup and redirect a page

My site using php and i create an online quiz and the quiz show in pop up mode.
Currently when i want to redirect my page using the script below. it goes to my homepage BUT it still at the pop up mode. I want it to close the pop up and go to homepage after I click finish.
How can I do that?
<script type="text/javascript">
window.parent.location = "../../../"
</script>
You can access the window that opened a popup with the window.opener property on the popup. So, something like this should work:
window.opener.location.replace(redirectUrl);
window.close;
If you wanted to put that behavior in the onclick event on a submit button you're building like this:
echo "<input type=\"submit\"
name=\"finishattempt\"
value=\"".get_string("finishattempt", "quiz")."\"
onclick=\"$onclick\" />\n";
You'd need to assign the String window.opener.location.href='someUrl';window.close(); to the variable $onclick before echoing the submit button out.
You can try this code (used on an HTML button):
<input type="button" onclick="parent.window.opener.location='http://homepage.com'; window.close();">
And have a look at some similar threads like this one: Javascript: Close Popup, Open New Window & Redirect
[EDIT] See this article too
this way you can do it ..
<script type="text/javascript">
function close_window(){
window.opener.location = 'pop_up.php';
window.close();
}
</script>
html code..
<body>
<form method="post" name="frm_2" id="frm_2">
<input type="submit" name="btn_close" id="btn_close" onclick="return close_window();" />
</form>
</body>

Categories

Resources