I am new to PHP. This is my code from our mailing.php. When a user submits a request, there are 5-7 select-able fields and 20-25 fields that end up not being selected. The output lists all fields and values regardless of whether they are empty or have been selected. I understand I need to use either unset or array_filter, but cannot figure out how and where I need to insert into code.
if($_POST && count($_POST)) {
$body = '';
foreach($_POST as $key=>$value)
$body .= $key . ": " . $value . "\r\n";
mail("email#email.com", "Email Received at email#email.com", $body);
You can try this
if($_POST && count($_POST)) {
$_POST = array_filter($_POST);
$body = '';
foreach($_POST as $key=>$value)
$body .= $key . ": " . $value . "\r\n";
mail("email#email.com", "Email Received at email#email.com", $body);
OR
if($_POST && count($_POST)) {
$body = '';
foreach($_POST as $key=>$value){
$trim_value = trim($value);
if (!empty($trim_value)){
$body .= $key . ": " . $value . "\r\n";
}
}
mail("email#email.com", "Email Received at email#email.com", $body);
Just before foreach loop you should use this
$_POST = array_filter($_POST);
Another option is to use a conditional inside foreach loop
foreach($_POST as $key=>$value)
if ($value != '' && $value != null)
$body .= $key . ": " . $value . "\r\n";
Related
I have this code in my WordPress plugin, it's supposed to get some data from database and after that send emails to different adresses with a common subject but different email body.
<?php
$pdv_subject = "Confirmation links from" . date('d-m-Y', time());
//
$pdv_message_a = "Salut!\n";
$pdv_email_a = 'user#example.com';
$pdv_headers_a[] = 'Cc: user#example.com';
//
$pdv_message_b = "Ciao!\n";
$pdv_email_b = 'user#example.com';
$pdv_headers_b[] = 'Cc: user#example.com';
//
$pdv_message_p = "Hello!\n";
$pdv_email_p = 'user#example.com';
$pdv_headers_p[] = 'Cc: user#example.com';
//
foreach( $results as $key => $val ){
if(date('d-m-Y', $val['confirmed_at']) === date('d-m-Y', time()) && $val['pco'] === '650'){
$pdv_message_a .= $val['link'] . "\n";
}
//
if(date('d-m-Y', $val['confirmed_at']) === date('d-m-Y', time()) && $val['pco'] === '620'){
$pdv_message_b .= $val['link'] . "\n";
}
//
if(date('d-m-Y', $val['confirmed_at']) === date('d-m-Y', time()) && $val['pco'] === '660' ){
$pdv_message_p .= $val['link'] . "\n";
}
}
In the code I've omitted the wp_mail function, I've done a test and it's working fine. The only problem I have is that the $pdv_message_ that needs to be added inside the if statement will be not added, this will cause that the email will be sent without the links inside the body. I've done a var_dump() and I'm able to see the $val but why the links aren't added to the messages?
Aside from anything, I think I'd lay the code out like this
foreach( $results as $key => $val ){
if(date('d-m-Y', $val['confirmed_at']) === date('d-m-Y', time())) {
switch ($val['pco']) {
case '620':
$pdv_message_b .= $val['link'] . "\n";
break;
case '650':
$pdv_message_a .= $val['link'] . "\n";
break;
case '660':
$pdv_message_p .= $val['link'] . "\n";
break;
}
}
}
(I'm not suggesting this is an answer to your problem, but it looks a lot nicer IMO and saves repeating all those identical if clauses.)
We have a php script that emails form field values when a user submits the form. The form action points to the script below.
We've been asked to configure things to use expressmail explicitly. My question is, would this entail a modification to the script or is this a config setting on the server somewhere?
<?php
if (! $_POST) {
header('HTTP/1.0 405 Method Not Allowed');
exit;
}
$redirectTo = html_entity_decode($_POST['post']);
$body = '<html><body>';
$content = array();
foreach ($_POST as $key => $value) {
if ('-label' !== substr($key, -6)) {
continue;
}
$field = substr($key, 0, strlen($key) - 6);
$content[$field]['value'] = $_POST[$field];
$content[$field]['label'] = $_POST[$key];
}
$body .= '<h1>' . htmlentities($_POST['formName']) . '</h1>';
foreach ($content as $field => $value) {
$data = $value['value'];
$label = $value['label'];
$body .= '<p><b>' . htmlentities($label) . '</b><br />';
if (false === is_array($data) && (null === $data OR "" === trim($data))) {
$body .= 'N/A';
} elseif (is_array($data)) {
$body .= '<ul>';
foreach ($data as $val) {
$val = htmlentities($val);
$body .= '<li>' . $val . '</li>';
}
$body .= '</ul>';
} else {
$body .= htmlentities($data);
}
$body .= '</p>';
}
$body .= '</body></html>';
$to = strip_tags($_POST['emailTo']);
$subject = strip_tags($_POST['emailSubject']);
$headers = "From: " . strip_tags($_POST['emailFrom']) . "\r\n";
$headers .= "Reply-To: " . strip_tags($_POST['emailFrom']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
mail($to, $subject, $body, $headers);
header('Location: ' . $redirectTo);
?>
I am emailing form data, using
$message .= "IP Address : ";
$message .= $_SERVER['REMOTE_ADDR'];
$message .= $eol;
$logdata = '';
foreach ($_POST as $key => $value)
{
if (!in_array(strtolower($key), $internalfields))
{
if (!is_array($value))
{
$message .= ucwords(str_replace("_", " ", $key)) . " : " . $value . $eol;
}
else
{
$message .= ucwords(str_replace("_", " ", $key)) . " : " . implode(",", $value) . $eol;
}
}
}
Is it possible to parse it in table form.
Currently i am getting result like;
NAME : nina
EMAIL: nina_20#gmail.com
Yes, You can do that. Just add the table tag in the code, like this:
$message .= "<table>";
foreach ($_POST as $key => $value)
{
$message .= "<tr>";
if (!in_array(strtolower($key), $internalfields))
{
if (!is_array($value))
{
$message .= "<td>".ucwords(str_replace("_", " ", $key)) . "</td> <td> " . $value . "</td>";
}
else
{
$message .= "<td>".ucwords(str_replace("_", " ", $key)) . "</td> <td> " . implode(",", $value) . "</td>";
}
}
$message .= "</tr>";
}
$message .= "</table>";
Check the places of table, tr and td. You can place them according to your requirement.
In my foreach loop I want the item that comes in the IF not te be outputted anymore after the if statement. It keeps coming back... How can I fix this?
$content1 = "";
foreach($array as $key => $value) {
if(!empty($brandnid)) {
$content1 .= ',"' . $key . '":{"und":[{"nid":"[nid:' . $value . ']"}]}';
unset($array[array_search('field_brandid',$array)]);
}
$content1 .= ',"' . $key . '":{"und":[{"value":"' . $value . '"}]}';
}
if(!empty($title) && !empty($day) && !empty($month) && !empty($year)) {
$postContent = '{"type":"carmodels","title":"' . $title . '"' . $content1 . ',"field_modelyear":{"und":[{"value":{"date":"' . $dateBuild . '"}}]}}';
}elseif(!empty($day) && !empty($month) && !empty($year)) {
$postContent = '{"type":"carmodels","field_modelyear":{"und":[{"value":{"date":"' . $dateBuild . '"}}]}' . $content1 . '}';
}else {
$postContent = '{"type":"carmodels"' . $content1 . '}';
}
In above case I entered fields color (normal field) and the brandid and it displays the following in $postContent:
{"type":"carmodels","field_modelcolor":{"und":[{"nid":"[nid:black]"}]},"field_modelcolor":{"und":[{"value":"black"}]},"field_brandid":{"und":[{"nid":"[nid:24]"}]},"field_brandid":{"und":[{"value":"24"}]}}
But it should only do the brandid in the if statement (once) and after that the normal field which doesnt reach the IF also once
I have a statement that is grabbing information from the database, and then is printed out after it is fully prepared.. For some reason though, my script is not printing out the information. I have it in this if statement:
if($community == ''){ print $community . "\n\n" . "END" . "\n"; } else { print $community; echo "hi";}
This prints out when it is ran:
() wrote:
But that is all it prints out. That is coming from the 8th $community .= line. So, my question is, why is it ONLY printing out () Wrote: and not all the variables as well?
// and ticker_symbol ='".$sym."'
$c_sql = "SELECT message_id, subject, author, FROM_UNIXTIME(datestamp,'%m-%d-%Y') AS formatted_datestamp, forum_id, body, thread, user_id FROM phorum_messages WHERE user_id=13423720 ORDER BY datestamp DESC LIMIT 5";
$c_result = mysql_query($c_sql,$connection) or die("Couldn't execute get query");
// Declare Variables
$body = $c_result['body'];
$forum_id = $c_result['forum_id'];
$user_id = $c_result['user_id'];
$author = $c_result['author'];
$formatted_datestamp = $c_result['formatted_datestamp'];
// Prepare the statement
if ($c_result != "") {
$community .= $forumPost = '<<<ENDL '. "\n";
$community .= $body . "\n";
$community .= 'ENDL;' . "\n";
$community .= '$forumPost = stripBBCode(strip_tags($forumPost));' . "\n";
$community .= "\n";
$community .= '<div class="comment">' . "\n";
$community .= '<table cellspacing="0" cellpadding="0" border="0" class="reply"><tbody><tr>' . "\n";
$community .= '<td width="90%"><b>'.$author.' ('.$formatted_datestamp.') wrote:</b><br />' . "\n";
$community .= '<p>'.iconv("ISO-8859-1//TRANSLIT", "UTF-8", $forumPost).'</p></td>' . "\n";
$community .= '</tr></tbody></table>'. "\n";
$community .= '</div>' . "\n";
}
// Print out the prepared statement
if($community = ''){ print $community . "\n\n" . "END" . "\n"; } else { print $community;}
When you are calling if($community = ''){ you only have one equals sign which will set $community to a blank string.
I think what you mean to do is if($community == ''){
It should have the double-equal:
if($community == '')
With a single = sign you're simply assigning an empty string to variable $community - and then checking whether it's true. Empty strings evaluate to false, hence you're getting into your else part - and losing your value in the process.
You only have one = sign
you need:
if($community == '') { etc...