gather and use form elements when they are arrays (from simple order form page)

Hi

I have a very basic form which collects the users details, then lets them add 1 or more elements (requests for cards), this all works fine (code attached) but I cannot get the values back to then calculate costs and display as when I use:

<?php
        foreach ($_POST as $var => $val){        
                   echo $var . ' : ' . $val . '<br>';
        }        
        ?>

I just get the name and array

need to be able to assign say card(1) value1, value2, value3 etc

Neil
FORM
----
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
script language="javascript">
function add(){
var html = document.getElementById('test').innerHTML;
document.getElementById('test').innerHTML=html+'
<table>
  <tr>
    <td valign="top"><input name="date[]" type="text" size="10" /></td>
    <td valign="top"><input name="occasion[]" type="text" size="10" /></td>
    <td valign="top"><input name="to[]" type="text" size="10" /></td>
    <td valign="top"><input name="from[]" type="text" size="10" /></td>
    <td valign="top"><input name="interest[]" type="text" size="10" /></td>
    <td valign="top"><input name="stamped[]" type="text" size="2"/></td>
  </tr>
</table>
';
}
</script>
</head>
<body>
<form action="order-confirm.php" method="post">
  <table>
    <tr>
      <td>Name :</td>
      <td><input name="Name" id="Name" type="text" /></td>
    </tr>
    <tr>
      <td>Address :</td>
      <td><input name="Address" type="text" id="Address" /></td>
    </tr>
    <tr>
      <td>Email :</td>
      <td><input name="Email" type="text" id="Email" /></td>
    </tr>
    <tr>
      <td>Phone :</td>
      <td><input name="Phone" type="text" id="Phone" /></td>
    </tr>
  </table>
  <br />
  <!-- Dynamic element , the customer should be able to add more of these if required -->
  <table>
    <tr>
      <td valign="top">Date </td>
      <td valign="top">Occasion :</td>
      <td valign="top">To :</td>
      <td valign="top">From :</td>
      <td valign="top">Interest :</td>
      <td valign="top">Stamped :</td>
    </tr>
    <tr>
      <td valign="top"><input name="date[]" type="text" size="10" /></td>
      <td valign="top"><input name="occasion[]" type="text" size="10" /></td>
      <td valign="top"><input name="to[]" type="text" size="10" /></td>
      <td valign="top"><input name="from[]" type="text" size="10" /></td>
      <td valign="top"><input name="interest[]" type="text" size="10" /></td>
      <td valign="top"><input name="stamped[]" type="text"  size="2"/></td>
    </tr>
  </table>
  <div id='test'></div>
  <!-- End Dynamic element -->
  <br />
  <input type="button" value="Add another card" onclick="return add();"/>
  &nbsp;|&nbsp;
  <input name="Place Order" type="submit" value="I'm ready to place me order" />
</form>
</body>
</html>
 
 
 
 
 
 
CONFIRM PAGE
------------
<?php 
        foreach ($_POST as $var => $val){        
       		echo $var . ' : ' . $val . '<br>';
        }        
        ?>

Open in new window

LVL 3
Neil ThompsonSenior Systems DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RoonaanCommented:
Hello NeilT,

Can you try and see what print_r($_POST) provides you with?

It might that you get an array on $_POST['from'] (and other fields) that you need to iterate through

Regards,

Roonaan
0
dr_dedoCommented:
now you have many "date", "occasion" and so on (as an array)
this means that $_POST['date'] is an array of many dates
you could do something like attached code, but be ware, all variables (dates, occasion and so on, must hace same count, meaning you can't have 3 dates and
 4 occasions, it would screw up
$count = count $_POST['date'];
for ($i = 0; $i<$count ; $i++){
	$date = $_POST['date'][$i];
	$xxx  = $_POST['xxx'][$i];
	etc...
}

Open in new window

0
Neil ThompsonSenior Systems DeveloperAuthor Commented:
Hi Roonaan

I get this:

Array ( [Name] => neil [Address] => test address [Email] => test@mrtest.com [Phone] => 0404 49494949 [date] => Array ( [0] => 23-04-08 [1] => 05-11-08 ) [occasion] => Array ( [0] => birthday [1] => anniversary ) [to] => Array ( [0] => neil [1] => jayne ) [from] => Array ( [0] => steve [1] => neil ) [interest] => Array ( [0] => football [1] => camping ) [stamped] => Array ( [0] => y [1] => y ) [Place_Order] => I\'m ready to place me order )

How can I pull this out though and sort it as I really want to show the user what they have requested, then total up the arrays (each one is £2.00) and give them a cost. The final bit will be wrap it up and send it to PayPal for the money

Here is the form live : http://www.dreamitparties.co.uk/dreamitparties/handmade-cards.htm

Neil
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Neil ThompsonSenior Systems DeveloperAuthor Commented:
edit to url, now simply http://www.dreamitparties.co.uk/
0
dr_dedoCommented:
correction in my code, line one should be
$count = count ( $_POST['date'] ) ;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Neil ThompsonSenior Systems DeveloperAuthor Commented:
Superb, thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.