Jump to content

Array on Form - Passing it to PHP


Kyrus

Recommended Posts

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/encomendar_serv.php) with a javascript to enable people to add row on a table.

 

Here's the javascript:

 

<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)

 

<?php 

$to = "[email protected]"; 

$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!

Link to comment
https://forums.phpfreaks.com/topic/194611-array-on-form-passing-it-to-php/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.