problem with pressing enter on one-textfield form

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • T.E.

    #1

    problem with pressing enter on one-textfield form

    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("Locatio n: 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
  • MJaC

    #2
    Re: problem with pressing enter on one-textfield form

    T.E. wrote:
    [color=blue]
    > Here's pseudocode of my php file:
    >
    > <?php
    > if (isset($_POST['submit']))
    > {
    > if (isset($_POST['textbox']))
    > header("Locatio n: 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>[/color]


    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("locatio n: 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 ;).

    Comment

    • T.E.

      #3
      Re: problem with pressing enter on one-textfield form

      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 <mjac@mjac.co.u k> wrote in message news:<4082F208. 7090907@mjac.co .uk>...
      [color=blue]
      > 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("locatio n: 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 ;).[/color]

      Comment

      Working...