Sending Arrays with POST

Hi guys, little bit stuck with a request form I’m trying to make.

The plan is on submit to have it show the items that only had a quantity entered along with that quantity.

Although at the moment I get all three items back on submitting whether or not a quantity has been entered.

It’s a bit of a learning process at the moment so I might be completely barking up the wrong tree with what I’ve already done, any help will be greatly appreciated though. :slight_smile:


<form method="POST">

<table>
<thead>
<tr><th>Description</th><th>Qty</th></tr></thead>

<tr><td>Wheels</td><td><input type="text" name="product[0][qty]" size="5"><input type="hidden" name="product[0][desc]" value="Wheels"></td></tr>
<tr><td>Tyres</td><td><input type="text" name="product[1][qty]" size="5"><input type="hidden" name="product[1][desc]" value="Tyres"></td></tr>
<tr><td>Brakes</td><td><input type="text" name="product[2][qty]" size="5"><input type="hidden" name="product[2][desc]" value="Brakes"></td></tr>

<tr><td colspan="2"><input type="submit" value="Submit" name="submit"></td></tr>
</tr>
</table>

</form>

<?php
if ( isset( $_POST['product'] ) )

    echo '<table>';
    foreach ( $_POST['product'] as $prod )
    {
        echo '<tr>';
        echo '  <td>', $prod['qty'], '</td>';
        echo '  <td>', $prod['desc'], '</td>';
        echo '</tr>';
    }
    echo '</table>';
?>

What you can do is check if there’s anything in the quantity (see my extra “if”):

  echo '<table>';
    foreach ( $_POST['product'] as $prod )
    {
        if ($prod['qty']){ 
		echo '<tr>';
       echo '  <td>', $prod['qty'], '</td>';
        echo '  <td>', $prod['desc'], '</td>';
        echo '</tr>';}
    }
    echo '</table>';

Hope this helps…

-Eric

Hi Eric, that worked perfectly, thanks for your help.

Right … I’m sure someone’s going to fall of their chair laughing for my next lump of newbie code I’m about to post but what I’m trying to do next is pass all of that info to an email reply.

What I’ve done now is created two files from the one I started with above, one for the form and another to process the email info as you would.

This is how far I’ve got so far with the email processing script:


<?php
if(isset($_POST['submit'])) 

{

$to = "aaron@myemail.com";
$subject = "Parts Offer";
$name_field = $_POST['name'];
$email_field = $_POST['email'];


echo "Thank you very much for your order";

function orderTable(){
if ( isset( $_POST['product'] ) )

    echo '<table>';
	echo '<br /><br />You have requested the following';
    foreach ( $_POST['product'] as $prod )
    {
		if ($prod['qty']){
        echo '<tr>';
        echo '  <td>', $prod['qty'], '</td>';
        echo '  <td>', $prod['desc'], '</td>';
        echo '</tr>';
		}
    }
    echo '</table><br /><br />';
		
}

$order = orderTable();

$body  =  "From: $name_field\
E-Mail: $email_field\
\
I'd like to order the following \
\
$order";

mail($to, $subject, $body);

}

else {
echo "Your order request didn't process, please try again!";
}

?>


The email sends fine with all the relevant info apart from what’s called up in the orderTable function, which I know works because I’ve called it up elsewhere to test it was actually working.

Think I’ve got as far as my limited skills will let me but can’t help thinking I’m quite close because I’ve had all the component parts working but knowing me I’m way off lol.

Aaron,

First off…I was in your shoes just 3 months ago…this forum is FANTASTIC with tons of help (I still get a bunch)…so don’t worry about too many questions. :slight_smile:

One of the problems is that the way that your function is set up; you’re just printing something, it’s not creating a variable, for example. Another problem, then would be that once you create a variable, you’d have to “return” it outside of the function.
As an example:
<?php
function Multiply($x,$y)
{
product=$x*$y;
return $product; //notice the return statement!
}

echo "3*16 = " . Multiply(3,16);
?>

So my “big picture suggestion” would be to create a string with all of the stuff that you want to print (I’m not sure how much you know about concatenating strings…), return it, and then use it.

Let me know if this helps… :slight_smile:

Does that give you something to go on?

Must admit, don’t know much about concatenating strings, got a rough idea though and that’s pointed me in the right direction cheers.

Only started to make a serious effort to learn PHP just this week by working on a couple of little projects.

Think it was a pretty good learning process though trying to figure out what I’d done wrong on a project of my own and then working out how to fix it, although when I get the time I’ve promised myself I’ll work though a book or some videos as I can’t help feel I’ve jumped in the middle somewhere and should really get the basics under my belt first.

A friendly forum like this place certainly helps though :slight_smile:

OK…

Basically, you can use a period to join strings:

$first = "hello";
$second = "bye";
$join = $first . $second;
$join_with_space = $first . ' ' . $second;
echo $join;
echo $join_with_space;

As for books, I gobbled down Training from the Source as well as PHP 6 and Mysql 5 (Ullman), that and LOTS of questions to this forum got me on my way.