how can inputed value display on a textfield

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • simon2x1
    New Member
    • Dec 2008
    • 123

    how can inputed value display on a textfield

    i want an inputed value that a user enter into the textfield
    should remain after clicking submit and an error message display
    that invalued email.the email textfield will be blank why the other
    textfield will still display the inputed value
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    The data will be available in the GET or POST array once the form has been submitted. Therefore, you can set the value of an input field to the data from your GET or POST array (if it exists).

    Code:
    <?php 
    function get_value($key, $default = '') {
        if (isset($_POST[$key]) {
            return $_POST[$key];
        }
        return $default;
    }
    ?>
    <input type="text" name="my_input" value="<?php echo get_value('my_input', 'default text');?>" />
    You could expand the helper function to include GET and COOKIE and SESSION if you liked. You could even use $_REQUEST.

    Comment

    • simon2x1
      New Member
      • Dec 2008
      • 123

      #3
      i have try the code but is give me an error where my input field his also how can do the same for the other field i will appreciate if you made the adjustment on my code
      Code:
      <?php
      if (isset($_POST['submit'])) {
      
      $firstname = ($_POST['firstname']);
       $lastname = ($_POST['lastname']);
      $email = ($_POST['email']);
      
      function get_value($fname, $default = '') {
         if (isset($_POST[$fname])) {
               return $_POST[$fname];
           }
           return $default;
       		}
      
      mysql_query("INSERT INTO USER (firstname, lastname, email) VALUES
      ('" . $firstname . "', '" . $lastname . "', '" . $email . "')") or die(mysql_error());
      
      <form  method="post" action="home.php">
      <input type="text" name="firstname" value="<?php echo get_value('fname','firstname');?> />
      <input type="text" name="lastname"  />
      <input type="text" name="email"  />
      <input type="submit" name="submit"  />
      </form>

      Comment

      • simon2x1
        New Member
        • Dec 2008
        • 123

        #4
        i have try the code but is give me an error where my input field his also how can do the same for the other field i will appreciate if you made the adjustment on my code
        Code:
        <?php
        if (isset($_POST['submit'])) {
        
        $firstname = ($_POST['firstname']);
         $lastname = ($_POST['lastname']);
        $email = ($_POST['email']);
        
        function get_value($fname, $default = '') {
           if (isset($_POST[$fname])) {
                 return $_POST[$fname];
             }
             return $default;
         		}
        
        mysql_query("INSERT INTO USER (firstname, lastname, email) VALUES
        ('" . $firstname . "', '" . $lastname . "', '" . $email . "')") or die(mysql_error());
        
        <form  method="post" action="home.php">
        <input type="text" name="firstname" value="<?php echo get_value('fname','firstname');?> />
        <input type="text" name="lastname"  />
        <input type="text" name="email"  />
        <input type="submit" name="submit"  />
        </form>

        Comment

        • dlite922
          Recognized Expert Top Contributor
          • Dec 2007
          • 1586

          #5
          What does the error say? Usually Errors tell you what's wrong.




          Originally posted by simon2x1
          i have try the code but is give me an error where my input field his also how can do the same for the other field i will appreciate if you made the adjustment on my code
          Code:
          <?php
          if (isset($_POST['submit'])) {
          
          $firstname = ($_POST['firstname']);
           $lastname = ($_POST['lastname']);
          $email = ($_POST['email']);
          
          function get_value($fname, $default = '') {
             if (isset($_POST[$fname])) {
                   return $_POST[$fname];
               }
               return $default;
           		}
          
          mysql_query("INSERT INTO USER (firstname, lastname, email) VALUES
          ('" . $firstname . "', '" . $lastname . "', '" . $email . "')") or die(mysql_error());
          
          <form  method="post" action="home.php">
          <input type="text" name="firstname" value="<?php echo get_value('fname','firstname');?> />
          <input type="text" name="lastname"  />
          <input type="text" name="email"  />
          <input type="submit" name="submit"  />
          </form>

          Comment

          • simon2x1
            New Member
            • Dec 2008
            • 123

            #6
            sorry what i mean by error is that it do not work pls help me check my code and see what i do wrong

            Comment

            • hoopy
              New Member
              • Feb 2009
              • 88

              #7
              It does not look like you are closing your IF statement and also have HTML embedded in the PHP which in turn has this embedded in it:

              <?php echo get_value('fnam e','firstname') ;?> />

              Simply saying "It doesnt work" does not help, give the errors which PHP has given to you.

              Try this code which is a cleaned up version of your existing version:

              Code:
              <?
              function get_value($fname, $default = '') {
                if (isset($_POST[$fname])) {
                return $_POST[$fname];
                }
                return $default;
              }
              
              if(isset($_POST['submit'])) 
              {
                $firstname = $_POST['firstname'];
                $lastname = $_POST['lastname'];
                $email = $_POST['email'];
               
                mysql_query("
                  INSERT INTO USER (firstname, lastname, email) VALUES
                  ('" . $firstname . "', '" . $lastname . "', '" . $email . "')
                ") or die(mysql_error());
              }
              ?>
              
              <form  method="post" action="home.php">
              <input type="text" name="firstname" value="<?php echo get_value('fname','firstname');?> />
              <input type="text" name="lastname"  />
              <input type="text" name="email"  />
              <input type="submit" name="submit"  />
              </form>

              Comment

              Working...