PDA

Click to See Complete Forum and Search --> : [RESOLVED] stupid javascript form.submit() problem


Sibby
Nov 30th, 2004, 05:10 PM
ok, I have a link calling a javascript function


<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


<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!!?!?


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?

kleinma
Nov 30th, 2004, 07:22 PM
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

wildcat_2000
Dec 1st, 2004, 10:19 AM
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:

<script language="javascript">
function submitAction(val) {
document.myform.action.value = val;
document.MyForm.onsubmit();
document.myform.submit();
}
</script>

does this solve your issue?

Sibby
Dec 1st, 2004, 04:51 PM
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 :D

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...


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....:eek2:

wildcat_2000
Dec 2nd, 2004, 11:45 AM
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.

<%@ 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.

Sibby
Dec 6th, 2004, 10:06 AM
Sorry, been busy the last few days.

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

Sibby