PDA

View Full Version : Email form values in a repeat?


student101
08-01-2008, 06:22 PM
How do I email form values in a repeat region?
I've looked at the tutorial on sending PHP mail,foreach ($_POST as $key => $value){This can't be used as these values aren't set as $_POST?

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="120" valign="top" nowrap="nowrap"><strong>Code:</strong></td>
<td><?php echo $row_rswish['code']; ?>
<input name="code" type="hidden" id="code" value="<?php echo $row_rswish['code']; ?>" /></td>
</tr>
<tr>
<td width="120" valign="top" nowrap="nowrap"><strong>Description:</strong></td>
<td><?php echo $row_rswish['description']; ?>
<input name="code2" type="hidden" id="code2" value="<?php echo $row_rswish['description']; ?>" /></td>
</tr>
<tr>
<td width="120" valign="top" nowrap="nowrap"><strong>Diamondinfo:</strong></td>
<td><?php echo $row_rswish['diamondinfo']; ?>
<input name="code3" type="hidden" id="code3" value="<?php echo $row_rswish['diamondinfo']; ?>" /></td>
</tr>
<tr>
<td width="120" valign="top" nowrap="nowrap"><strong>Price:</strong></td>
<td><?php echo $row_rswish['price']; ?>
<input name="code4" type="hidden" id="code4" value="<?php echo $row_rswish['price']; ?>" /></td>
</tr>
<tr>
<td colspan="2" valign="top" nowrap="nowrap"><hr></td>
</tr>
<tr>
<td valign="top" nowrap="nowrap">&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>

There are always going to be multiple values to send in one email.

Cheers

student101
08-01-2008, 06:44 PM
Fixed this but the text is on top of eachother, anyway to add more line breaks or the <hr> (horizional rule)?

foreach ($_POST as $key => $value){
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}

student101
08-01-2008, 10:57 PM
Any way to remove these below?

code:SRI-6
description: square hand
diamondinfo: 0.1ct 3mm round brilliant cut
price: 10
email: email[at]example[.]com
submit: Email
sendForm: mform
code:SRI-2description: square
diamondinfo: 0.2ct 4mm
price: 10
email: email[at]example[.]com
submit: Email
sendForm: mform

Desired result:

code:SRI-6
description: square hand
diamondinfo: 0.1ct 3mm round brilliant cut
price: 10
<hr separator>-------------------
code:SRI-2description: square
diamondinfo: 0.2ct 4mm
price: 10
<hr separator>-------------------

student101
08-02-2008, 12:20 AM
Have tried many tests to get this to work; NO LUCK!

// *** Mail as Text ***
if (isset($_POST["sendForm"]) && $_POST["sendForm"] == "mform") {
// Subject of email sent to you.
$subject = 'My Wishlist';
// Your email address. This is where the form information will be sent.
$emailadd = $_POST['email'];
// --------------------------Do not edit below this line---------------------
$text = "My Wishlist:\n\n";

//START - may be here, not sure?
do {
//
$message = "Code: $code\n Description: $description\n info: $info\n Price: $price\n";
$extra = "From: $emailadd\r\nReply-To: $emailadd\r\n";
mail ($emailadd, $subject, $message, $extra);
//
} while ($row_rswish = mysql_fetch_assoc($rswish));
//END - may be here, not sure?

echo '<META HTTP-EQUIV=Refresh CONTENT="0;">';
}

A bit of help to see the light?

