473,394 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

JavaScript - two submit buttons

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

Jul 23 '05 #1
4 5443
On Fri, 11 Feb 2005 08:18:05 -0800, houstoncity2004 wrote:
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.


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

Jul 23 '05 #2
ho*************@yahoo.com wrote:
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">
'language' attribute deprecated (adios).
<!--
Stop hiding ! (ancient history)
function radio_button_checker()
{

if(radio_form.but1.value == "Continue")
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.
{
if((radio_form.Desc.value == "")
|| (radio_form.General.value == 0)
Form values are strings ---> document.radio_form.Specific.value == '0'

Are those zeros text entries? Hard to tell without the form.
|| (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);
}
}
Bad news all around (see below)
-->
Stop hiding !
</script>
<form method="post" action="return decideLink()"
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.
onsubmit="return radio_button_checker()" name="radio_form">


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.

Jul 23 '05 #3
RobB wrote:

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


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
Jul 23 '05 #4
RobG wrote:
RobB wrote:

Form values are strings ---> document.radio_form.Specific.value == '0'
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.


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

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>


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

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: tshad | last post by:
I posted this on the asp.net group, also. I wasn't sure whether this was an asp.net problem or a javascript problem. I have a page that was originally created from a program I found on the net...
6
by: tshad | last post by:
I am trying to set up a Javascript popup box that has a way of sending back a message to asp.net on how to process some data. At the moment I am just doing: ...
9
by: tshad | last post by:
This is from my previous post, but a different issue. I have the following Javascript routine that opens a popup page, but doesn't seem to work if called from an asp.net button. It seems to work...
4
by: Mark Miller | last post by:
I've been trying to execute a javascript function just before submit on a form that contains an <input type="file"> input field and it isn't working. The reason I want to do this is the end users...
5
by: | last post by:
Hi all, Has anyone been able to write some custom javascript on the onclick event of submit button to do certain things like disable submit button, only submit form once etc. This was a breeze...
9
by: Poker Man | last post by:
Hi, I know how to do Sumbit buttons in Forms, and I know how to do custom buttons in Javascript. What I can't seem to find is how to do custom Submit buttons using Javascript! Anybody out...
2
by: khng | last post by:
When there is a HTML Form with lots of input elements, how can I know which element is triggering the form submit action with the help of javascript.
10
by: IchBin | last post by:
I am trying to set the state of a radio button. I do not see what I am doing wrong. Sorry, I am new at this.. I need another set of eyes to look at this snip of code. I am trying to set the radio...
3
by: KOFI QUANSAH | last post by:
Hello, 1) I have a php script A with several buttons(not submit) on a form. 2) On click of any of the buttons, a specific javascript function is called. 3) Depending on the outcome of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.