Connecting Tech Pros Worldwide Help | Site Map

JavaScript - two submit buttons

houstoncity2004@yahoo.com
Guest
 
Posts: n/a
#1: Jul 23 '05
Hi, I need help to JavaScript. I am new to this, and am trying to the
page online ASAP. I have two submit buttons on one form, and each
button will have to do different things.

I named both button "but1". The first button has the value "Continue",
the second button has the value "new page". If someone click on the
button with the value "Continue", I need to check if certain
information has been filled in before proceeding, and if yes, I need to
go to page1.php (which is the second page of the form) and I have to
save the result on a database.

If someone click on the button with the value "New Page", I do not need
to check for anything, but I have to go to pagenew.php (which is a
different version of the second page) and also save the result onto a
database.

I have the following codes, I have been trying for the whole day, it is
still not working. Would some please help me? Thank you very, very
much!

<script language="JavaScript" type="text/javascript">
<!--

function radio_button_checker()
{

if(radio_form.but1.value == "Continue")
{
if((radio_form.Desc.value == "")
|| (radio_form.General.value == 0)
|| (radio_form.Specific.value == 0))
{
alert("Please answer all questions.")
return (false);
}
return (true);
}

}

function decideLink()
{
if(radio.form.but1.value == "Continue")
{
return (page1.php);
}
if (radio.form.but1.value == "Would Not Outsource Offshore")
{
return (pagenew.php);
}
}
-->
</script>


<form method="post" action="return decideLink()"
onsubmit="return radio_button_checker()" name="radio_form">

Ivan Marsh
Guest
 
Posts: n/a
#2: Jul 23 '05

re: JavaScript - two submit buttons


On Fri, 11 Feb 2005 08:18:05 -0800, houstoncity2004 wrote:
[color=blue]
> Hi, I need help to JavaScript. I am new to this, and am trying to the
> page online ASAP. I have two submit buttons on one form, and each
> button will have to do different things.
>
> I named both button "but1". The first button has the value "Continue",
> the second button has the value "new page". If someone click on the
> button with the value "Continue", I need to check if certain information
> has been filled in before proceeding, and if yes, I need to go to
> page1.php (which is the second page of the form) and I have to save the
> result on a database.
>
> If someone click on the button with the value "New Page", I do not need
> to check for anything, but I have to go to pagenew.php (which is a
> different version of the second page) and also save the result onto a
> database.[/color]

Correct me if I'm wrong folks but as I understand it either submit button
will always pass the value of the last submit button on the form.

I'd use regular buttons instead of submit buttons, each one triggering a
different onclick function that submits the form.

--
Life is short, but wide. -KV

RobB
Guest
 
Posts: n/a
#3: Jul 23 '05

re: JavaScript - two submit buttons


houstoncity2004@yahoo.com wrote:[color=blue]
> Hi, I need help to JavaScript. I am new to this, and am trying to[/color]
the[color=blue]
> page online ASAP. I have two submit buttons on one form, and each
> button will have to do different things.
>
> I named both button "but1". The first button has the value[/color]
"Continue",[color=blue]
> the second button has the value "new page". If someone click on the
> button with the value "Continue", I need to check if certain
> information has been filled in before proceeding, and if yes, I need[/color]
to[color=blue]
> go to page1.php (which is the second page of the form) and I have to
> save the result on a database.
>
> If someone click on the button with the value "New Page", I do not[/color]
need[color=blue]
> to check for anything, but I have to go to pagenew.php (which is a
> different version of the second page) and also save the result onto a
> database.
>
> I have the following codes, I have been trying for the whole day, it[/color]
is[color=blue]
> still not working. Would some please help me? Thank you very, very
> much!
>
> <script language="JavaScript" type="text/javascript">[/color]

'language' attribute deprecated (adios).
[color=blue]
> <!--[/color]

