Adding input field to PHP emailer only if it exists, how?
I have a form with inputs for 'name' and 'email'. And another for 'data-1'. The user can click on add button and jQuery will dynamically add a input for 'data-2', 'data-3' etc..as needed.
The form is posted to a PHP emailer script which validates fields and places data into a template for mailing.
How can i add inputs 'data-2', 'data-3' etc.. if they are created? And if they are not how can i avoid gaps in my email template?
(is there a way to write it so if the post is received add this and if not do nothing?)
Here is an example of the code i am using:
$name = $_POST['name'];
$email = $_POST['email'];
$data-1 = $_POST['data-1'];
(do i need to add: $data-2 = $_POST['data-2'] and $data-3....up to a set value of say 10?)
$e_body = "You were contacted by $name today.\r\n\n";
$e_data = "Data Set 1: $data-1.\r\n\n";
Here is where开发者_如何学Python i would like to show "Data Set 2/3/4....etc" if they exist
$e_reply = "You can contact $name via email, $email";
$msg = $e_body . $e_data . $e_reply;
if(mail($address, $e_subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n")) {
I hope that is clear and thank you for any help or guidance
thom
You should be using input arrays for this purpose.
In your HTML, set the name of the form element to conform to this naming scheme: data[]
.
Then, when the form is submitted you can simply loop through this array and add fields to the email within the loop:
$name = $_POST['name'];
$email = $_POST['email'];
$data = $_POST['data'];
$e_data = '';
foreach($data as $i => $d) {
if((int) $i != $i) continue; //thanks Alex
$e_data .= "Data Set {$i}: " . $d . "\r\n\n";
}
//...
On the client side, your code should be something like this:
<input type="hidden" name="data[]"/>
As a general point form processing is trick and has many traps for the unwary. It's worth having a look at this post on best practice form processing
The crux of your problem is that you do not know how many "data" values you will get in your $_POST array. The answer is simply to iterate over the $_POST array to find your data values.
$rawData = array();
foreach ($_POST as $index => $value) {
// Test for a "data-n" index
if ((preg_match('#^data-\d+#i', $index, $matches))) {
$rawData[] = $value;
}
}
The above code will copy all the $_POST values with keys of the form 'data-0', 'data-1', etc. You can then filter and validate these values.
All we do is iterate over $_POST to get each key and value. We then test the key using preg_match to see if it starts with the string 'data-' and is followed by one or more digits. If so, we add it to our raw (unfiltered, non validated) data.
In this example, we could replace the preg_match with the strpos function -
if ( strpos ($index, 'data-') === 0) {
The use of the preg_match gives us more flexibility. For example, if you wanted to capture the numeric portion of your 'data-n' keys - 'data-23', 'data-999', 'data-5', etc. Then change the if statement to
if ((preg_match('#^data-(\d+)#i', $index, $matches))) {
$rawData[$matches[1]] = $value;
}
The variable $matches is an array that captures the results of the search. The complete matching string is is $matches[0]. However, we have enclosed the digit matching pattern in parenthesis and hence the captured digits are placed into $matches1 which we then use to key the $rawData array. In this case $rawData would have the keys = 23, 999 and 5.
Use something like this:
if(isset($_POST['data-1']))
{
// $_POST['data-1'] exists, include it.
}
else
{
// $_POST['data-1'] doesn't exists, don't include it.
}
精彩评论