Unwanted Characters in Data

Please report bugs and any annoyances here. Kindly include all possible details: steps to reproduce, expected result, actual result, screenshots, ... etc.
Post Reply
rpierce
Veteran Member
Posts: 255
Joined: 2018-11-26 13:55
Location: Washington State

Unwanted Characters in Data

Post by rpierce » 2021-02-11 00:43

Hi Group,

I have an accident form that is completed on our construction projects when there is an incident or injury. The people complete this form and submit. I use the Hooks to generate an email notification that goes out to principals in our company. The problem I want to resolve is unwanted characters that seem to be coming from the database. If the user includes "Quotes" or a carriage return in their information, the resulting email message has additional unwanted symbols. I hope there is a fix and I don't just have to live with this. Please bear in mind if you reply, I'm not a coder and you'll have to help me out with some basic instructions on how to implement any ideas you might have.

Thanks in advance!

Ray
Attachments
carriage_return.JPG
carriage_return.JPG (113.75 KiB) Viewed 2877 times

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: Unwanted Characters in Data

Post by onoehring » 2021-02-11 10:39

Hi,

depending on the function you use to send mail, you may need to adjust some code.
When you use the AG function sendmail (in incFunctions.php) you may want to use nl2br for encoding the message:

Code: Select all

sendmail(array(
			'to' => $email,
			'name' => $memberID,
			'subject' => $subject,
			'message' => nl2br($message)
		));
See https://www.php.net/manual/en/function.nl2br.php

