www.webdeveloper.com
Results 1 to 12 of 12

Thread: How to retain input textbox after submitting page?

  1. #1
    Join Date
    Feb 2008
    Posts
    134

    How to retain input textbox after submitting page?

    Ok I got a really silly question to ask

    This is supposed to be very simple but its not cutting it for me so here is the question.

    I have a user registration form..the user enters his/her info and clicks submit.

    server does some error checking..if errors are found, an error message is displayed to the user ....

    now, I want to be able to retain the user's information in the input textboxes so that the input boxes do not go blank...


    so how do I do this?

    This is what I have:

    <form name="myform" method="post" action='<?=$_SERVER['PHP_SELF'];?>'>
    <input type="text" name="email" id="email" value="<?=$_POST['email'];?>"/>
    </form>

    This is supposed to work right? however I get an error message saying that the index 'email' is undefined.


    THe other solution is to make the code ugly like this:

    <input type="text" name="email" id="email" value="<? if(isset($_POST['email'])) echo $_POST['email']; ?>"/>

    and I sure dont want to do it this way...its too long and ugly...

    Am I missing something in the first method?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,241
    When you first access the form page, nothing has been submitted, so $_POST will be empty. You should therefore check that the value exists before trying to use it:
    PHP Code:
    <input type="text" name="email" id="email" value="<?php
    if(isset($_POST['email'])) {
       echo 
    htmlentities($_POST['email']);  // always filter outputs of external data
    }
    ?>" />
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Feb 2008
    Posts
    134
    Thats what I thought as well but if I'm not mistaken,

    I've seen this being used and it worked out fine...

    <input type="text" name="email" id="email" value="<?=$_POST['email'];?>"/>

    see that red equal sign?

    What does that mean?

    What is the difference if I went

    <input type="text" name="email" id="email" value="<? echo $_POST['email'];?>"/>

    ???

  4. #4
    Join Date
    Mar 2008
    Location
    CA
    Posts
    160
    Those two lines of code do exactly the same thing.
    You should get the same error messages.

    I recommend creating a function

    PHP Code:
    <?
    function prepopulate($name) {
     if(isset(
    $_POST[$name])) {
      return 
    $_POST[$name];
     }
     else {
      return 
    "";
     }
    }
    ?>
    ...

    PHP Code:
    <input type="text" name="email" id="email" value="<?=prepopulate('email');?>"/>

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,241
    I've seen this being used and it worked out fine...

    <input type="text" name="email" id="email" value="<?=$_POST['email'];?>"/>
    The "<?=" tag (as well as the "<?" tag) will only work if the now-deprecated short_open_tags option is enabled. And even if it is, it still does not validate that the array element exists before trying to use it, so it will throw a notice-level warning whenever it is not. That warning may not be seen, depending on your error_reporting and display_errors setting, but it's still sloppy coding, plus it does nothing to filter the data being output, so ugly things can happen depending upon what the user entered in the first place.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Mar 2008
    Location
    CA
    Posts
    160
    Thanks NogDog. I took some training and they said DO NOT USE <?= ?>, but they never said why ... so, I kept on using it. The training also said to always use <?php ?> and NOT to use <? ?>. They never stated why so I still use the <? ?>.

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,241
    The main reason that the short_open_tags option has become deprecated is the potential for confusion with "<?xml" tags. Therefore PHP has decided in recent versions to make the default configuration have short_open_tags disabled, and it will most likely not be available at all in PHP 6. Therefore for code portability and future version upgrade compatibility, it's best to avoid the "<?" and "<?=" short_open_tags entirely - the "<?php" tag will always work. (Yes, you have to type a few more characters now, but that's a minuscule price to pay now compared to migrating to a new host in the future and finding out that all of your scripts break.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    Jun 2012
    Posts
    1
    Quote Originally Posted by jeremu View Post
    Thanks NogDog. I took some training and they said DO NOT USE <?= ?>, but they never said why ... so, I kept on using it. The training also said to always use <?php ?> and NOT to use <? ?>. They never stated why so I still use the <? ?>.
    You should use <?= ?> ... this is like an echo in PHP
    if you want to echo some value couz you saved this in your Database, a single <? ?> oder <?php ?> wont show it .. you can try this with firebug (plugin for Firefox) and see !

  9. #9
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by dejitoki View Post
    You should use <?= ?> ... this is like an echo in PHP
    if you want to echo some value couz you saved this in your Database, a single <? ?> oder <?php ?> wont show it .. you can try this with firebug (plugin for Firefox) and see !
    If you use the shorthand versions "<?=" or "<?" it may not function with all installs of php as for quite some time now the default has been to disable the short tags.

  10. #10
    Join Date
    Mar 2012
    Posts
    1,394
    The correct solution has been given, but sometimes you may need a quick-and-dirty answer to the problem. Which is, in this case, to add the following to your code:

    error_reporting(E_ALL ^ E_NOTICE);

    That should suppress the error warning.

  11. #11
    Join Date
    Apr 2010
    Posts
    227
    I turn off all errors from php since i never want actual users seeing php errors. would rather they get a blank page!

    Then i can add lines for testing.

    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);

    Once i m ready to upload to live site i escape those lines.

    /**
    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    **/

  12. #12
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by jedaisoul View Post
    The correct solution has been given, but sometimes you may need a quick-and-dirty answer to the problem. Which is, in this case, to add the following to your code:

    error_reporting(E_ALL ^ E_NOTICE);

    That should suppress the error warning.
    Probably should be noted that for this specific problem the above wouldn't do anything either since nothing would be parsed at all.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles