onSubmit event

Hi all,
I am trying to launch a timer window using the code below.


<form name="redirect" action="'.$url.'" method="post" onSubmit="timerWindow('.$timeremain.')">

When I click “submit” the popup window is created.
When I add,


   document.redirect.submit();

I don’t get a popup window.

Does anyone have a workaround for this, I really need the popup to show how much time remaining a user has.

If you are wondering why?

The user goes to the login page and logs in (at this point I don’t know if the user has time remaining).
I have to query the database to find the time remaining for the user.
Then I need to automatically post to another server and launch a popup window showing their remaining time.

I’m wondering if at the login page I could use AJAX and do an onKeyDown on the submit button or would this be better as an onBlur on the user name.

I think there is still going to be a problem getting the timeremaining into this,
onSubmit=“timerWindow(‘.$timeremain.’)”

Thank you, any speculation or advice is greatly appreciated.

bit hard to tell with only 2 lines of code posted and not knowing where that line is added or what your function code is.

Now I’m confused, onSubmit is supposed to be triggered by a users action and not a scripts action but this works (below). I guess if onSubmit is in an HTML tag it is ignored but if it is in a script (tag) it gets executed.


document.redirect.onsubmit(timerWindow('.$dopopup.'));
document.redirect.submit();

The popup blockers get this one, I do know that if the form is submitted by a user, the popup works perfect when called from the form tag.

@Kalon here is what I have, It’s echoed out in PHP.


$dopopup = '5000';	
echo '
<form name="redirect" action="'.$url.'" method="post" onSubmit="timerWindow('.$dopopup.')">

<input type="hidden" name="dst" value="'.$dst.'">
<input type="hidden" name="username" value="'.$username.'">
<input type="hidden" name="password" value="'.$password.'">
<input type="submit" value="continue">
</form>
<script language="JavaScript">
   document.redirect.onsubmit(timerWindow('.$dopopup.'));
   document.redirect.submit();
</script></center>
';

onsubmit is not a html tag. it is an event

you can assign event handlers as attributes to html elements or fire an event using javascript.

@Kalon, onSubmit is definitely an event. you must have read something wrong, what I wrote was “if onSubmit is in an HTML tag”. The keyword here is “in”.

Seriously I am trying to get a popup going here. Can you help with that?

I can help with that.

Give your form a unique identifier so that the script can easily find the form.
The name element on the form does nothing useful so that is removed.


<form id="redirect" action="'.$url.'" method="post">

Now from a script that runs after the form, preferably just before the </body> tag, obtain a reference to the form and attach your event.


<script type="text/javascript">
var form = document.getElementById("redirect");
form.onsubmit = function () {
    timerWindow('.$dopopup.');
};
</script>

As a side note, when you’re wanting to echo large chunks of content it can be more effective to break out of PHP then to back to it again.

For example:


// php scripting, and then...
?&gt;
&lt;form id="redirect" action="&lt;?php echo $url; ?&gt;" method="post"&gt;
...

&lt;script type="text/javascript"&gt;
var form = document.getElementById('redirect');
form.onsubmit = function () {
    timerWindow(&lt;?php echo $dopopup; ?&gt;);
};
&lt;/script&gt;
&lt;?php
// more php scripting

I can help with that.

Give your form a unique identifier so that the script can easily find the form.
The name element on the form does nothing useful so that is removed.


<form id="redirect" action="'.$url.'" method="post">

Now from a script that runs after the form, preferably just before the </body> tag, obtain a reference to the form and attach your event.


<script type="text/javascript">
var form = document.getElementById("redirect");
form.onsubmit = function () {
    timerWindow('.$dopopup.');
};
</script>

As a side note, when you’re wanting to echo large chunks of content it can be more effective to break out of PHP then to back to it again. That way you’re not restricted as to which types of quotes you can use, so that you can correctly use single quotes within your javascript code (which should be contained in a separate file, but none-the-less).

For example:


// php scripting, and then...
?>
<form id="redirect" action="<?php echo $url; ?>" method="post">
...

<script type="text/javascript">
var form = document.getElementById('redirect');
form.onsubmit = function () {
    timerWindow(<?php echo $dopopup; ?>);
};
</script>
<?php
// more php scripting

Thanks pmw57,

In a nutshell I am trying to mimic “header location” but need the values posted (not $_GET). cURL will not work as it does not do a redirect in the browser.

With your code I have this,


<script type="text/javascript">
function timerWindow(trem)
{

var ip = "10.10.0.1";
var trem = "55555";
<?
$dopopup = '55555';
?>
	window.open("time_remaining.php?timeremaining=" + trem + "&tik=" + ip, "timerWindow", "width=430, height=170, location=no, toolbar=no,status=no, scrollbars=no, menubar=no, resizable=yes, titlebar=yes");
}

var form = document.getElementById('redirect');
form.onsubmit = function () {
    timerWindow(<?php echo $dopopup; ?>);
	
};
document.redirect.submit();

</script>

document.redirect.submit(); does do the redirect but no popup. If the submit button is clicked there is a popup.

From the reading I have done this approach is not going to fly.

Thank you for your help, it’s greatly appreciated.

That’s right. Due to historical problems with popup advertising windows, popup windows must be initiated by the user now instead of scripting.