Skip to main content
February 10, 2009
Question

What is wrong with this code

  • February 10, 2009
  • 3 replies
  • 687 views
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>

This topic is closed to new replies. Start a new post to keep the conversation going.

3 replies

ajithman
Inspiring
February 12, 2009
Remove the return statement from the saySomething function.
BKBK
Community Expert
Community Expert
February 11, 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>
Inspiring
February 10, 2009
You can achieve the same thing with a lot less code if you used cfform and cfinput.