Results 1 to 6 of 6

Thread: [RESOLVED] stupid javascript form.submit() problem

  1. #1

    Thread Starter
    Addicted Member Sibby's Avatar
    Join Date
    Feb 2001
    Location
    Milwaukee, WI *The United States of America*
    Posts
    144

    [RESOLVED] stupid javascript form.submit() problem

    ok, I have a link calling a javascript function

    Code:
    <a href=updateuser.aspx?id=123 onclick=submitAction(10)>save</a>
    then I have a function defined that changes a value of a hidden form element

    Code:
    <script language="javascript">
       function submitAction(val) {
           document.myform.action.value = val;
           document.myform.submit();
       }
    </script>
    pretty damn simple so far right.....ok, well the link is clicked, in the Page_Load sub, I check the value of request.form("action") and attempt to take the appropriate action. When I check it the value is equal to NOTHING, which means the form never got submitted!! Now here's the kicker....when I put a meaningless alert box (see below) it works just fine!!?!?

    Code:
       function submitAction(val) {
           document.myform.action.value = val;
           document.myform.submit();
           alert('garbage');
       }
    I am at my wits end....can someone see what stupid thing I am doing wrong?
    Last edited by Sibby; Dec 6th, 2004 at 11:06 AM.
    If you can think it....you can code it....

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    this is just a guess, as perhaps it COULD be syntax, but the one thing that did stick out right away was your hidden form element.. you called it ACTION... action is already a form element as in

    <form name=MyForm method=post action=http://www.whatever.com/submit.asp>

    so is this the actual ACTION property you are trying to set? i think its not since you said you are using a hidden form element.. try to change its name and see if that works.. if it doesn't I will look at it further

  3. #3
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727
    it may be that the page is submitted before the variable is set, this is why the alert box gives enough time to compute before the page is exited.

    try adding the onsubmit event like this:
    Code:
    <script language="javascript">
       function submitAction(val) {
           document.myform.action.value = val;
           document.MyForm.onsubmit();
           document.myform.submit();
       }
    </script>
    does this solve your issue?
    When your car breaks down,
    close all windows and retry

    => please rate all users posts! <=

  4. #4

    Thread Starter
    Addicted Member Sibby's Avatar
    Join Date
    Feb 2001
    Location
    Milwaukee, WI *The United States of America*
    Posts
    144

    (sigh)

    kleinma, good point, never thought of the name conflict...however changing this didn't resolve anything. Although I have chaged it from "action" to "cmd" to avoid later confusion

    wildcat_2000, I definatly think you're on to something as far as the timing issue. When I tried using "document.myform.onsubmit();" threw an error "Microsoft JScript runtime error: Object doesn't support this property or method". However, when I debug skip that line, and move it to the next (the form submit), it works flawlessly!! (well except for the debug error or course)

    ...what else can I try, I think I've seriously wasted a good hour or 2 on this thing. Any other suggestions would be welcome. Oh, and with the timing issue on the mind, I've tried submitting the form as such...

    Code:
       setTimeout('document.myform.submit()',5000);
    ...with no luck at all.

    It's something to do with the timing of the form and that variable not being set within the form or something....although if I do an alert to display the contents of "cmd" it DOES display the correct value...BUT If I do the alert BEFORE the form submit it doesn't work, if I do it AFTER it does....I think i'm going to kill someone....
    If you can think it....you can code it....

  5. #5
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727
    the problem is that you are using the href to set the destination page, which is taking over the form.submit event.

    save the following code into a page called test.aspx. this works.
    Code:
    <%@ Page Language="VB" %>
    <script runat="server">
    
        Sub Page_Load(obj As Object, e As EventArgs)
        
        	mVal.Text = Request("MyVar")
        
        End Sub
    
    </script>
    <html>
    <head>
    <script language="javascript">
       function submitAction(val,ActionPage) {
           document.MyForm.MyVar.value = val;
           document.MyForm.action = ActionPage;
           document.MyForm.submit();
       }
    </script>
    </head>
    <body>
    
    <asp:Label id="mVal" runat="server"></asp:Label>
    
    <form name="MyForm">
    <input type="hidden" name="MyVar" value="0">
    <a href='#' onclick="submitAction(10,'test.aspx');">save</a>
    </form>
    </body>
    </html>
    cheers,

    wc.
    Last edited by wildcat_2000; Dec 2nd, 2004 at 05:19 PM.
    When your car breaks down,
    close all windows and retry

    => please rate all users posts! <=

  6. #6

    Thread Starter
    Addicted Member Sibby's Avatar
    Join Date
    Feb 2001
    Location
    Milwaukee, WI *The United States of America*
    Posts
    144

    Thumbs up Re: stupid javascript form.submit() problem

    Sorry, been busy the last few days.

    wildcat, Thank You!! This was exactly my problem! This problem illustrates why I hate HTML so much . I hope this post will help someone else out in the future! Thanks again guys!

    Sibby
    If you can think it....you can code it....

Posting Permissions

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



Click Here to Expand Forum to Full Width