student101
08-02-2008, 09:25 AM
Fixed the format problem - SORT OF :(
How to get a horizontal rule or a line after each repeat?

// *** Mail form as Text format ***
if (isset($_POST["email"])) {
// Subject of email sent to you.
$subject = 'My Wishlist';
// Your email address. This is where the form information will be sent.
$emailadd = $_POST['email'];
// Where to redirect after form is processed.
//$url = '';
// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '0';
$text = "My Wishlist:\n\n";
$space = ' ';
$line = '';
foreach ($_POST as $key => $value){
if ($req == '1'){
if ($value == ''){
echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20){
echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++){
$space .= ' ';}
# Fields to Avoid
$ex=array();
$ex[]="submit"; # Where submit is the name of the submit button.
$ex[]="email"; # Where email is the name of the email textfield.
# iterate the $_POST array
foreach ($_POST as $key => $value) {
if (!in_array($key, $ex)) {
$conc = "{$key}:$space{$value}$line"."\n";
$text .= $conc;
$space = ' ';
}
}
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
}

student101
08-02-2008, 10:01 AM
Any help on formatting with a <hr> or --------- after each item?
I've attempted adding a form value like this;
<input name="---" type="hidden" value="----------" />
Which outputs like this;
code:SRI-8
description: Diamond suspended in resin on a hand engraved square band
diamondinfo: 0.1ct 3mm round brilliant cut
price: 10
---: ----------

davidj
08-02-2008, 10:11 AM
hehehehe

this thread is like student's blog

like your helping yourself

student101
08-02-2008, 10:13 AM
LOL It seems like it.

davidj
08-02-2008, 10:15 AM
cant you just add a var and insert it between each item

student101
08-02-2008, 10:20 AM
$hrline .='<hr>';
//then add it here
$conc = "{$key}:$space{$value}$line$hrline"."\n";

Then I get this;

code: SRI-8<hr>
description: Diamond suspended in resin on a hand engraved square band<hr>
diamondinfo: 0.1ct 3mm round brilliant cut<hr>
price: 10<hr>
---: ----------<hr>

The objective is here;

code: SRI-8
description: Diamond suspended in resin on a hand engraved square band
diamondinfo: 0.1ct 3mm round brilliant cut
price: 10
------------- // this line can be this or the <hr>


The <hr> is HTML, this message is text - so no <hr> will have to be the -------------

student101
08-02-2008, 12:54 PM
Had to change this up a bit, was getting 5 or more even if there was only one.
Where do I put the loop? also which loop? DO while or While do?

// *** Mail form as Text format ***
if (isset($_POST["email"])) {
$db = mysql_connect("localhost", "user", "pass");
mysql_select_db("db1",$db);
$name = trim($HTTP_GET_VARS['name']);
$query=("SELECT * FROM table WHERE name ='$name'");
$result= mysql_query($query);
$num=mysql_num_rows($result);
if ($num > 0) { //Records match
//trim($_POST['email'])
//define the receiver of the email
$to = trim($_POST['email']);
//define the subject of the email
$subject = 'My Wishlist';
//define the message to be sent. Each line should be separated with \n
$message = "My Wishlist"."\n\n";
$message .= "Code: ". $code."\n";
$message .= "Description: ". $description."\n";
$message .= "Diamond info: ". $diamondinfo."\n";
$message .= "Price: ". $price."\n";
$message .= "------------------------";
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: ".$to;
//$headers = "From: $to\r\nReply-To: $to";
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
//echo $mail_sent ? "Mail sent" : "Mail failed";
}else{
//no send
}
}

davidj
08-02-2008, 12:59 PM
hope this helps

you need to know how many lines to loop through before adding the <hr>


$array = array(
"test1",
"test2",
"test3",
"test4",
"test5",
"test6",
"test7"
);

$count = 1; /// int count
$break = 3; /// how many lines before adding a <hr>. Change here to adjust line count
$hr = "<hr>"; /// hr

foreach($array as $values){

echo $values."<br /><br />";

$calc = $count / $break; /// divide two values forcing a floating point. By changing the $break variable value is when the <hr> will be created

if(!is_float($calc)){ /// check for non floats before adding the <hr>.

echo $hr;

}

$count++;

}

student101
08-02-2008, 01:04 PM
That is really confusing.
What is test?

My other method works, except don't know where to put the loop, or which loop.

davidj
08-02-2008, 01:06 PM
test is your array which you loop through

i am using that as an example

davidj
08-02-2008, 01:06 PM
try running it on its own to see if it meets your requirements

davidj
08-02-2008, 01:10 PM
also change the break value to when you want the <hr> to appear

student101
08-02-2008, 01:13 PM
Like this?


$array = array(
//add entire part here
// *** Mail form as Text format ***
if (isset($_POST["email"])) {
//--------------------------Set these paramaters-----------------------
// Subject of email sent to you.
$subject = 'My Wishlist';
// Your email address. This is where the form information will be sent.
$emailadd = $_POST['email'];
// Where to redirect after form is processed.
//$url = '';
// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '0';
// --------------------------Do not edit below this line--------------------------
$text = "My Wishlist:\n\n";
$space = ' ';
$line = '';
foreach ($_POST as $key => $value){
if ($req == '1'){
if ($value == ''){
echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20){
echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++){
$space .= ' ';
}
# Fields to Avoid
$ex=array();
$ex[]="submit"; # Where submit is the name of the submit button.
$ex[]="email"; # Where email is the name of the email textfield.
# iterate the $_POST array
foreach ($_POST as $key => $value) {
if (!in_array($key, $ex)) {
$conc = "{$key}:$space{$value}$line"."\n";
$text .= $conc;
$space = ' ';
}
}
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
//echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
}
//end add entire part
);
$count = 1; /// int count
$break = 3; /// how many lines before adding a <hr>. Change here to adjust line count
$hr = "<hr>"; /// hr
foreach($array as $values){
echo $values."<br /><br />";
$calc = $count / $break; /// divide two values forcing a floating point. By changing the $break variable value is when the <hr> will be created
if(!is_float($calc)){ /// check for non floats before adding the <hr>.
echo $hr;
}
$count++;
}

davidj
08-02-2008, 01:16 PM
i didnt provide a cut n paste solution so that wont work

i provided a snippet which when you run on its own and see how im adding a <hr> at a predetermined line count.

your job is to take my example and learn from it then intergrate it in your code in the correct place

student101
08-02-2008, 01:19 PM
Cool thanks - I didn't understand it.
It still doesn't solve the problem where the data is bein duplicated with the same record even if there is only one.

So I have one record added and click send then I get 5 or more - underneath eachother.

That example works out pretty cool - neat feature.

student101
08-02-2008, 03:12 PM
Fixed;
Thank you!!! davidj

davidj
08-02-2008, 03:13 PM
did my solution help

wasnt sure if i was on the right tracks

student101
08-02-2008, 06:36 PM
Yes, sort of.
I used a method of sorts to loop throught the code like this;
//added a counter
$x = 1;
do {

<input name="code<?php echo $x; ?>" type="hidden" id="code" value="<?php echo $row_rswish['code']; ?>

} while ($row_rswish = mysql_fetch_assoc($rswish));
//increase counter
$x++;

the only issue I have is that it adds the number after each value, ie; code1, code2 and so on.
I am unable to use a blank line counter unfortunately, but it works so what the heck!

Cheers

amanda
08-12-2008, 08:00 AM
hehehehe

this thread is like student's blog

like your helping yourself

student101
08-12-2008, 08:05 AM
hehehehe
this thread is like student's blog
like your helping yourself

I am not one (unlike most) to sit and twiddle my thumbs, if you can't help yourself, who can?

Cheers

Corrosive
08-12-2008, 09:40 AM
I am not one (unlike most) to sit and twiddle my thumbs, if you can't help yourself, who can?

Well said Student101.

It is a bit like a soap opera though. At least you'll have a running record of what you have learnt this way :wink: