LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-14-2009, 06:48 PM   #1
cmnorton
Member
 
Registered: Feb 2005
Distribution: Ubuntu, CentOS
Posts: 585

Rep: Reputation: 35
Changing Enter Key Behavior In PHP/HTML FORM


I have a very simple form that is implemented as an html form with embedded PHP. I would like to prevent the default behavior of pressing the Enter key from triggering the submit action of the form. Is there anyway to do that?

I have a workaround, which is to have the post action of the submit call a PHP function, instead of just forwarding to another web site, but I am curious about fixing my current problem.

I cannot prevent a user from pressing CR after entering a value (money) in a text field in the form. I have used visual instructions to request that the user check a checkbox on the form that says confirm amount.
 
Old 01-14-2009, 10:18 PM   #2
dcroxton
Member
 
Registered: Apr 2004
Distribution: Ubuntu Jaunty
Posts: 65

Rep: Reputation: 16
I can't say for certain, but I was never able to find away around this. Even if you intercept the enter key-press event, the form will still submit.

I had an idea that might work -- don't include a submit button. My theory is that the enter key actually activates the submit button (which is treated as the default button) rather than submitting the form directly. If you just put in a regular button and have it call a javascript function to submit the form, it might work -- I haven't had a chance to test it.

A more drastic solution (if that doesn't work) would be to put the input boxes on the page, but not in a form. When you're ready for the user to submit, you could transfer the values to hidden fields in a form and submit that. Another alternative along the same lines might be to have the input boxes in a form, but don't give the form an action; use javascript to assign an action to the form and submit it.

Short of that, I don't know of any way to make a browser not treat the enter key as a submit action.
 
Old 01-15-2009, 02:16 AM   #3
vandergugten
LQ Newbie
 
Registered: Feb 2006
Posts: 1

Rep: Reputation: 1
You can prevent submittal of the form by returning false from the onsubmit() event handler.

Here is an example:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
	<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
function handleSubmit()
{
	if( confirm("Do you want to submit the form data?") ) {
		return true;
	} else {
		return false;
	}
}

//-->
</script>
</head>

<body>

<form id="myform" name="myform" action="somepage.php" method="get" onsubmit="return handleSubmit();">
<input type="text" name="mytext" id="mytext" value="some text data">
<input type="button" value="Click me">
</form>

</body>
</html>
Hope this answers your question.
 
Old 01-15-2009, 04:12 AM   #4
inanimous
LQ Newbie
 
Registered: Apr 2006
Posts: 2

Rep: Reputation: 1
perhaps this may help...

personally, i wouldn't mess with default/familiar user habits/behaviors - that is UI 101.

however js user side validation is definitely the way to go, just have the form verify that everything is in place before is actually sending the data, and also prevent refreshing the page and needing to fill in the data again.

here is a nice example:

http://www.leigeber.com/2008/04/dyna...rm-validation/

with a demo: http://sandbox.leigeber.com/messages/messages.html


hope it is helpful!
 
  


Reply

Tags
form, html, php, submit


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
php global variable appears in every html form with firefox 3.0.3 baikonur Programming 1 11-02-2008 06:16 AM
PHP: html Form Select function problem camlinux Programming 2 05-15-2005 09:23 PM
PHP:Unable to post variables from html form to mysql database vikram_cvk Linux - Software 1 09-29-2004 04:01 PM
PHP and HTML Form Post Savahn Programming 12 06-30-2004 04:15 PM
Simple php script with html form not working. sinsoush Programming 4 04-01-2004 09:02 PM


All times are GMT -5. The time now is 11:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration