471,596 Members | 826 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,596 software developers and data experts.

how can inputed value display on a textfield

123 100+
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
May 9 '09 #1
6 3953
Markus
6,050 Expert 4TB
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).

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. function get_value($key, $default = '') {
  3.     if (isset($_POST[$key]) {
  4.         return $_POST[$key];
  5.     }
  6.     return $default;
  7. }
  8. ?>
  9. <input type="text" name="my_input" value="<?php echo get_value('my_input', 'default text');?>" />
  10.  
You could expand the helper function to include GET and COOKIE and SESSION if you liked. You could even use $_REQUEST.
May 9 '09 #2
simon2x1
123 100+
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
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. if (isset($_POST['submit'])) {
  4.  
  5. $firstname = ($_POST['firstname']);
  6.  $lastname = ($_POST['lastname']);
  7. $email = ($_POST['email']);
  8.  
  9. function get_value($fname, $default = '') {
  10.    if (isset($_POST[$fname])) {
  11.          return $_POST[$fname];
  12.      }
  13.      return $default;
  14.          }
  15.  
  16. mysql_query("INSERT INTO USER (firstname, lastname, email) VALUES
  17. ('" . $firstname . "', '" . $lastname . "', '" . $email . "')") or die(mysql_error());
  18.  
  19. <form  method="post" action="home.php">
  20. <input type="text" name="firstname" value="<?php echo get_value('fname','firstname');?> />
  21. <input type="text" name="lastname"  />
  22. <input type="text" name="email"  />
  23. <input type="submit" name="submit"  />
  24. </form>
  25.  
May 12 '09 #3
simon2x1
123 100+
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
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (isset($_POST['submit'])) {
  3.  
  4. $firstname = ($_POST['firstname']);
  5.  $lastname = ($_POST['lastname']);
  6. $email = ($_POST['email']);
  7.  
  8. function get_value($fname, $default = '') {
  9.    if (isset($_POST[$fname])) {
  10.          return $_POST[$fname];
  11.      }
  12.      return $default;
  13.          }
  14.  
  15. mysql_query("INSERT INTO USER (firstname, lastname, email) VALUES
  16. ('" . $firstname . "', '" . $lastname . "', '" . $email . "')") or die(mysql_error());
  17.  
  18. <form  method="post" action="home.php">
  19. <input type="text" name="firstname" value="<?php echo get_value('fname','firstname');?> />
  20. <input type="text" name="lastname"  />
  21. <input type="text" name="email"  />
  22. <input type="submit" name="submit"  />
  23. </form>
  24.  
May 12 '09 #4
dlite922
1,584 Expert 1GB
What does the error say? Usually Errors tell you what's wrong.




@simon2x1
May 13 '09 #5
simon2x1
123 100+
sorry what i mean by error is that it do not work pls help me check my code and see what i do wrong
May 14 '09 #6
hoopy
88
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('fname','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:

Expand|Select|Wrap|Line Numbers
  1. <?
  2. function get_value($fname, $default = '') {
  3.   if (isset($_POST[$fname])) {
  4.   return $_POST[$fname];
  5.   }
  6.   return $default;
  7. }
  8.  
  9. if(isset($_POST['submit'])) 
  10. {
  11.   $firstname = $_POST['firstname'];
  12.   $lastname = $_POST['lastname'];
  13.   $email = $_POST['email'];
  14.  
  15.   mysql_query("
  16.     INSERT INTO USER (firstname, lastname, email) VALUES
  17.     ('" . $firstname . "', '" . $lastname . "', '" . $email . "')
  18.   ") or die(mysql_error());
  19. }
  20. ?>
  21.  
  22. <form  method="post" action="home.php">
  23. <input type="text" name="firstname" value="<?php echo get_value('fname','firstname');?> />
  24. <input type="text" name="lastname"  />
  25. <input type="text" name="email"  />
  26. <input type="submit" name="submit"  />
  27. </form>
May 14 '09 #7

Post your reply

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

Similar topics

13 posts views Thread by Stumped and Confused | last post: by
5 posts views Thread by xirowei | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.