Connecting Tech Pros Worldwide Help | Site Map

Form Problem

Newbie
 
Join Date: Feb 2007
Posts: 3
#1: Feb 10 '07
I have this if statement in a form
<cfif #URL.Type# IS "Edit">
<input type="button" value="Edit message" onClick="validate()" name="Edit">
<cfelseif #URL.Type# IS "Delete">
<input type="submit" value="Delete message" name="Delete">
</cfif>

where validate() is a javascript function that submits the form.

<script type="text/javascript">
function validate(){
document.form.submit();
}
</script>

however, this is not working, i used a submit type button instead of the validate function and it worked, but i need the submission to be done in the javascript function and not as a submit button.

any suggestion??
thanks in advance.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Feb 10 '07

re: Form Problem


Are you sure that the name of your form is "form"?

Why is it necessary for it to be via Javascript? What if someone has Javascript turned off? It's much safer/better to have a normal submit button.

Another thing: there's no need to use hashes in cfif statements. You can simply use:
Expand|Select|Wrap|Line Numbers
  1. <cfif url.type EQ "Edit">
Newbie
 
Join Date: Feb 2007
Posts: 3
#3: Feb 11 '07

re: Form Problem


Quote:

Originally Posted by acoder

Are you sure that the name of your form is "form"?

Why is it necessary for it to be via Javascript? What if someone has Javascript turned off? It's much safer/better to have a normal submit button.

Another thing: there's no need to use hashes in cfif statements. You can simply use:

Expand|Select|Wrap|Line Numbers
  1. <cfif url.type EQ "Edit">

im only submiting in the javascript for testing reasons, but when this works ill have to check whether any of the inputs is empty, if it is then i have to fire an alert message else i will submit the form.
Do you have any idea how to check a form input if it is empty and thus display a prompt or alert message.???
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Feb 11 '07

re: Form Problem


Quote:

Originally Posted by hasano

im only submiting in the javascript for testing reasons, but when this works ill have to check whether any of the inputs is empty, if it is then i have to fire an alert message else i will submit the form.
Do you have any idea how to check a form input if it is empty and thus display a prompt or alert message.???

For this, you can still use the submit button, but also include an onsubmit in your form tag:
[HTML]<form name="form" action="..." onsubmit="return yourErrorCheck();">[/HTML]
Your error check function will check for errors. If there are any errors it will return false (stopping submit), otherwise return true (the form is submitted).

To check if a form input is empty, you can use the following code:
Expand|Select|Wrap|Line Numbers
  1. if (document.form.inputname.value == "") {
  2. alert("Please enter a value for blah blah...");
This is Javascript code, but since you're using Coldfusion, I've answered here.

If you still have problems, post again.
Reply