SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Location
    United Kingdom
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Returning post data to a textarea - how?

    When a PHP script's validation catches an error and the user's data is returned to the form fields with the get_data("") function in a value attribute, how can the content of a textarea be returned also, with no value attribute allowed?

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    get_data() is not a native php function(meaning we have no clue what it does).

    But if you want to put a value in a textarea
    PHP Code:
    <textarea><?php echo $foo?></textarea>
    However, you probably want to escape the value using htmlspecialchars().

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Location
    United Kingdom
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does $foo represent the field name from which the data originally came? I assume so, but just want to be sure.

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Location
    United Kingdom
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just tried it and it didn't work.

  5. #5
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <textarea><?php echo get_data("fieldname"); ?></textarea>
    And replace the fieldname with the name of the textarea field. That's the only thing I can telll you by the info you gave us :P.

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure it does.

    $foo is a variable. You need to assign a value to a variable for it to be useful. For example
    PHP Code:
    // this wont do anything because $foo has no value
    echo $foo;

    // assign a value to the $foo variable
    $foo 'i am the foo variable';
    // now it will output the value
    echo $foo
    php will create some variables for you automatically. For example, when a form is posted, php will make some variables for you in the $_POST array. If your <textarea name="foobar" then php will create $_POST['foobar'] variable for you, and it will contain the value the form sent for the textarea.
    PHP Code:
    echo $_POST['foobar']; 

  7. #7
    SitePoint Enthusiast
    Join Date
    Jan 2007
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure did I understand, but I hope this helps;

    PHP Code:
    <?php

    // lets check is the form sent
    if (isset($_POST[field1]))
    {
    $form_is_posted 1;

      
    // lets check is the field empty
      
    if($_POST[field1] == "")
      {
      
    $error 1;
      }

        
    // if there is a problem with posted values
        
    if ($error == 1)
        {
        echo 
    "Error. The field1 is empty.";
        }

    }

    // if form is send and error is not 1, then we say;
    if ($form_is_posted == and $error != 1)
    {
    echo 
    "Yappadappadoo! You did it!";
    }
      else
      {
    echo <<<END
    <form action="yourfile.php" method="post">
    <textarea name="field1">
    $_POST[field1]</textarea>
    </form>
    END;
      }


    ?>

  8. #8
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are telling us that the data for your other forms are retrieved by using:
    PHP Code:
    get_data(""
    Why aren't you using the same function to retrieve the data for your textarea?

  9. #9
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might want this

    Code HTML4Strict:
    <textarea><?php if isset($_POST[field]) {echo $_POST[field] } ?></textarea>

  10. #10
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Location
    United Kingdom
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 023-jimmy View Post
    You are telling us that the data for your other forms are retrieved by using:
    PHP Code:
    get_data(""
    Why aren't you using the same function to retrieve the data for your textarea?
    I didn't state that I wasn't. I simply asked how to do it without entering the PHP code as a form field value. I wasn't aware that textareas cannot take value attributes until a tried to add one.

    Your own answer in post #5 was the closest and got me to where I needed to be. I found that I didn't need to include the echo, but apart from that, you found the fix. Thanks.

  11. #11
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Please put single quotes around your keys when accessing a string-indexed array:

    $_POST['field']

    If your PHP settings weren't hiding notices, PHP would be complaining to you about unknown constant field1 all over the place

  12. #12
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Location
    United Kingdom
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @ Dan. Um, I not at all sure what a string indexed array is, but thanks for the tip.

    Please note that this is not my code, though; just a simple script that I found that pretty much meets my needs. I can see a mix of single and double quotes in it depending on what's being quoted; should they all be single quotes?

  13. #13
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    No, either is fine for that purpose. The main difference between single and double quotes in PHP is that if you enclose a string in double quotes, its contents will be parsed, replacing variables with their values. If you enclose it in single quotes, it's treated as a literal string only.

    PHP Code:
    $var "Dan";

    echo 
    "Hello $var";
    //output: Hello Dan

    echo 'Hello $var';
    //output: Hello $var 

  14. #14
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Location
    United Kingdom
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK. That kind of makes sense. Thanks.


Bookmarks

Posting Permissions

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