Page 1 of 1

Array on Form - Passing it to PHP

Posted: Tue Mar 09, 2010 5:20 am
by Xctazy
Hi everyone.

I'd like some help here if you can. I'll explain:

I've built a form in html (which you can see here: http://www.bastosviegas.com/bastos_pt/e ... r_serv.php) with a javascript to enable people to add row on a table.

Here's the javascript:

Code: Select all

<script type="text/javascript">
function addRowToTable()
{
  var tbl = document.getElementById('tblAddress');
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow;
//  var iteration = lastRow + 1;
  var row = tbl.insertRow(lastRow);
 
  //  cell 0
  var cell0 = row.insertCell(0);
  var el = document.createElement('input');
  el.type = 'text';
  el.NAME = 'cod[]';
  el.id = 'cod[]';
  el.size = 14;
  cell0.appendChild(el);
 
  //cell 1
  var cell1 = row.insertCell(1);
  var el = document.createElement('input');
  el.type = 'text';
  el.NAME = 'descri[]';
  el.id = 'descri[]';
  el.size = 48;
  cell1.appendChild(el);
 
  //cell 2
  var cell2 = row.insertCell(2);
  var el = document.createElement('input');
  el.type = 'text';
  el.NAME = 'qty[]';
  el.id = 'qty[]';
  el.size = 14;
  cell2.appendChild(el);
 
}
</script>
The rest of the html is pretty straight forward. The ID's given to the 3 cells I'm having trouble with are cod[] , descri[] , qty[].

This brings us to my problem, which i believe is in the php file which is being used by the form.
The purpose of this is to email the form. I can get it to work with simple input field IDs (unique), but it's not passing the array of values for my cod[] , descri[] , qty[]. I suppose this is a code flaw on my part (I'm not a programmer, although I like to develop this kind of stuff), so here's the code:

(i've taken out some parts that don't matter for this case)

Code: Select all

<?php 
 
$to = "email@address.com"; 
 
$fax = $_POST['fax'];
$email = $_POST['email'];
$arrCod = explode("\n", $_POST["cod"]);
$arrDescri = explode("\n", $_POST["descri"]);
$arrQty = explode("\n", $_POST["qty"]);
 
 
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "Site encomenda";
 
$EmailBody = "  Fax: $fax\n
        Email: $email\n
        ";
 
 
foreach ( $arrCod as $codigos ) {
    $EmailBody .= " Codigo: $codigos\n";
} 
 
foreach ( $arrDescri as $descricao ) {
    $EmailBody .= " Descricao: $descricao\n";
} 
 
foreach ( $arrQty as $quantidade ) {
    $EmailBody .= " Quantidade: $quantidade\n";
} 
 
// Validation stuff
// mail() function
 
exit;
?>
After all this, the email I get is this:

Codigo: Array
Descricao: Array
Quantidade: Array

How can I get this to work?

Thank you very much!

Re: Array on Form - Passing it to PHP

Posted: Tue Mar 09, 2010 5:26 am
by joppe
$arrCod = Array();
$arrCod = explode("\n", $_POST["cod"]);

?

Re: Array on Form - Passing it to PHP

Posted: Tue Mar 09, 2010 5:28 am
by s.dot
explode() returns an array

You could do..

Code: Select all

implode("\n", explode($_POST['cod']))

Re: Array on Form - Passing it to PHP

Posted: Tue Mar 09, 2010 5:40 am
by Xctazy
Thank you for replying.

s.dot, i tried your solution, it fails to retrieve anything on the email it sends. While it retrieved "Array" before, now it just doesnt show up.

Is there anything i must change to acomodate that change?

Re: Array on Form - Passing it to PHP

Posted: Tue Mar 09, 2010 5:55 am
by s.dot
Whoopsie, sorry :P

Code: Select all

implode("\n", explode("\n", $_POST['cod']));