Or use htmlspecialchars ( https://www.php.net/manual/de/function. ... lchars.php )

Olaf

rpierce
Veteran Member
Posts: 255
Joined: 2018-11-26 13:55
Location: Washington State

Re: Unwanted Characters in Data

Post by rpierce » 2021-02-11 17:59

Thank you for the reply onoehring,

I tried to insert the nl2br into my Hooks code but get an error. My code looks somewhat different than your example. I'm using an array to send the email message to many recipients using sendmail. All of this coding has been put together with the help of others in this forum and AppGini support.

Where exactly do I insert the nl2br with the code as I have it in my hook file??

Thank you again.
Ray

Code: Select all

$receipt=array([email protected]','[email protected]');

	foreach ($receipt as $address){
	
	$projectname = sqlValue("select name from project where id='{$data['project']}'");
	
	sendmail(array(
	
		// mail recipient
		'to' => $address,
		
		'name' => "",

		// subject
		'subject' => "An injury report has been submitted for " . sqlValue("select name from project where id='{$data['project']}'"),
		
		// message
		'message' => "The following Injury Report was submitted by {$memberInfo['username']}<br> This is a preliminary report, details may change. \n\n"."<br>

User avatar
aarlauskas
Veteran Member
Posts: 127
Joined: 2019-04-28 18:03
Location: Medway, UK

Re: Unwanted Characters in Data

Post by aarlauskas » 2021-02-11 20:03

I see this often in my emails too after submitting the form. No special characters or so in the fields, but email data comes with some nonsence as per this post \n\r blah. Interested to see what cases it as well.

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: Unwanted Characters in Data

Post by onoehring » 2021-02-12 08:30

Hi,

I would suggest trying this

Code: Select all

$receipt=array([email protected]','[email protected]');
	foreach ($receipt as $address){
	$projectname = sqlValue("select name from project where id='{$data['project']}'");
	sendmail(array(
		// mail recipient
		'to' => $address,	
		'name' => "",
		// subject
		'subject' => "An injury report has been submitted for " . sqlValue("select name from project where id='{$data['project']}'"),		
		// message
		'message' => nl2br("The following Injury Report was submitted by {$memberInfo['username']}<br> This is a preliminary report, details may change. \n\n"."<br>)
or replacing my inserted nl2br with htmlspecialchars.

Olaf

rpierce
Veteran Member
Posts: 255
Joined: 2018-11-26 13:55
Location: Washington State

Re: Unwanted Characters in Data

Post by rpierce » 2021-02-12 22:30

Hi Olaf,

Thank you so much for trying to help a rookie out! I tried adding both of your suggestions to the code but get an error. It seems to me that in the call- 'message' => that the opening and closing quote marks are negated by the addition of the parenthesis that you indicate. It appears that I must have a closing quote mark a the end of my email message? I am including the entire message below to give a better picture....Thanks again!

Code: Select all

		// message
		'message' => "The following Injury Report was submitted by {$memberInfo['username']}<br> This is a preliminary report, details may change. \n\n"."<br>

<p>
<p><strong>Project:</strong> $projectname
<br>
<table>
    <tbody>
        <tr>
            <td><strong>Date of Injury</strong></td>
            <td>&nbsp;&nbsp;</td>
            <td><strong>Time of Injury&nbsp;</strong></td>
        </tr>
        <tr>
            <td>{$data['date']}</td>
            <td></td>
            <td>{$data['time']}</td>
        </tr>
    </tbody>
</table>

<p>
    <Strong><h3>Injured Worker Details</h3></strong>


    <table>
        <tbody>
            <tr>
                <td><strong>First Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong></td>
                <td><strong>Last Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong></td>
                <td><strong>Craft</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                <td><strong>D.O.B.</strong>&nbsp;&nbsp;&nbsp;</td>
            </tr>
            <tr>
                <td>{$data['fname']}</td>
                <td>{$data['lname']}</td>
                <td>{$data['craft']}</td>
                <td>{$data['dob']}</td>
            </tr>
        </tbody>
    </table>
    
    <h3>Injury Details</h3>

    <p><strong>Injury Severity:</strong>&nbsp;{$data['severity']}
    <p><strong>Type of Injury:</strong>&nbsp;{$data['injury']}
    <p><strong>Body Part</strong>&nbsp;{$data['body_part']}

    <p><strong>Treatment Provided:</strong> {$data['treatment']}</p>
    <p><strong>Treated By:</strong> {$data['treat_by']}</p>
    <p><strong>Mode of Injury:</strong> {$data['event']}</p>
    <p><strong>Event Description:</strong> {$data['event_desc']}</p>
    <p><strong>Source of the Injury:</strong> {$data['source']}</p>
    <p><strong>Injury Source Details:</strong> {$data['source_desc']}</p>
    <p><strong>What was the employee doing just before the incident occurred?&nbsp;</strong> {$data['action']}, {$data['action_desc']}
    <p><strong>What object or substance directly harmed the employee?&nbsp;</strong> {$data['object']}, {$data['object_desc']}
    
    <p><strong>Direct Cause:</strong> {$data['direct_cause']}</p>
    <p><strong>Indirect Cause:</strong> {$data['indirect_cause']}</p>
    <p><strong>Basic Cause:</strong> {$data['basic_cause']}</p>
    <p><strong>Causation Rationale:</strong> {$data['cause_explain']}</p>
    
    <p><strong>Workplace Layout Issues:</strong> {$data['workplace']}</p>
    <p><strong>Workplace Layout Explanation:</strong> {$data['work_explain']}</p>
    <p><strong>Behavior Based Safety Factor:</strong> {$data['bbs']}</p>
    <p><strong>Authors comments:</strong> {$data['comments']}</P>",	
		
));
}


	return TRUE;
} 

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: Unwanted Characters in Data

Post by onoehring » 2021-02-14 08:43

Hi,

maybe I missed something. I copied your code from the previous posting. Addon: You might throw some variable into the message (see my second sugegstion)

Code: Select all

$yourTEXT = "The following Injury Report was submitted by {$memberInfo['username']}<br> This is a preliminary report, details may change. \n\n"."<br>

<p>
<p><strong>Project:</strong> $projectname
<br>
<table>
    <tbody>
        <tr>
            <td><strong>Date of Injury</strong></td>
            <td>&nbsp;&nbsp;</td>
            <td><strong>Time of Injury&nbsp;</strong></td>
        </tr>
        <tr>
            <td>{$data['date']}</td>
            <td></td>
            <td>{$data['time']}</td>
        </tr>
    </tbody>
</table>

<p>
    <Strong><h3>Injured Worker Details</h3></strong>


    <table>
        <tbody>
            <tr>
                <td><strong>First Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong></td>
                <td><strong>Last Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong></td>
                <td><strong>Craft</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                <td><strong>D.O.B.</strong>&nbsp;&nbsp;&nbsp;</td>
            </tr>
            <tr>
                <td>{$data['fname']}</td>
                <td>{$data['lname']}</td>
                <td>{$data['craft']}</td>
                <td>{$data['dob']}</td>
            </tr>
        </tbody>
    </table>
    
    <h3>Injury Details</h3>

    <p><strong>Injury Severity:</strong>&nbsp;{$data['severity']}
    <p><strong>Type of Injury:</strong>&nbsp;{$data['injury']}
    <p><strong>Body Part</strong>&nbsp;{$data['body_part']}

    <p><strong>Treatment Provided:</strong> {$data['treatment']}</p>
    <p><strong>Treated By:</strong> {$data['treat_by']}</p>
    <p><strong>Mode of Injury:</strong> {$data['event']}</p>
    <p><strong>Event Description:</strong> {$data['event_desc']}</p>
    <p><strong>Source of the Injury:</strong> {$data['source']}</p>
    <p><strong>Injury Source Details:</strong> {$data['source_desc']}</p>
    <p><strong>What was the employee doing just before the incident occurred?&nbsp;</strong> {$data['action']}, {$data['action_desc']}
    <p><strong>What object or substance directly harmed the employee?&nbsp;</strong> {$data['object']}, {$data['object_desc']}
    
    <p><strong>Direct Cause:</strong> {$data['direct_cause']}</p>
    <p><strong>Indirect Cause:</strong> {$data['indirect_cause']}</p>
    <p><strong>Basic Cause:</strong> {$data['basic_cause']}</p>
    <p><strong>Causation Rationale:</strong> {$data['cause_explain']}</p>
    
    <p><strong>Workplace Layout Issues:</strong> {$data['workplace']}</p>
    <p><strong>Workplace Layout Explanation:</strong> {$data['work_explain']}</p>
    <p><strong>Behavior Based Safety Factor:</strong> {$data['bbs']}</p>
    <p><strong>Authors comments:</strong> {$data['comments']}</P>"';
    
    
    
    $receipt=array([email protected]','[email protected]');

	foreach ($receipt as $address){
	
	$projectname = sqlValue("select name from project where id='{$data['project']}'");
	
	sendmail(array(
	
		// mail recipient
		'to' => $address,
		
		'name' => "",

		// subject
		'subject' => "An injury report has been submitted for " . sqlValue("select name from project where id='{$data['project']}'"),
		
		// message
		'message' => nl2br($yourTEXT),	

... MESSAGE IS IN YOUR NEW $yorTEXT VARIABLE...
..... HERE IS SOMETHING MISSING IN YOUR CODE ... AT LEAST 2 CLOSING ). YOU MIGHT WANT TO REMOVE THE , AFTER THE MESSAGE

	return TRUE;
} 


Olaf

rpierce
Veteran Member
Posts: 255
Joined: 2018-11-26 13:55
Location: Washington State

Re: Unwanted Characters in Data

Post by rpierce » 2021-02-17 19:18

Hi Olaf,

I tried your new code but still get the unwanted characters in the email message.

The characters don't appear in the database, so I'm assuming that the PHP is injecting them?? Any other ideas?

rpierce
Veteran Member
Posts: 255
Joined: 2018-11-26 13:55
Location: Washington State

Re: Unwanted Characters in Data

Post by rpierce » 2021-02-18 00:58

Olaf!!

I used the link you provided to PHP String Functions and found this "stripcslashes". That worked to remove the characters I don't want, but it also removed the line breaks. But, at least I'm moving in the forward direction!! Thank you for offering your time to help with my problem.

rpierce
Veteran Member
Posts: 255
Joined: 2018-11-26 13:55
Location: Washington State

Re: Unwanted Characters in Data

Post by rpierce » 2021-02-18 01:02

Olaf,

This seemed to work:

// message
'message' => nl2br (stripcslashes($yourTEXT)),


Post Reply