Stop hiding ! (ancient history)
[color=blue]
> function radio_button_checker()
> {
>
> if(radio_form.but1.value == "Continue")[/color]

You've got two form elements named 'but1', so the first is now
referenced as but1[0]
and the other is but1[1]. Their values are exactly as you wrote them in

your HTML, no need to check. ;D

Need to make this *document*.radio_form (or use
document.getElementById('radio_form') with an id.
[color=blue]
> {
> if((radio_form.Desc.value == "")
> || (radio_form.General.value == 0)[/color]

Form values are strings ---> document.radio_form.Specific.value == '0'

Are those zeros text entries? Hard to tell without the form.
[color=blue]
> || (radio_form.Specific.value == 0))
> {
> alert("Please answer all questions.")
> return (false);
> }
> return (true);
> }
>
> }
>
> function decideLink()
> {
> if(radio.form.but1.value == "Continue")
> {
> return (page1.php);
> }
> if (radio.form.but1.value == "Would Not Outsource Offshore")
> {
> return (pagenew.php);
> }
> }[/color]

Bad news all around (see below)
[color=blue]
> -->[/color]

Stop hiding !
[color=blue]
> </script>
>
>
> <form method="post" action="return decideLink()"[/color]

A form's action attribute takes a string url (or an empty string if the
form is submitting to itself). This is not a (JavaScript) event
handler. You can use a javascript: url to call JS
(action="javascript:doThis()") but that's rarely useful.
[color=blue]
> onsubmit="return radio_button_checker()" name="radio_form">[/color]

Try this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>untitled</title>
<script type="text/javascript">

function Continue()
{
var f, els;
if (f = document.getElementById('radio_form'))
{
els = f.elements;
if((els.Desc.value == "")
|| (els.General.value == '0')
|| (els.Specific.value == '0'))
{
alert("Please answer all questions.")
return false;
}
else f.action = 'page1.php';
}
}

function Newpage()
{
var f;
if (f = document.getElementById('radio_form'))
f.action = 'pagenew.php';
}

</script>
</head>
<body>
<form id="radio_form" name="radio_form" action="page1.php"
method="post">
<input type="text" name="Desc" value="" />
<br />
<input type="text" name="General" value="0" />
<br />
<input type="text" name="Specific" value="0" />
<br />
<input type="submit" name="sub" value="Continue"
onclick="this.form.onsubmit=Continue" />
<input type="submit" name="sub" value="New Page"
onclick="this.form.onsubmit=Newpage" />
</form>
</body>
</html>

Clicking either button assigns a new onsubmit handler, defined above.
If JS is disabled, the form will submit anyway, to the default action.
Not sure enough about what is going on here to say more.

RobG
Guest
 
Posts: n/a
#4: Jul 23 '05

re: JavaScript - two submit buttons


RobB wrote:[color=blue]
>
> Form values are strings ---> document.radio_form.Specific.value == '0'
>[/color]

Strictly speaking, yes. But in this case testing ...value == 0
or ...value == '0' will return true (Firefox & IE).

I guess it's an artifact of JavaScript's type conversion.

Try this:

<form action="">
<input type="text" name="tst" value="0">
<input type="button" value="check" onclick="
var msg = '0 test: ';
msg += (this.form.tst.value == 0);
msg += '\n\'0\' test: ';
msg += (this.form.tst.value == '0');
msg += '\nisNaN test: ';
msg += (isNaN(this.form.tst.value));
alert(msg);
">
</form>


--
Rob
RobB
Guest
 
Posts: n/a
#5: Jul 23 '05

re: JavaScript - two submit buttons


RobG wrote:[color=blue]
> RobB wrote:[color=green]
> >
> > Form values are strings ---> document.radio_form.Specific.value ==[/color][/color]
'0'[color=blue][color=green]
> >[/color]
>
> Strictly speaking, yes. But in this case testing ...value == 0
> or ...value == '0' will return true (Firefox & IE).
>
> I guess it's an artifact of JavaScript's type conversion.[/color]

'Strictly speaking'...as in..."that's correct"?

I think there is arguably a way to be overly elaborate for posters just
learning the basics (not being condescending there)...sometimes I
prefer to be straightforward about basic issues. Form elements are
strings, and type conversion shouldn't, I think, be relied upon to
'fix' things.
[color=blue]
>
> Try this:
>
> <form action="">
> <input type="text" name="tst" value="0">
> <input type="button" value="check" onclick="
> var msg = '0 test: ';
> msg += (this.form.tst.value == 0);
> msg += '\n\'0\' test: ';
> msg += (this.form.tst.value == '0');
> msg += '\nisNaN test: ';
> msg += (isNaN(this.form.tst.value));
> alert(msg);
> ">
> </form>[/color]

Very messy HTML - cute formatting, but prone to errors. (jmo)

Closed Thread