SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: onSubmit event

  1. #1
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,098
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    onSubmit event

    Hi all,
    I am trying to launch a timer window using the code below.
    Code:
    <form name="redirect" action="'.$url.'" method="post" onSubmit="timerWindow('.$timeremain.')">
    When I click "submit" the popup window is created.
    When I add,

    Code:
       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.
    What I lack in acuracy I make up for in misteaks

  2. #2
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,098
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.
    Code:
    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.
    Code:
    $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>
    ';
    What I lack in acuracy I make up for in misteaks

  4. #4
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,098
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    @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?
    What I lack in acuracy I make up for in misteaks

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    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.

    Code html4strict:
    <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.

    Code php:
    <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:

    [highlight="php"]
    // 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
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    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.

    Code html4strict:
    <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.

    PHP Code:
    <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 Code:
    // 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
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,098
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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,

    Code:
    <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.
    What I lack in acuracy I make up for in misteaks

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by lorenw View Post
    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.
    That's right. Due to historical problems with popup advertising windows, popup windows must be initiated by the user now instead of scripting.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •