Retrieve value of a textarea with PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • baehr
    New Member
    • Feb 2010
    • 3

    Retrieve value of a textarea with PHP

    Would anyone perhaps know how to get the value of a specific element in an HTML document with PHP? What I'm doing right now is using file_get_conten ts to pull up the HTML code from another website, and on that website there is a textarea:

    Code:
    <textarea id="body" name="body" rows="12" cols="75" tabindex="3">Hello, World!
    
    </textarea>
    What I want to do is have my script do the file_get_conten ts and just pull out the "Hello World!" from the textarea. Is that possible? Sorry for bugging you guys, again, you give such helpful advice :].
  • baehr
    New Member
    • Feb 2010
    • 3

    #2
    *bump*
    I hope someone's there :]

    Comment

    • johny10151981
      Top Contributor
      • Jan 2010
      • 1059

      #3
      $_GET or $_POST was suppose to work :?

      example:
      index.html
      Code:
      <form mathod=get action=query.php>
       <textarea name=txtare>
       </textarea>
      </form>
      query.php
      Code:
      <?php
      echo $_GET[txtare];
      ?>

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        you should use correct syntax, while you’re at it:
        Code:
        <form method="get" action="query.php">
         <textarea name="txtare">
         </textarea>
        </form>
        Code:
        <?php
        echo $_GET["txtare"];
        ?>

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Hey.

          Assuming you have the HTML in a string in your PHP script, you could use a few methods to get at the data.

          The most reliable way would probably be to parse it as an XML document, using on of the XML parsers. (I would recommend trying SimpleXML). Then it is a "simple" matter of loaded and traversing the HTML to find the appropriate value.

          You could also try using Regular Expressions. It's a bit more involved, and not as reliable when dealing with HTML. (Regular Expressions aren't ideally suited for "irregular" markup like HTML.)

          And then there is always old-school string manipulation, but that pretty unreliable and prone to errors. It's also a pain to implement.

          In the end, loading it up as XML would be my first choice.

          Comment

          Working...