I have a <textarea> in a form for user comments, and when the contents are passed to form mail, the line breaks are being converted to spaces. How can I preserve the line breaks that the form's user types in?
relevant php:
$comments = $_REQUEST['comments'];
// This grabs the comments from the submitted form
//...
$to = $configEmail;
$subject = "Website Order Received: $offer";
$contents = "blah blah blah...";
if (!empty ($comments)) {
$contents = $contents."\nComments: $comments\n\n";
}
//...
mail($to, $subject, $contents);
And in the HTML end of the form... (the comments are put into the form if it's submitted with errors, so data isn't lost)
<label>Comments / Questions</label>
<textarea name="comments"><?php echo $comments; ?></textarea>
If I type:
line 1
line 2
line 3
It remains like that if the form is submitted with errors, so $comments = $_REQUEST['comments']; is definitely preserving the line breaks. But the plain-text e-mail gives me:
line 1 line 2 line 3
How can I preserve the line breaks?
The problem is the line breaks coming from the textarea are \n not <br>..
So replace the \n by <br> before sending the mail..
$body= str_replace("[enter]", "\n",$body);
Rember user double quoutes in "\n"...
Try the nl2br() function, if it doesn't initially work try to send the message as an HTML email.
Related
I have a web form that allows a person to enter text and then send an email using swift mail. The text they enter may contain \r\n. I send email from swift mail in text/html. Prior to sending the email, I have attempted to do a sting replace on the \r\n, as well as a nl2br function on the user entered text so that all occurrences of \r\n are changed to ; yet in all cases the email still arrives with \r\n being displayed in the text message, as opposed to actual line breaks.
below is the code snippet to prep the text and the email code.
/* replace all cr/lf with a <br> */
$ind_msg = nl2br($ind_msg);
/* send the email message */
$status = send_email($db, $ind_email, $ind_name, $sender, $sender_name, $msg_subj, $ind_msg, "");
email code
/* create the message */
$message = Swift_Message::newInstance();
$message->setTo(array($recipient => $recipient_name));
$message->setSubject($msg_subject);
$message->setContentType("text/html; charset=UTF-8");
$message->setBody($msg_body);
$message->setFrom(array($sender => $sender_name));
What am I missing or doing wrong?
nl2br do not replace newline-chars. It adds the "br" tag before all newline chars. If you want to replace them, use str_replace();
vg
bytecounter
You can try :
$ind_msg = str_replace(array('\r\n'), array('<br>'), $ind_msg);
i have an html page with text filed , and an action button to open a popup like this example
http://www.andwecode.com/playground-demo/pop-up-login-signup-box-jquery/#modal
(hit login to see the popup) who had also a two text fields , how i can collect the text entered in the 3 boxes (the one in the page and the two in the popup window) simultaneously with a php file ? cause i need to send them all to my email
an exemple of my php file :
<?php
$txt1 = "textfield 1 : ".$_POST['textfield1'];
$txt2= "textfield 2 : ".$_POST['textfield2'];
$tx3= "textfield 3 : ".$_POST['textfield3'];
$message = "
$txt1
$txt2
$txt3
";
$to = "myemail#example.com";
$subject = "data :".$txt1;
$headers = "From: <myemail#example.com>";
$headers = "MIME-Version: 1.0\n";
$from = "example";
mail($to,$subject,$message,$headers,$from);
}
?>
any idea how to collect all the text from the
For PHP to receive the values of three different inputs in a single POST, all three inputs need to be contained within the same HTML <form>. Try moving HTML around so that all inputs are contained in a single form, then they should all be accessible to PHP in the $_POST array.
It seems to me you don't have 'name' attributes on the input tags, try something like this,
<!--ALL THE HTML-->
Email: <input type="text" name="email"></input>
Password: <input type="pass" name="pass"></input>
<!--MORE HTML-->
SEPARATE PHP FILE!!
<?php
function getdata(){
$email= $_GET['email']; //Email
$pass= $_GET['pass']; //Password
$name = $_GET['name']; //Full name (Only will apply if registering)
};//This gets the data and gives it a variable
//Then more php like you had already to email to yourself
?>
In simple terms, add the 'name' attribute to the inputs (name="whatevername") and then use the $_GET in php to get the data ($varname = $_GET['nameattributehere'];).
Just remember!
To put the PHP code in a separate file with the .PHP extension on it (.HTML doesn't work!)
Add the 'name' attributes to the inputs
And on the <form> tag you MUST ADD THIS! method="get" action="srcforthephpfile.php"
Good Luck
I apologize but I'm very new to PHP and I am trying to create a very simple form that sends an email back to a user when they enter in their email address. I want the message to include some data from our database. I have been able to create a form that works perfectly as long as I enter in the message manually (like $message = "Hi. How you doing?") but I can't seem to figure out how to incorporate the recordset data. What I was hoping was to use something like...
<?php
$to = $_REQUEST['Email'] ;
$message = '<?php echo $row_rsPersonUser['bio']; ?>'; <<<<<<<<Line 63
$fields = array();
$fields{"Email"} = "Email";
$headers = "From: noreply#domain.ca";
$subject = "Thank you";
mail($to, $subject, $message, $headers);
?>
What I get from this is "Parse error: syntax error, unexpected T_STRING in.... on line 63". I know it's formatted wrong but I don't have a clue why. When I drop the into the body, the info I want does display on the webpage so I know that part is working. Any help would be welcomed.
Thanks
You don't have to use PHP start and end tags inside PHP code itself
$message = '<?php echo $row_rsPersonUser['bio']; ?>'; // this is wrong
^^^^^ ^^
Should be
$message = $row_rsPersonUser['bio'];
Just change the 63rd line like below..
you can't start one <?php block in another <?php block
$message = $row_rsPersonUser['bio'];
If you were to do that it would just print <?php echo… as literals since you can't send php code as a email only html/plan text
$message = '<?php echo $row_rsPersonUser['bio']; ?>';
should be:
$message = $row_rsPersonUser['bio'];
and (I tested the following and it appears the {}'s work but you might want to switch to only []'s for standardization and not sure if you might get in trouble later on?)
FROM: http://us1.php.net/manual/en/language.types.array.php
Note:
Both square brackets and curly braces can be used interchangeably for accessing array elements (e.g. $array[42] and $array{42} will both do the same thing in the example above).
$fields{"Email"} = "Email";
should be:
$fields["Email"] = "Email";
You are already inside the php code, no need to add extra php start and end tags inside the variable name. Similar to how you have used the $to variable, you can use the $message variable.
So use
$message = $row_rsPersonUser['bio'];
and it would work fine.
I am implementing a contact form. When the user submits the form, all input is validated and stored in a session. It is then forwarded to a page that informs the user of a successfully posting of the comment, displaying the data entered.
The trouble I am having is that all new lines are not being displyed correctly as breaks using nl2br().
User input:
<textarea name="comments" rows="10" cols="50" id="comments" tabindex="5" title="comments">
<?php echo isset($_POST['comments']) ? $_POST['comments'] : ''; ?>
when validated...
$_SESSION['comments'] = $_POST['comments'];
forwarded on to contact-sent page and then appended to the string to display
$forwardString = "<h2>New Website Comment: </h2><h3>" . $cEmail . "</h3><p>" . $cComment . "</p>";
Then displayed:
echo nl2br($forwardString);
Where do I implement the nl2br() function?
Example input:
Just a test to verify contact works correctly.
We should see two line breaks here.
One line break here
Currently yields:
Just a test to verify contact works correctly.We should see two line breaks here.One line break here
try
echo nl2br(stripslashes($forwardString));
You could be possibly escaping the string twice so the \n becomes \n
i'm using ajax contact form, downloaded from: http://youhack.me/2010/07/22/create-a-fancy-contact-form-with-css-3-and-jquery/
Everything works ok except UTF as i can't use cyrilic symbols when submitting.
The php:
$name = $_POST['name']; // contain name of person
$email = $_POST['email']; // Email address of sender
$web = $_POST['web']; // Your website URL
$body = $_POST['text']; // Your message
$receiver = "receiver#domain.com" ; // hardcorde your email address here - This is the email address that all your feedbacks will be sent to
if (!empty($name) & !empty($email) && !empty($body)) {
$body = "Name: {$name}\n\nSubject: {$web}\n\nMessage: {$body}";
$send = mail($receiver, 'Contact from domain.com', $body, "From: {$email}");
if ($send) {
echo 'true'; //if everything is ok,always return true , else ajax submission won't work
}
}
It uses jquery.validationEngine-en for validation.
My html already has "Content-Type" content="text/html; charset=utf-8" in header.
I'm new to php and jquery, so i would appriciate some guidance to make UTF-8 work when submitting.
Thanks :)
Edit: When i try to use cyrilic chars (čšćđ) on a required field i get ajax input error "Please use letters only". If i submit the form with cyrilic chars on a non-required field, i receive and email, all letters show ok except cyrilic, which are like this: Å¡.
Edit 2: When i set the recipient to gmail (webmail), cyrilic chars show up ok, except in one field, where Ajax doesnt let me use them (regex from Reinder answer).
When i set recipient in outlook (local) and submit the form, none of the cyrilic chars don't show up ok, example: ÄĹĄ oÄa ĹĄ ÄŽŠÄÄ
SOLVED Thanks to Reinder for guide and David! Will solve it today :)
having looked at the plugin you're using, I think this has to do with the validation regex inside jquery.validationEngine-en.js
when the validation is set to 'onlyLetter' it will check using
/^[a-zA-Z\ \']+$/
and none of your characters čšćđ are allowed here...
you need to create a language validation javascript for the language you're using and change that regular expression. For example, have a look at this post
The next thing is to check the encoding of your PHP file and your headers.
Place this at the top of your PHP code
header("Content-type: text/html; charset=utf-8");
Check if the values are correctly displayed when just outputting them in PHP, like so:
echo $name;
If they are correctly displayed in the browser and it's just the email that's incorrectly displaying the characters, then you need to pass an encoding header to the email too
example:
$headers = "From: $name <$email>\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\n";
$body = "Name: {$name}\n\nSubject: {$web}\n\nMessage: {$body}";
$send = mail($receiver, 'Contact from domain.com', $body, $headers);
have a look at the mail function on the PHP.NET website
Rather than use the default PHP mail() function, I've found this come in handy when working with Japanese:
http://bitprison.net/php_mail_utf-8_subject_and_message