Forum Moderators: open

Message Too Old, No Replies

Form submit validation/redirect

Changing the target CGI form handling depending on the radio choice

         

johny_rm

11:17 pm on Mar 8, 2004 (gmt 0)

10+ Year Member



In my first post I figured out how to do some very basic form validation (OK, I copied one script and modified it).

I really would need a specific example how to handle the following task: If the user selects radio button 1 (so if he chooses Yes), he is to be taken to page2.txt - see the code below. If he chooses No (radio value = 2), the form submits the _browser_out value to page3.txt

Sniplet of the code:


<form name="form" method="post" onsubmit="return checkFields()" action="MYURL/cgi-bin/script.cgi">
<input type="hidden" name="_browser_out" value="page2.txt">
<input type="radio" name="agree" value="1"> Yes, I do<br>
<input type="radio" name="agree" value="2"> No, I do not
</form>

So as you can see, I already have the checkFields javascript function enabled which requires all fields to be entered or selected, and now I'm really completely lost how to do the function I really need.

Your help really is VERY much appreciated...

luckydude

11:59 pm on Mar 8, 2004 (gmt 0)

10+ Year Member



try this one


head>
<script language="javascript">
<!--
function checkFields()
{
if(document.form.elements[0].checked)
window.location=document.form.elements[0].value;

if(document.form.elements[1].checked)
window.location=document.form.elements[1].value;
}
//-->
</script>
</head>
<body>
<form name="form" method="post" onsubmit="return checkFields()" action="MYURL/cgi-bin/script.cgi">
<input type="hidden" name="_browser_out" value="page2.txt">
<input type="radio" name="agree" value="agree.htm"> Yes, I do<br>
<input type="radio" name="agree" value="disagree.htm"> No, I do not
</form>
</body>

Hope this helps

Luckydude

johny_rm

4:38 pm on Mar 10, 2004 (gmt 0)

10+ Year Member



Thanks for the suggestion.

I will test it and let you know how it goes; I'll have to modify it a bit, though. You see, the CGI script takes the page where the "_browser_out" variable tells it to go. So I guess this would be a solution:


<input type="radio" name="_browser_out" value="page2.txt"> Yes, I do<br>
<input type="radio" name="_browser_out" value="page3.txt"> No, I do not

If this is the case, however, I lose the values of yes/no which are important to me. Actually the YES/NO variable could be read or deducted from the other fields (so if the ones on page2 are missing, that means that he chose NO and if all are filled out that means he chose YES.

But I have one additional question, similar to this one, and that one has four options, and only if the user selects the first one, he is to be taken to an extra page, and just proceeds on if he chooses any of the other three.

So the question would probably be can you assign two sets of values to one radio button, or maybe there's another javascript which could do that.

I could really use all the help I can get - this has been bugging me for quite a while and I just discovered this forum this week :)

This should be up and running by this Friday. Am I too optimistic?

ajkimoto

5:44 pm on Mar 10, 2004 (gmt 0)

10+ Year Member



johnny_rm,

Here is an alternate approach that allows you to keep the values that you assigned to your radio buttons. This example has 4 radio buttons--if first button selected, then _browser_out is set to 'page1a.txt' otherwise to 'page2.txt'

<head>

<script type="text/javascript">
<!--
function setPage(actionVal){
//go to extra page1a.txt if first radio button is selected, otherwise go to page2.txt
document.forms[0].elements[1].value=(actionVal=='Yes')?'page1a.txt':'page2.txt'
}
//-->
</script>

</head>

<body>
<form name="myform" id="myform" method="post" onsubmit="return checkFields()" action="MYURL/cgi-bin/script.cgi">
<input type="hidden" name="_browser_out" value="page2.txt" />
<input type="radio" name="agree" value="1" onclick="setPage('Yes')" /> Yes, I do<br />
<input type="radio" name="agree" value="2" onclick="setPage('No')" /> No, I do not<br />
<input type="radio" name="agree" value="3" onclick="setPage('No')" /> I really do not!<br />
<input type="radio" name="agree" value="4" onclick="setPage('No')" /> I really, really do not!<br />
</form>
</body>

Hope this helps,

ajkimoto

johny_rm

11:19 pm on Mar 10, 2004 (gmt 0)

10+ Year Member



Thanks ajkimoto,

your script works great and this way I also get to keep all the previously defined values.

That doesn't mean that luckydude's suggestion wasn't OK; I'm sure I'll be able to implement that in some other case/form.

THANK YOU for your suggestins/solutions.

That was definitely a load off my back so an e-hug of gratitude :)

johny_rm

1:47 am on Mar 11, 2004 (gmt 0)

10+ Year Member



I guess I spoke too soon - I stumbled across a strange problem.

I took an existent page out od the aready set-up form in order to test this (page 4/9) and when I start from the Page 4, the script works OK but when I start from page 1, the function defined in javascript on page 4 does not work.

Any suggestions why might that be?

Syntax and everything else seem OK and to emphasise again - it works if it starts with page 4, but doesn0t work if you start from page 1.

;(

ajkimoto

6:48 am on Mar 11, 2004 (gmt 0)

10+ Year Member



johny_rm,

Hmmm... That does sound strange. Can you confirm that page 4/9 when you start from page1 is the same one as when you start from page4 (maybe do a view source to verify that the script is there). Also check to make sure that _browser_out is the first input object on the form and that the form that contains _browser_out is the first form on the page.

Also, when you say that the function doesn't work, what do you mean? Does it not go to the right page, generate an error, or what?

BTW, I used the document.forms[0].elements[1].value construct to be kind to older browsers. The more modern approach (good on IE5+ NS6+, etc) is to use document.getElementById('_browser_out').value.

ajkimoto

johny_rm

9:37 am on Mar 11, 2004 (gmt 0)

10+ Year Member



I can confirm that this is the actually the same file. I retested it by starting with that page, and that works. But if I start with the first page, then it doesn't take the *Yes* value into account and it just goes on as default (or if chosen *No*).

I've been banging my head over this and I may just use the first suggestion and sacrifice some values :(

ajkimoto

5:21 pm on Mar 11, 2004 (gmt 0)

10+ Year Member



Yikes, I think that I found the problem. I stupidly put the wrong array reference in the function--I set it to:
document.forms[0].elements[1].value

when it should have been:

document.forms[0].elements[0].value

That's the trouble of using one language where arrays start at 1 and another where they start at 0!

ajkimoto

johny_rm

3:58 pm on Mar 12, 2004 (gmt 0)

10+ Year Member



With ajkimoto's help, I figured it out what works for me (specifically defining form elements):


<script type="text/javascript">
function setPage(actionVal){
document.form._browser_out.value=(actionVal=='Yes')?'page1a.txt':'page2.txt'
}
</script>

Thanks again for your help!