2 Replies - 345 Views - Last Post: 25 September 2012 - 09:24 AM Rate Topic: -----

#1 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Retrieving multiple bits of data

Posted 24 September 2012 - 03:29 PM

Hi guys. Say I have a form like so
<table class="table1">
  <tr>
    <td class="title">1:</td>
    <td><input type="text" placeholder="Giving" class="selector" name="giving1" autocomplete="off"/></td>
    <td><input type="text" placeholder="Getting" class="selector" name="getting1" autocomplete="off"/></td>
  </tr>

  <tr>
    <td class="title">2:</td>
    <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>
</table>


I also have a button which uses javascript to add new rows to my table, with the exact same fields. When new rows are added, the field names will increment e.g. giving3 for third row, giving4 for fourth row etc. I dont know how many rows will be added though.

Now once the user adds however many rows they need, enters all the data, and clicks submit, I need to send that data to my email. At the moment, all I do in php is
$giving= $_POST['giving1'];
$getting= $_POST['getting2'];


Obviously the above will only grab the data from the first row. I cant just produce a load of these post for different rows as I dont know how many rows there are. So how could I fetch all this data in this kind of scenario?

Cheers

Is This A Good Question/Topic? 0
  • +

Replies To: Retrieving multiple bits of data

#2 nightscream  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 237
  • Joined: 04-December 08

Re: Retrieving multiple bits of data

Posted 24 September 2012 - 11:11 PM

try to use name="giving[]" and name="getting[]"

now you can easily fetch in php
foreach ($_POST['giving'] as $k => $v) {
each 'giving' . $k . ' = ' . $v;
}
/* will return
giving0 = xxx
giving1 = yyy
giving2 = zzz
*/

foreach ($_POST['getting'] as $k => $v) {
each 'getting' . $k . ' = ' . $v;
}
/* will return
getting0 = xxx
getting1 = yyy
getting2 = zzz
*/


Was This Post Helpful? 1
  • +
  • -

#3 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Retrieving multiple bits of data

Posted 25 September 2012 - 09:24 AM

Kool. I have tried something slightly differently, but getting 0 as my output. I have changed all my td's to use the same array
<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>


Then in the php file, I do
foreach ($_POST['giving2'] as $item) {
	$giving += $item;
}
foreach ($_POST['getting2'] as $item) {
    $getting += $item;
}


And to add to the email I do
$body = "
===== Enquiry: $reference =====\n
Enqury made: ".date("d/m/y H:i:s",time())."\n\n

Giving: $giving\n\n
Getting: $getting\n\n"
;


I have removed some of the outputs from above just to show to two I am concerned with, so it may be slightly wrong. Is there something I am missing in what I am doing here?

Cheers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1