Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    May 2009
    Posts
    168
    Thanks
    78
    Thanked 1 Time in 1 Post

    Retrieiving form data from array

    Hi guys. I have a form where I have dynamic fields, so I dont know how many fields there will be. To handle this, each tr has td's like so
    Code:
    <td><input type="text" placeholder="Giving" class="selector" name="giving2[]" autocomplete="off"/></td>
    <td><input type="text" placeholder="Getting" class="selector" name="getting2[]" autocomplete="off"/></td>
    So, the name is an array, and it is the same in all rows. At the moment, to retrieve the data, I am trying
    Code:
    foreach ($_POST['giving2'] as $item) {
    	$giving += $item;
    }
    foreach ($_POST['getting2'] as $item) {
        $getting += $item;
    }
    
    $body = "
    ===== Enquiry: $reference =====\n
    Enqury made: ".date("d/m/y H:i:s",time())."\n\n
    
    Giving: $giving\n\n
    Getting: $getting\n\n"
    ;
    Then I send it via email. The output I am getting for these fields is 0 though, so its not giving me the correct values. Am I missing something here or doing something wrong?


    Cheers

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    What is the datatype of the values put into those fields?
    If they are numeric, you can run a simple array_sum() on it. If they are not numeric, you intend to concatenate them using the . operator instead of the +, but then you should look at using implode() instead of iterating with foreach.

  • Users who have thanked Fou-Lu for this post:

    nick2price (09-25-2012)

  • #3
    Regular Coder
    Join Date
    May 2009
    Posts
    168
    Thanks
    78
    Thanked 1 Time in 1 Post
    Thanks for the reply, sorry its gone a bit over the top of me. What I showed is one row, its really more like
    Code:
    <tr>
       <td><input type="text" placeholder="Giving" class="selector" name="giving2[]" autocomplete="off"/></td>
       <td><input type="text" placeholder="Getting" class="selector" name="getting2[]" autocomplete="off"/></td>
    </tr>
    
    <tr>
       <td><input type="text" placeholder="Giving" class="selector" name="giving2[]" autocomplete="off"/></td>
       <td><input type="text" placeholder="Getting" class="selector" name="getting2[]" autocomplete="off"/></td>
    </tr>
    
    ...
    And I also have a javascript method where more rows with the same fields can be added, so I will never be sure as to the total number of rows. So what I have done is made it so all names are an array. The field inputed into these fields is text. The reason I used a foreach loop is because wouldnt I need to loop the array which holds my values, especially if I dont know how many there will be?

    My overall aim is to take all the inputs from all the fields, and send them in an email. So, the values from row 1 will display first Giving[0] Getting[0], then row 2 etc. As I say though, I cannot do it like this because I dont know how many there will be.

    Nick

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You mean you want to match up the two arrays so data from the first one matches with the data in the second?
    There are a number of ways to do this. I'll iterate only.
    PHP Code:
    $sResult "";
    if (
    count($_POST['giving2']) == count($_POST['getting2']))
    {
        while ((
    $curGiv current($_POST['giving2'])) && ($curGet current($_POST['getting2'])))
        {
            
    $sResult .= sprintf("Giving: %s\nGetting:%s\n\n"$curGiv$curGet);
            
    next($_POST['giving2']);
            
    next($_POST['getting2']);
        }

    Something like that would work. HTML could be massaged as well to represent a multidimensional array of getting and giving together.


  •  

    Posting Permissions

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