By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,034 Members | 1,714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,034 IT Pros & Developers. It's quick & easy.

How to POST 'submit' when ENTER KEY is pressed on a FORM with only one textfield

P: 13
Hi!

Here's my situation:
I have one textfield with one 'submit' button in PAGE1.PHP.

When I click on the 'submit' button I am sent to PAGE2.PHP where I have a "switch" routine that checks which 'submit' button was clicked and directs which instructions to do next.

My Problem is this:
On PAGE1.PHP, there is only one textfield. After filling in the textfield, most of the time people press the 'ENTER KEY' instead of clicking the submit button. Naturally, I am now sent to PAGE2.PHP after pressing the enter key but when I check the value of 'submit', it's empty, or not set, or not posted. So, my "switch" routine can no longer direct my program to the appropriate instructions.

Can someone please help or show me how to post the 'submit' variable when I press the enter key.

Observation:
I have observed that this behaviour only happens with one textfield on the FORM. Other FORMS with more than one textfield, treats the enter key the same way as clicking the submit button... it posts the 'submit' variable

Hope someone can help.

Learning
Sep 6 '07 #1
Share this Question
Share on Google+
9 Replies


ak1dnar
Expert 100+
P: 1,584
Could you please post the form and the php script that you have used for this. (no need to post the entire code here)
Sep 6 '07 #2

P: 13
Could you please post the form and the php script that you have used for this. (no need to post the entire code here)
Hi ajaxrand.

Here's a simple version for easier understanding:

PAGE1.PHP:

Expand|Select|Wrap|Line Numbers
  1. <form action="page2.php" method="post">
  2. <input type="text "name="name" size="20"/>
  3. <input type="submit" name="submit" value="process1"/>
  4. </form>


PAGE2.PHP:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. switch ($_POST['submit']) {
  3. case 'process1':
  4. echo "processing ONE";
  5. break;
  6. case 'process2':
  7. echo "processing TWO";
  8. break;
  9. default:
  10. break;
  11. }
  12. ?>
When you execute this code, try clicking on the button. Try again and this time press enter instead of clicking the button and you'll see what I mean.

Oh and by the way, I'm using Internet Explorer as my browser.

Learning
Sep 6 '07 #3

ak1dnar
Expert 100+
P: 1,584
Please find out how to use the CODE tags in the correct way. Do not type or paste the code with line numbers. It will set by the text editor by default.

This will do the job for your requirement. and why are you using "switch case" for this, its simple with this.
Expand|Select|Wrap|Line Numbers
  1. <?Php
  2. if(isset($_POST['submit'])){
  3. echo $_POST['name'];
  4. }
  5. ?>
  6.  
The bad news is the html form is not submitting the text input when there is a enter key posting. but the great fire fox is doing the job on both the ways. Yet I couldn't find out a solution for the IE issue. may be some one can help you from the HTML forum.Its better to ask the question on this forum. or I can move it there since It is a HTML issue. Shall I?
Sep 6 '07 #4

P: 13
Please find out how to use the CODE tags in the correct way. Do not type or paste the code with line numbers. It will set by the text editor by default.

This will do the job for your requirement. and why are you using "switch case" for this, its simple with this.
Expand|Select|Wrap|Line Numbers
  1. <?Php
  2. if(isset($_POST['submit'])){
  3. echo $_POST['name'];
  4. }
  5. ?>
  6.  
The bad news is the html form is not submitting the text input when there is a enter key posting. but the great fire fox is doing the job on both the ways. Yet I couldn't find out a solution for the IE issue. may be some one can help you from the HTML forum.Its better to ask the question on this forum. or I can move it there since It is a HTML issue. Shall I?
Hi again ajaxrand.

Yes, I kinda realize that after submitting my concern. I'll switch to HTML forum and find it out there.

And yes, I've already tried that approach instead of using the 'switch'. I may have to go back to that if I can't find out a solution to the enter key thing.

Thank you for helping me understand how to use the code tags. I never really understood that till now.

I really appreciate your taking time to share your thoughts to help me out.

Learning
Sep 6 '07 #5

ak1dnar
Expert 100+
P: 1,584
No problem good luck with your project.Since you have already posted another thread on the HTML/CSS forum, I am not going to move this one.
Sep 6 '07 #6

Atli
Expert 5K+
P: 5,058
Hi.

If a form is submitted without the use of a Submit button, none of the Submit <input> tags will be included in the data that is sent.

My advice is to decide on a default course of action, if the user presses the enter key, or the form is submitted via JavaScript, then you can add the 'default' case in your switch clause and handle all such cases the same.

For example:
Expand|Select|Wrap|Line Numbers
  1. switch($_POST['mySubmit'])
  2. {
  3.   case 'btn1':
  4.     # Do stuff for btn1
  5.     break;
  6.   case 'btn2':
  7.     # Do stuff for btn2
  8.     break;
  9.   case 'btn3':
  10.     # Do stuff for btn3
  11.     break;
  12.   default:
  13.     # Do stuff if $_POST['mySubmit'] has any
  14.     # other value than the once listed above.
  15. }
  16.  
Sep 6 '07 #7

ak1dnar
Expert 100+
P: 1,584
Hey guys,
I have merged duplicate threads.
Sep 6 '07 #8

P: 13
Hi.

If a form is submitted without the use of a Submit button, none of the Submit <input> tags will be included in the data that is sent.

My advice is to decide on a default course of action, if the user presses the enter key, or the form is submitted via JavaScript, then you can add the 'default' case in your switch clause and handle all such cases the same.

For example:
Expand|Select|Wrap|Line Numbers
  1. switch($_POST['mySubmit'])
  2. {
  3.   case 'btn1':
  4.     # Do stuff for btn1
  5.     break;
  6.   case 'btn2':
  7.     # Do stuff for btn2
  8.     break;
  9.   case 'btn3':
  10.     # Do stuff for btn3
  11.     break;
  12.   default:
  13.     # Do stuff if $_POST['mySubmit'] has any
  14.     # other value than the once listed above.
  15. }
  16.  
Hello Atli,

Yes, you're right. I will definitely used your solution. You're a genius.

Thank you. I really appreciate it.

Learning
Sep 7 '07 #9

P: 13
Hey guys,
I have merged duplicate threads.

Hi Ajaxrand,

Thank you. You're all very helpful.

Learning
Sep 7 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.