Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

What is wrong with this code

Guest
Feb 10, 2009 Feb 10, 2009
Hi All,

I have the following code with two Javascript functions. When I submit the form, it does not navigate to the next page. I tried lot of alternatives but nothing seems to work. Can anyone kindly suggest what is wrong with the code below:

index.cfm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>

</head>
<script language="javascript">
function validate_form()
{
if(document.myform.lastname.value == '')
alert('Please enter a value for Lastname');
return false;

}

function saySomething(arg)
{
document.myform.firstname.value = arg;
if(validate_form()){
document.myform.submit();
return false;
}

}
</script>
<body>
<form method="post" action="index1.cfm" name="myform">
First Name:<input type="text" name="firstname" id="firstname" value=""/><br />
Last Name:<input type="text" name="lastname" id="lastname" value=""/>
<a href="javascript:saySomething('me');">Click here for me</a>
<a href="javascript:saySomething('all');">Click here for all</a>
</form>
</body>
</html>

index1.cfm

<cfoutput>Hello</cfoutput>
<!--- make sure firstname is defined and has a value that isn't an empty string, if not, use "me" --->
<cfif isdefined("form.firstname") and Trim(form.firstname) neq "">
<cfset myvariable = #form.firstname#>
<cfelse>
<cfset myvariable = "me" />
</cfif>
<cfoutput>#myvariable#</cfoutput><br />
<cfif form.firstname eq 'me'>
<cfoutput>This is me</cfoutput>
<cfelse>
This is not me.This is all
</cfif>
<cfif isdefined("form.lastname") and Trim(form.lastname) neq "">
<cfoutput>#form.lastname#</cfoutput>
</cfif>

TOPICS
Getting started
690
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 10, 2009 Feb 10, 2009
You can achieve the same thing with a lot less code if you used cfform and cfinput.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 10, 2009 Feb 10, 2009
The form wont be submitted because validate_form() always returns false. There are other things you could do better.

1) If you must use HTML tags, then place Javascript in the head section.

2) What's with all the "Me" and all? Why don't you just do basic form validation, something like this:

<script language="javascript">
function validate_form()
{
var isValidated = true
if(document.myform.firstname.value == '') {
alert('Please enter your first name');
isValidated = false;
}
if(document.myform.lastname.value == '') {
alert('Please enter your last name');
isValidated = false;
}
return isValidated;
}

function sendName()
{
if(validate_form()){
document.myform.submit();
}
}
</script>
<body>
<form method="post" action="index1.cfm" name="myform">
First Name:<input type="text" name="firstname" id="firstname" value=""/><br />
Last Name:<input type="text" name="lastname" id="lastname" value=""/>
<a href="javascript:sendName();">Send</a>
</form>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 12, 2009 Feb 12, 2009
LATEST
Remove the return statement from the saySomething function.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources