Array on Form - Passing it to PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Xctazy
Forum Newbie
Posts: 2
Joined: Tue Mar 09, 2010 4:57 am

Array on Form - Passing it to PHP

Post 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!
joppe
Forum Newbie
Posts: 7
Joined: Tue Mar 09, 2010 3:18 am

Re: Array on Form - Passing it to PHP

Post by joppe »

$arrCod = Array();
$arrCod = explode("\n", $_POST["cod"]);

?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Array on Form - Passing it to PHP

Post by s.dot »

explode() returns an array

You could do..

Code: Select all

implode("\n", explode($_POST['cod']))
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Xctazy
Forum Newbie
Posts: 2
Joined: Tue Mar 09, 2010 4:57 am

Re: Array on Form - Passing it to PHP

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Array on Form - Passing it to PHP

Post by s.dot »

Whoopsie, sorry :P

Code: Select all

implode("\n", explode("\n", $_POST['cod']));
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply