Jump to content

Form, posting multiple fields


Mutley

Recommended Posts

I have a product list page, users can check the product then go to an e-mail form, that lists the products they checked.

 

On this, they can then chose a quantity for every product, how do I make it so when they send the e-mail, it gets the quantity for each product (using PHP mail, to send)?

 

The field would be:

<input type="text" size="4" maxlength="4" value="1" name="quantity">

 

But that wouldn't work for multiple ones? Would I use a "foreach" statement, somehow?

 

Link to comment
https://forums.phpfreaks.com/topic/48297-form-posting-multiple-fields/
Share on other sites

with forms if your using the same field more than once use this

<input type="text" size="4" maxlength="4" value="1" name="quantity[]">

then

<?php
foreach ($_POST['quantity'] as $q)
{
echo "$q<br>";
}
?>

 

hope that helps

 

 

EDIT: Ahhh Frost!!! beat me

How do I do that using mail though?

 

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

$name = $_POST['name'];
$telephone = $_POST['telephone'];
$address = $_POST['address'];
$postcode = $_POST['postcode'];
$email = $_POST['email'];
$notes = $_POST['notes'];

$send_email = "[email protected]";
$send_subject = "Site E-Mail Order";
$send_message = "
E-Mail order/enquiry from website:

Name: $name
Telephone: $telephone
E-Mail: $email
--------------------
Address:
$address
$postcode
--------------------
Order notes:
$notes
--------------------

";
$send_header = "From: [email protected]\n";
mail($send_email, $send_subject, $send_message, $send_header);
?>

 

That's how I'm doing it, I can't insert PHP code in there, can I?

 

why won't this work ?

<?php
foreach ($_POST['quantity'] as $k => $q)
{
echo "{$_POST['quantity'][$k]} | $q<br>";
}
?>

 

 

<input type="text" size="10" maxlength="10" value="mice" name="product[]">

<input type="text" size="4" maxlength="4" value="1" name="quantity[]">

 

