?
Solved

Coldfusion 8 - How to submit a form using enter key

Posted on 2009-06-29
16
Medium Priority
?
1,315 Views
Last Modified: 2012-05-07
I have a page that contains a single field search form and a cfgrid. When I hit the eneter key, regardless of whether the text field is in focus or not the entire page refreshes instead of the search form being submitted. How do I fix this behavior? I do not want the page to ever refresh when hitting enter and I would like the form to be submitted on hitting enter only if there is a search term in the field.
0
Comment
Question by:futr_vision
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 2
16 Comments
 
LVL 3

Expert Comment

by:jdthedj
ID: 24741639
I assume you mean hitting enter when the focus is on the search field?  If so you could use a JavaScript function to trigger in the onBlur event of the searchfield

<input type="text" name="searchfield" size="10"onBlur="ProcessForm()">

<SCRIPT LANGUAGE="JavaScript">
<!-- hide

 function ProcessForm()
  {
if(document.yourformname.elements("searchfield").value!=""
{
document.yourformname.submit();
      }
}
//-->

</script>
0
 
LVL 3

Expert Comment

by:jdthedj
ID: 24741642
oops, missed the closing bracket at th end of the if( statement
0
 

Author Comment

by:futr_vision
ID: 24745089
Hmmm. Can you have an onBlur and an onClick event for the same button? I currently have an onClick event the launches a cfwindow. Below is my code for the button as it currently stands.
	<cfform name="clientSearch">
        <cfinput type="hidden" name="collname" value="searchDB">
        <cfinput type="text" value="Search Term" name="searchTerm" required="yes">
        <cfinput type="button" name="searchButton" value="Search" onClick="javascript:ColdFusion.Window.show('searchResults')" ><br />
	</cfform>

Open in new window

0
TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

 
LVL 39

Expert Comment

by:gdemaria
ID: 24745166
you can have onBlur and onClick together.   OnBlur fires when someone leaves a field, onClick fires when someone clicks the field/button.

You definitely do not want to execute a search onBlur.   Imagine putting your cursor into a search field and then you move to another field and the page automatically submits a search on you..

0
 
LVL 39

Expert Comment

by:gdemaria
ID: 24745190
0
 

Author Comment

by:futr_vision
ID: 24745488
Thanks but please excuse my ignorance. What you are saying is that I can leave my onClick in place and add onKeyPress? Or is there a way to use onKeyPress to fire off both the submit and launch the cfwindow?

I am really new to programming so if you can help me out with what the code will look like that would be great.
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 24745545
Can you explain what you're trying to do?   Your initial question just asks to test for the Enter key to submit a form.  Where does the pop-up box come in?  

Are you trying to submit a form and open a new window at the same time?  Why both?   Are you thinking the form submit will appear in the new open window doing that?  (it won't)     What is in the pop-up?

0
 

Author Comment

by:futr_vision
ID: 24745872
Ahh. I overlooked that in my initial question. Sorry. I have a page with both a cfgrid and a cfform. THe form is a single text field and a single button. When the user enters a search terms and clicks the button a cfwindow opens (essentially a positioned div that loads another page). That all works great. The problem is that the window is only triggered by the onClick event. When a person hits enter after entering a search term that page refreshes instead of submitting the term to the cfwindow. Below is the code for the cfwindow that opens up.
<!--- cfform that opens the cfwindow with the search results --->
 
<cfform name="clientSearch">
   <cfinput type="hidden" name="collname" value="searchDB">
   <cfinput type="text" value="Search Term" name="searchTerm" required="yes">
   <cfinput type="button" name="searchButton" value="Search" onClick="javascript:ColdFusion.Window.show('searchResults')" >
</cfform>
 
 
<!--- cfwindow that displays the search results --->
 
 
<cfwindow center="true" width="610" height="500" name="searchResults"
        minHeight="400" minWidth="500" 
        title="Search Results" initshow="false"
        source="sresults.cfm?collname={clientSearch:collname}&searchTerm={clientSearch:searchTerm}"/>

Open in new window

0
 
LVL 39

Expert Comment

by:gdemaria
ID: 24746256
So, you do NOT want to submit when the user presses enter.   You keep saying you do...

When the user presses enter you want to do the same action as onClick which is the javscript  show window command, you do NOT want to submit the form..



<script type="text/javascript">
 function doSearch(evt){
   evt = (evt) ? evt : event;
   var charCode = (evt.which) ? evt.which : evt.keyCode;
   if(charCode==13) {javascript:ColdFusion.Window.show('searchResults');}
 }
</script>
 
 
<cfinput type="text" value="Search Term" name="searchTerm" onKeyPress="doSearch(event);">

Open in new window

0
 

Author Comment

by:futr_vision
ID: 24746411
When I say submit when the enter key is pressed I am wanting the same functionality as I have when I click the button. In this case it is lauching the cfwindow and passing the parameters. Please forgive my poor usage of the jargon ;)
0
 

Author Comment

by:futr_vision
ID: 24747122
That almost did it. The cfwindow now pops open when the cursor is in the text field and enter is pressed but immediately after that behavior the entire page is refreshed.
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 24747275
That is the default action of a form with only one field, try this...

change..
onKeyPress="doSearch(event);"

to..

onKeyPress="doSearch(event);return false;"

0
 

Author Comment

by:futr_vision
ID: 24747670
That did the trick but now the search box is disabled. I can put the cursor in the text box but I can not type a new search term or delete the default value.
0
 
LVL 39

Accepted Solution

by:
gdemaria earned 1000 total points
ID: 24747727
oh right, we need to move the return false; to only happen on the Enter key..

both function and input tag have changed...
<script type="text/javascript">
 function doSearch(evt){
   evt = (evt) ? evt : event;
   var charCode = (evt.which) ? evt.which : evt.keyCode;
   if(charCode==13) {javascript:ColdFusion.Window.show('searchResults');return false;}
   else {return true;}
 }
</script>
 
 
<cfinput type="text" value="Search Term" name="searchTerm" onKeyPress="return doSearch(event);">

Open in new window

0
 

Author Comment

by:futr_vision
ID: 24747798
Looks like that did it.
0
 

Author Closing Comment

by:futr_vision
ID: 31598195
Thanks for walking me through this.
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question