Jump to content

Recommended Posts

As it is, the php executes on page load. How can I set a flag to true or false for this so that the php only executes onunload?[code]<script type="text/javascript">
function killBatch() {
<?php // do php stuff here ?>
}
window.onunload=killBatch;
</script>[/code]

i would like it to be something like:[code]<script type="text/javascript">
function killBatch() {
<?php
    if(killBatch =='true') {
        // do php stuff here
    }
?>
}
window.onunload=killBatch('true');
</script>[/code]
Link to comment
https://forums.phpfreaks.com/topic/10206-php-executing-before-onunload/
Share on other sites

[!--quoteo(post=376112:date=May 22 2006, 07:37 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 22 2006, 07:37 PM) [snapback]376112[/snapback][/div][div class=\'quotemain\'][!--quotec--]
umm.. you can't? PHP is parsed on the server, before anything ever reaches the browser. And JS is parsed in on the client, after everything has been sent. So you cannot use JS to stop PHP from executing something.
[/quote]
dammit! I always forget about that.
Thanks :)
You can using Ajax, just place the php function in a separate file and use the XMLHttpRequest object.

[b]killbatch.php[/b]
[code]
<?php
//batch is slaughtered...
if($batchSlaughteredSuccesfully){
   echo true;
}?>[/code]

[code]
var http;
if(window.XMLHttpRequest) {
    http = new XMLHttpRequest();
}
    else if(window.ActiveXObject) {
    http = new ActiveXObject("Microsoft.XMLHTTP");
}
    else {
    alert('Problem creating the XMLHttpRequest object');
}
function killBatch()
{
    http.open('get','killbatch.php?');
    http.onreadystatechange = handleResponse;
    http.send(null);
}
function handleResponse()
{
    if(http.readyState == 4 && http.status == 200) {
        var response = http.responseText;
        if(response == false) {
            /*Killing of batch failed, do something accordingly */
        }
    }
}
window.onunload=killBatch()
[/code]

This is probably the simplest implementation of Ajax I ever saw.. [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]
Very cool. I hope we can get this to work!

With what you posted, it's not working yet. I put this at the top of my page[code]<script language="javascript" type="text/javascript">
var http;
if(window.XMLHttpRequest) {
    http = new XMLHttpRequest();
}
    else if(window.ActiveXObject) {
    http = new ActiveXObject("Microsoft.XMLHTTP");
}
    else {
    alert('Problem creating the XMLHttpRequest object');
}
function killBatch()
{
    http.open('get','killbatch.php?');
    http.onreadystatechange = handleResponse;
    http.send(null);
}
function handleResponse()
{
    if(http.readyState == 4 && http.status == 200) {
        var response = http.responseText;
        if(response == false) {
        /*Killing of batch failed, do something accordingly */
        }
    }
}
window.onunload=killBatch()
</script>[/code]
And then , just to really make sure it is working put this in killbatch.php[code]<?php
write_file('includes/files/batch_status.txt','stopped');
$to = 'my_email_address@domain.com';
$sub = 'batch killed';
$msg = 'batch killed';
mail($to, $sub, $msg, "From: gNews <$to>\nX-Mailer: PHP/" . phpversion());
?>[/code]
EDIT{
I AM A DUMB@$$ - who didn't scroll down through this thread before posting the same response.
}

not sure what you are trying to accomplish, but if you want to be tricky you CAN use JS to control data that comes from the server side, using AJAX of course.

have the page load, client side, noting the element ID of the data you want to control with PHP onChange.
then just use client side JavaScript to call your PHP scripts that run server side. When they are done running, they send the info back to your javaScript function which then updates the data in the element on the semingly static, client side page.
With Ajax you can do a lot more than you're trying to do right now, but let's focus on the task at hand:

This script should work, I only see one error: try replacing the "onunload" line with
[code]window.onunload=killBatch;[/code]

Actually, I think we could simplify this a whole bit:
[code]var http;
if(window.XMLHttpRequest) {
    http = new XMLHttpRequest();
}
    else if(window.ActiveXObject) {
    http = new ActiveXObject("Microsoft.XMLHTTP");
}
function killBatch()
{
    http.open('get','killbatch.php?',true);
    http.send(null);
}
window.onbeforeunload=killBatch;[/code]

Note this line: [u]http.open('get','killbatch.php?',[b]true[/b]);[/u]
With the third parameter set to true, we basicly tell javascript not to wait for the file we're calling to get the "complete" status (it is executed asynchonously). We just call it and leave it to execute.

The server kills the batch (whatever that does), and the user doesn't have to wait on it to complete; the window closes. If you wanted to do something else after slaughtering the batch, you have to use the first method.

EDIT: Did some testing and found out using the problem lies with using "onunload". Appearantly, it doesn't allow for the the whole code to be executed. I don't see why (though there's probaly a good reson). However, you can use "onbeforeunload" instead, that works proper.
What's killing the batch? If an email batch is running and the admin wants to 'kill' it, then a script just writes 'stopped' to a text file. That's it.
Originally I had looked into javascript to do this but was told I couldn't open/write/close files, so here we are jumping through hoops :)

Still not working. Even with window.onbeforeunload=killBatch; :(

EDIT: I also changed 'get' to 'GET' and 'killbatchhp.php' to 'http://www.thefullurl.com/killbatch.php' with no change.

EDIT AGAIN: IT'S NOW WORKING! I've been working on this for three days and it was killing me!

Thank you VERY much for your help!!! I really appreciate it!

:)
[!--quoteo(post=376365:date=May 23 2006, 10:38 AM:name=tgavin)--][div class=\'quotetop\']QUOTE(tgavin @ May 23 2006, 10:38 AM) [snapback]376365[/snapback][/div][div class=\'quotemain\'][!--quotec--]
What's killing the batch? If an email batch is running and the admin wants to 'kill' it, then a script just writes 'stopped' to a text file. That's it.
Originally I had looked into javascript to do this but was told I couldn't open/write/close files, so here we are jumping through hoops :)

Still not working. Even with window.onbeforeunload=killBatch; :(

EDIT: I also changed 'get' to 'GET' and 'killbatchhp.php' to 'http://www.thefullurl.com/killbatch.php' with no change.

EDIT AGAIN: IT'S NOW WORKING! I've been working on this for three days and it was killing me!

Thank you VERY much for your help!!! I really appreciate it!

:)
[/quote]

I would've been stumped if didn't work, since it worked properly when I tested it.. [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]

You're welcome. [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]
[!--quoteo(post=376370:date=May 23 2006, 03:52 PM:name=448191)--][div class=\'quotetop\']QUOTE(448191 @ May 23 2006, 03:52 PM) [snapback]376370[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I would've been stumped if didn't work, since it worked properly when I tested it.. [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]

You're welcome. [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]
[/quote]
I hate to ask this of you, but now my onbeforeunload message won't work with the new script.[code]<script language="javascript">
var runClose = true;
function unloadMess(){
    if(runClose==true){
        mess = "MESSAGE GOES HERE"
        return mess;
    }
}
function setBunload(on){
    window.onbeforeunload = (on) ? unloadMess : null;
}
setBunload(true);
</script>[/code]
Do I need to merge them?
[!--quoteo(post=376378:date=May 23 2006, 11:07 AM:name=tgavin)--][div class=\'quotetop\']QUOTE(tgavin @ May 23 2006, 11:07 AM) [snapback]376378[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I hate to ask this of you, but now my onbeforeunload message won't work with the new script.[code]<script language="javascript">
var runClose = true;
function unloadMess(){
    if(runClose==true){
        mess = "MESSAGE GOES HERE"
        return mess;
    }
}
function setBunload(on){
    window.onbeforeunload = (on) ? unloadMess : null;
}
setBunload(true);
</script>[/code]
Do I need to merge them?
[/quote]

Yes, you need to merge them, because if you set onunload twice, only the last bound event is kept.

You probably know how to do that better than I do, because unlike you might think, I'm still a javascript newbie! [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]
[!--quoteo(post=376391:date=May 23 2006, 04:45 PM:name=448191)--][div class=\'quotetop\']QUOTE(448191 @ May 23 2006, 04:45 PM) [snapback]376391[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Yes, you need to merge them, because if you set onunload twice, only the last bound event is kept.

You probably know how to do that better than I do, because unlike you might think, I'm still a javascript newbie! [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]
[/quote]
You know more than I! [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]

Just in case anybody else is looking:[code]<script language="javascript" type="text/javascript">
var http;
var message = 'Are you sure you want to leave?';
if(window.XMLHttpRequest) {
    http = new XMLHttpRequest();
}
    else if(window.ActiveXObject) {
    http = new ActiveXObject("Microsoft.XMLHTTP");
}
function killBatch() {
    http.open('GET','killbatch.php',true);
    http.send(null);
    return message;
}
window.onbeforeunload=killBatch;
</script>[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.