Page 1 of 2
Merge two variables
Posted: Sat Jan 03, 2009 6:12 pm
by sk8bft
I don't know much about php, but I have put together a mail form with the help of some tutorials. This is the code:
Code: Select all
<?php
if(isset($_POST['submit'])) {
$to = "test@test.com";
$subject = "Test Subject";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
foreach($_POST['check'] as $value) {
$check_msg .= "$value\n";
}
foreach($_POST['txt'] as $value) {
$txt_msg .= "$value\n";
}
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message\n $check_msg $txt_msg";
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "blarg!";
}
?>
Now, I need $check_msg and $txt_msg to be shown in the same line in the email. So if $check_msg is 10 and $txt_msg is Pancakes, it will come up in the email as "10 Pancakes". Not:
10
Pancakes
(with a line break)
I realise this might be very basic, but I have tried for many hours to do it without success. Any help will be appreciated.
Re: Merge two variables
Posted: Sat Jan 03, 2009 6:20 pm
by requinix
\n represents a line break. You've got it twice in there, only one of them you want.
Code: Select all
foreach($_POST['check'] as $value) {
$check_msg .= "$value\n";
}
foreach($_POST['txt'] as $value) {
$txt_msg .= "$value\n";
}
Re: Merge two variables
Posted: Sat Jan 03, 2009 6:24 pm
by sk8bft
Thank you so much!
Re: Merge two variables
Posted: Sat Jan 03, 2009 6:35 pm
by kaszu
I think problem is that each $_POST['check'] value is appended with newline, so at the end it is "10\n Pancakes\n", which I thought was unwanted.
If it's so, then instead of foreach loops use
Code: Select all
$check_msg = implode($_POST['check'], "\n"); //Now all values except last will be appended with new line
$txt_msg = implode($_POST['txt'], "\n"); //same
Re: Merge two variables
Posted: Sat Jan 03, 2009 6:47 pm
by sk8bft
kaszu: yes, I think you got me right. However, this solution comes up like this when there are more than one value:
10
5 Pancakes
Waffles
Where it should be:
10 Pancakes
5 Waffles
Any idea why? What I need is just a list of the values, I tried HTML tables, but gave it up as I couldn't get it through hotmail, gmail etc.
Code: Select all
$check_msg = implode($_POST['check'], "\n");
$txt_msg = implode($_POST['txt'], "\n");
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message\n $txt_msg $check_msg";
Re: Merge two variables
Posted: Sat Jan 03, 2009 7:06 pm
by requinix
Ah, so you really
are "merging" variables together.
You need two loops running at the same time.
Code: Select all
for ($msg = ""; list(, $c) = each($_POST["check"]) and list(, $t) = each($_POST["txt"]); )
$msg .= "$c $t\n";
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message\n $msg";
Re: Merge two variables
Posted: Sat Jan 03, 2009 7:13 pm
by sk8bft
Yes! That's exactly what I was looking for. I have tested it with three checkboxes/textfields now and it works just the way I need it to. Didn't understand all of the coding, though, but I will read through the php manual. Thank you!
Re: Merge two variables
Posted: Sat Jan 03, 2009 7:39 pm
by sk8bft
Is it possible to expand this to three values? For some of the choices, but not all, I have a dropdown menu.
Ex:
10 Pancakes w/Chocolate
5 Waffles
Where Chocolate is one of the options in the dropdown menu. I tried this:
for ($msg = ""; list(, $c) = each($_POST["txt"]) and list(, $t) = each($_POST["check"]) and list(, $h) = each($_POST["drop"]); )
$msg .= "$c $t $h\n";
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message\n $msg";
But only the first line is visible in the email (10 Pancakes w/Chocolate).
Re: Merge two variables
Posted: Sat Jan 03, 2009 7:52 pm
by requinix
There was one of each of check and txt so it worked with two variables; your third won't always be there. If there's only one then the loop will only go through once - then, since it ran out of drop it will stop.
How does drop work with check? Meaning, how do you know that "w/Chocolate" belongs to Pancakes and not Waffles?
(PS: I'll add comments on the next version of the code.)
Re: Merge two variables
Posted: Sat Jan 03, 2009 8:04 pm
by sk8bft
How does drop work with check? Meaning, how do you know that "w/Chocolate" belongs to Pancakes and not Waffles?
I attached a picture, is that what you meant? It's a non graphical version, but you can imagine there will be up to 40 different dishes, where you choose what kind of meat you want for some dishes etc (or if you want chocolate or ice cream on the pancakes - nevermind if someone want both, in the real website the choices will be chicken/beef/scampi etc)
Re: Merge two variables
Posted: Sat Jan 03, 2009 8:08 pm
by sk8bft
However I guess it's possible to avoid this problem by adding radio button groups (or dropdowns) to each dish (even those with only one choice of meat), but it wouldn't be as smooth
[edit]
There might however be problems if someone wants to order 5 of the same dish, but with different meat (2 w/chicken and 3 w/beef). Ungh, this gets a little more complicated than I first expected
Re: Merge two variables
Posted: Sat Jan 03, 2009 8:44 pm
by requinix
I meant how can the PHP know what goes where, but if it's getting complicated already...
Instead of grouping stuff by what it is (checkbox, name, extras) group by item. So $_POST["food"][0] would be an array of check=>1, item=>Pancakes, amount=>3, extra=>Chocolate, [1] would be check=>1, item=>Waffles, amount=>2.
Then the code to display that again is easy:
Code: Select all
$msg = "";
foreach ($_POST["food"] as $food) {
if ($food["check"] && $food["amount"]) { // checked and at least one
$msg .= $food["amount"] . " "; // number
$msg .= $food["item"]; if ($food["amount"] > 1) $msg .= "s"; // name
if ($food["extra"]) $msg .= " with " . $food["extra"]; // extra
$msg .= "\n";
}
}
if ($msg) {
echo "You wanted:\n", $msg;
} else {
echo "You didn't select any food.\n";
}
Re: Merge two variables
Posted: Sun Jan 04, 2009 12:07 am
by sk8bft
I've been reading about arrays since you posted but I can't understand how to group this correctly by item. I found several tutorials on how to do this with one dropdown, but I can't find anything about something that looks close to this. Should the checkbox, text field and dropdown be grouped like this:
Code: Select all
<input type="checkbox" name="food[check][0]" id="check1" onClick="setQuantiy('amount1', this.id);"/>
Pancakes
<input name="food[amount][0]" type="text" id="amount1" size="1" onChange="setChecked(this.id, 'check1');"/>
<select name="food[extra][0]" id="drop1">
<option>Choose</option>
<option>Chocolate</option>
<option>Ice cream</option>
</select><br>
<input type="checkbox" name="food[check][1]" id="check2" onClick="setQuantiy('amount2', this.id);"/>
Waffles
<input name="food[amount][1]" type="text" id="amount2" size="1" onChange="setChecked(this.id, 'check2');"/>
? All examples I can find contains only one checkbox, text field and dropdown.
Re: Merge two variables
Posted: Sun Jan 04, 2009 5:10 am
by requinix
That way you're still grouping by check/amount/extra/etc. Group by the item itself - that is to say, the
number.
Code: Select all
<input type="checkbox" name="food[0][check]" id="check1" onClick="setQuantiy('amount1', this.id);"/>
Pancakes
<input name="food[0][amount]" type="text" id="amount1" size="1" onChange="setChecked(this.id, 'check1');"/>
<select name="food[0][extra]" id="drop1">
<option>Choose</option>
<option>Chocolate</option>
<option>Ice cream</option>
</select><br>
<input type="checkbox" name="food[1][check]" id="check2" onClick="setQuantiy('amount2', this.id);"/>
Waffles
<input name="food[1][amount]" type="text" id="amount2" size="1" onChange="setChecked(this.id, 'check2');"/>
Re: Merge two variables
Posted: Sun Jan 04, 2009 7:12 am
by sk8bft
Yes, fantastic! I had to do a small change in the php code also as I wanted the item names as from the checkbox values. So i replaced ["item"] with ["check"] and now it seems to be working fine!