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

problem with pressing enter on one-textfield form

P: n/a
Hi all,

I'm a newbie in php.

I encounter a problem with php form that has only one textfield.

If I click on the submit button, the form works fine. However, when I
press 'enter', the form doesn't work right.

After comparing that form with other forms, I noticed the problem
happens because I only have one textfield and a submit button in the
form.
If I add another textfield, I can hit 'enter' to submit the form;
however, that's not a good solution.

Has anyone encountered this problem before, and what's the best
solution?

Here's pseudocode of my php file:

<?php
if (isset($_POST['submit']))
{
if (isset($_POST['textbox']))
header("Location: otherpage.php"); //redirect to other page
}
?>
//else stay on the form page
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Please enter something in the textbox
<input type="text" name="textbox" size="20">
<input type="submit" value="Submit" name="submit">
</form>

Does anyone have the right solution for this problem?
FYI, I use php 4.3.3 and I test it on IE 6.0.

I really appreciate your help and thx in advance.

Regards,
Tony
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
T.E. wrote:
Here's pseudocode of my php file:

<?php
if (isset($_POST['submit']))
{
if (isset($_POST['textbox']))
header("Location: otherpage.php"); //redirect to other page
}
?>
//else stay on the form page
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Please enter something in the textbox
<input type="text" name="textbox" size="20">
<input type="submit" value="Submit" name="submit">
</form>

Hello, I would be happy to help :)

I guess it does not work when you press enter because the submit button
variable is not being sent with the form (right?). To counter this use
the code below:

You have to remove the if(isset($_POST['BUTTONNAME'])){ }

<?php
if (isset($_POST['textbox'])){
/* redirects to other page and exits */
header("location: otherpage.php");
exit;
}
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Please enter something in the textbox
<input type="text" name="textbox" size="20" />
<input type="submit" name="submit" value="Submit" />
</form>
NOTE: I added an exit command after the header is sent. Not having this
is a severe security issue, since the server will execute all the code
below even if the client has been directed somewhere else.

Hope I helped ;).
Jul 17 '05 #2

P: n/a
Thanks MJaC for your input.

I think I also need to check if textbox is empty, because
isset(textbox) returns 'true' when my cursor is inside the box as I
hit enter (even though the textbox is empty).

And thanks as well for the 'exit()' reminder ;)

T.E.
MJaC <mj**@mjac.co.uk> wrote in message news:<40**************@mjac.co.uk>...
Hello, I would be happy to help :)

I guess it does not work when you press enter because the submit button
variable is not being sent with the form (right?). To counter this use
the code below:

You have to remove the if(isset($_POST['BUTTONNAME'])){ }

<?php
if (isset($_POST['textbox'])){
/* redirects to other page and exits */
header("location: otherpage.php");
exit;
}
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Please enter something in the textbox
<input type="text" name="textbox" size="20" />
<input type="submit" name="submit" value="Submit" />
</form>
NOTE: I added an exit command after the header is sent. Not having this
is a severe security issue, since the server will execute all the code
below even if the client has been directed somewhere else.

Hope I helped ;).

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.