SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: $_post

  1. #1
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $_post

    I have been trying to retrieve data from forms. I know how to do this generally. But my problem here is that there is an unspecified numbers of data sent by post. I have maybe 1 up to ten textareas all named comment1, comment2, comment3, comment4

    How could I write code to retrive these unspecified number of boxes in say a while statement?

  2. #2
    Grumpy Minimalist
    Join Date
    Jul 2006
    Location
    Ontario, Canada
    Posts
    424
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    foreach ($_POST as $field_name => $field_value)
    {
        if (
    preg_match('/^comment[0-9]+$/D'$field_name))
        {
            
    //Found matching field named $field_name with value $field_value
        
    }

    Notes: Requires PCRE installed. Allows input data to be submitted in any order, including skips (ex. the following would be a valid submission: {'comment1000'=>'value1', 'comment0'=>'value2'}).

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Use a foreach loop to go through the fields. Depending on what you want to do with them after that!

    PHP Code:
    foreach($_POST as $key=>$data) {
        echo 
    $data ,'<br />';

    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #4
    Chessplayer kleineme's Avatar
    Join Date
    Apr 2004
    Location
    Germany
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you still can influence the naming of your textareas, then you could stay away from explicitly numbering them and create an textarea array instead, which could be accessed quite easily without regular expressions:

    PHP Code:
    <?php
    if(isset($_POST['comment'])){
      foreach(
    $_POST['comment'] as $idx => $value) {
        echo 
    "Index $idx : $value<br />";
      }
    }
    ?>
    <form method="post">
    <textarea name="comment[]"></textarea>
    <textarea name="comment[]"></textarea>
    <textarea name="comment[]"></textarea>
    <input type="submit" name="go" value="go" />
    </form>
    Never ascribe to malice,
    that which can be explained by incompetence.
    Your code should not look unmaintainable, just be that way.

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Kleineme

    Thats the solution I was looking for. I tried that but just named them comment rather than comment[].

    Thanks everyone. I will take into account the other solutions as well


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
  •