if(isset($_POST['submitemail'])) {

$name = $_POST['name'];
$telephone = $_POST['telephone'];
$address = $_POST['address'];
$postcode = $_POST['postcode'];
$email = $_POST['email'];
$notes = $_POST['notes'];

if($name == NULL || $telephone == NULL || $address == NULL || $postcode == NULL || $email == NULL) {
include("includes/header.php");
	echo "Please fill in <b>all</b> the fields! <a href=\"javascript:history.go(-1)\">Click here to go back</a>.";
include("includes/footer.php");
} else {

$send_email = "[email protected]";
$send_subject = "Site E-Mail Order";

foreach ($_POST['quantity'] as $k => $q)
{

$send_message = "
E-Mail order/enquiry from website:

Name: $name
Telephone: $telephone
E-Mail: $email
--------------------
Address:
$address
$postcode
--------------------
Order notes:
$notes
--------------------
{$_POST['quantity'][$k]} | $q<br>
";

}

$send_header = "From: [email protected]\n";
mail($send_email, $send_subject, $send_message, $send_header);
?>
			<script type="text/javascript">
			window.location = "index.php?msg=Your e-mail product enquiry has been sent"
			</script>
<?
}

 

HTML isn't allowed in the e-mail, so <br> won't work, is above right though, because when I try it I just get the last quantity not all of them?

<form enctype="multipart/form-data" action="mailorder.php" method="post" />
<table class="mail" width="400" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>Name:</td>
    <td><input type="text" size="20" maxlength="35" name="name"></td>
  </tr>
  <tr>
    <td>Telephone:</td>
    <td><input type="text" size="15" maxlength="15" name="telephone"></td>
  </tr>
  <tr>
    <td>Address:</td>
    <td><textarea name="address" rows="3" cols="40"></textarea></td>
  </tr>
  <tr>
    <td>Postcode:</td>
    <td><input type="text" size="7" maxlength="7" name="postcode"></td>
  </tr>
  <tr>
    <td>E-Mail:</td>
    <td><input type="text" size="20" maxlength="35" name="email"></td>
  </tr>
  <tr>
    <td>Order Notes:</td>
    <td><textarea name="notes" rows="3" cols="40"></textarea></td>
  </tr>
</table>
<br /><br />

Your selected items:
<br /><br />
<table class="mail" width="600" border="0" cellspacing="0" cellpadding="0">
<td>JJM #:</td>
<td>Product #:</td>
<td>Category:</td>
<td>Description:</td>
<td>Weight*:</td>
<?
if(!empty($_COOKIE['id']) && !empty($_COOKIE['pass'])) {
$userid = htmlspecialchars($_COOKIE['id'], ENT_QUOTES);
$pass = htmlspecialchars($_COOKIE['pass'], ENT_QUOTES);

$check = mysql_query("SELECT * FROM users WHERE password = '$pass' AND id = '$userid'") or die(mysql_error());
  
if(mysql_num_rows($check) <> 1)	{
echo "";
} else {
/////////////// ### PRICE ###
?>
<td>Price*:</td>
<?
////////////////////// End login ### PRICE ###
}
} else {
echo "";
}
?>
<td>Quantity:</td>
<?
foreach ($_POST['order'] as $id => $x) {

$sql = "SELECT id, category, name, cpn, pn, description, descriptionf, descriptiong, weight, price, ofeatured, onew, osale FROM products WHERE id = ".$id." LIMIT 1";
$result = mysql_query($sql) or die("SQL Query: {wordwrap($sql)}<br />Error because:  " . mysql_error());

if(mysql_num_rows($result)!=0) {

while(list($id, $category, $name, $cpn, $pn, $description, $descriptionf, $descriptiong, $weight, $price, $ofeatured, $onew, $osale) = mysql_fetch_row($result)) {
?>
<!-- START ORDER FORM -->

<tr>
<td><?=$cpn?> </td>
<td><?=$pn?> </td>
<td><?=$category?> </td>
<td><?=$description?> </td>
<td>
<?=$weight?> </td>
<?
if(!empty($_COOKIE['id']) && !empty($_COOKIE['pass'])) {
$userid = htmlspecialchars($_COOKIE['id'], ENT_QUOTES);
$pass = htmlspecialchars($_COOKIE['pass'], ENT_QUOTES);

$check = mysql_query("SELECT * FROM users WHERE password = '$pass' AND id = '$userid'") or die(mysql_error());
  
if(mysql_num_rows($check) <> 1)	{
echo "";
} else {
/////////////// ### PRICE ###
?>
<td>€<?=$price?> </td>
<?
////////////////////// End login ### PRICE ###
}
} else {
echo "";
}
?>
<td>
	<input type="hidden" name="price[]" value="<?=$price?>" />
	<input type="hidden" name="cpn[]" value="<?=$cpn?>" />
	<input type="hidden" name="pn[]" value="<?=$pn?>" />
	<input type="text" size="4" maxlength="4" value="1" name="quantity[]">
</td>
</tr>

<!-- END ORDER FORM -->
<?
}
}
// Split closing between results and selected
}
?>
</table>
* <i>Per unit.</i><br /><br />
<input type="submit" name="submitemail" value="Submit E-Mail" />

 

Thanks,

 

I have other hidden fields in there, when I know how to get the quantity working I'll do the same to the $cpn, $pn you see in the hidden fields.

i have created my own script which does basically what i think your after

 

if you paste it into its own file and test it you might be able to use parts, as a note i used an array instead of a SQL statement

 

<?php
if( isset($_POST['submit']) )
{
$body = "";
foreach($_POST['Product'] as $k => $p)
{
	$body .= "$p | {$_POST['quantity'][$k]}\n";
}

echo nl2br($body); // of course don't use nl2br on the mail
}else{
?>
<form method="post">
<?php
$item = array("fish", "chips", "Beans", "i missed lunch");
$q = array("1", "65", "1002", "lol");
foreach($item as $k => $p)
{
?>
	<input type="text"  value="<?php echo $p ?>" name="Product[]"><br />
	<input type="text" size="4" maxlength="4" value="<?php echo $q[$k] ?>" name="quantity[]"><br />
	<br />
<?php
}
?>
<input name="submit" type="submit" value="SendMail" />
</form>
<?php
}
?>

<?php
foreach($_POST['Product'] as $k => $p)
?>

 

$_POST['Product'] = array

$k = Key

$p = value

 

is if

$_POST['Product'] = array(0 => "123", 1 => "234", 2 => "345")

 

then on the first loop

$k = 0

$p = "123"

 

2nd loop

$k=1

$p= "234"

 

etc

So how do you join the quantity and product fields?

 

If I wanted it like this:

 

	foreach($_POST['quantity'] as $k => $p)
{
	$body .= "$cpn | $pn | $price | {$_POST['quantity'][$k]}\n";
}

echo $body;

 

The part I can't do is the foreach on the form as that data is already getting looped by a SQL query.

I tried:

 

	foreach($_POST['cpn'] as $cpn)
{
foreach($_POST['pn'] as $pn)
{
foreach($_POST['price'] as $price)
{

foreach($_POST['quantity'] as $quantity => $cpn)
{
	$body .= "$cpn | $pn | $price | {$_POST['quantity'][$cpn]}\n";
}
}
}
}

 

But it didn't work, just got spammed with loads of "$cpn" field.

  • 2 weeks later...

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.