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.