PHP textarea problems

djcustom

New Member
Messages
17
Reaction score
0
Points
0
I am working on an edit page to change variables in a config file. My issue is that I am loading a variable that contains an email message into a textarea.

$message = Dear $first_name $last_name,\n\n We thank you for your purchase. Please allow 7-10 days for delivery. If you have any questions please feel free to email us at info@yourwebsite.com.

My issue is that I cant seem to get the variable to load into the textarea properly. I have changed the \n\n to <br> tags and it just displays the br tags in its place. I want the message to look like this in the text box.

Dear $first_name $last_name,

We thank you for......

If anyone can help I would highly appreciate it.
 

jrobert755

New Member
Messages
15
Reaction score
0
Points
0
here is a working example:

PHP:
<?php
 $first_name = "test";
 $last_name = "test";
 $message = "Dear " . $first_name . " " . $last_name . ",\n\n We thank you for your purchase. Please allow 7-10 days for delivery. If you have any questions please feel free to email us at info@yourwebsite.com.";
 echo "<html>
  <textarea  cols=\"20\" rows=\"5\" wrap=\"hard\">".$message."</textarea>
  </html>";
?>

your biggest problem was not using quotes when you wanted to put the information in $message. Then, when you want to add a php variable, you end quotes, put a period, type the variable name, then (if there are more variables or more you want to display on the screen) you add another period and another set of quotes or another variable. and, if you don't know, remember to escape the quotes with a backslash.
 

djcustom

New Member
Messages
17
Reaction score
0
Points
0
Ok let me add some more information. I have tried what you just sent me it does not work.

I want the text box to show exactly this.

Dear $first_name $last_name,

We thank you for......


$first_name and $last_names are used by a php code on another page. Like i said this is for a config document.

right now no matter what i try it displays...

Dear $first_name $last_name, \n\nWe thank you for....
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Looks like you're using single quotes which don't interpolate variables. I can't say for certain because you didn't post the code you're using. Please post a minimal test case; it's very hard to see why your code isn't working if we can't see what your code is.

If you want a largish multiline string, use the heredoc syntax.

Also, read up on strings in PHP.
 

djcustom

New Member
Messages
17
Reaction score
0
Points
0
Ok here are some code snippets

This is the php page that I am editing
Code:
$completed_message_content='Dear $first_name $last_name,\n\n We thank you for your purchase. Please allow 7-10 days for delivery. If you have any questions please feel free to email us at info@yourwebsite.com.';
$completed_subject='Your Book Password...';
$completed_from_email='info@yoursite.com';
$completed_reply_email='sales@yoursite.com';
This is the code used to retrieve the information from the file above
Code:
$handle1 = fopen($completed_email, 'r');
            $contents1 = fread($handle1, filesize($completed_email));
            // get values from file
                foreach($completed_array as $from => $to)
                { 
                    if(preg_match("/$from\=\'(.*?)\';/", $contents1, $matches)) {
                         $config_values[$from] = $matches[1];
                    } 
                    else { 
                        $config_values[$from] = '';
                    } 
                } 
                $completed_message_content = $config_values['\$completed_message_content'];
                $completed_subject = $config_values['\$completed_subject'];
                $completed_from_email = $config_values['\$completed_from_email'];
                $completed_reply_email = $config_values['\$completed_reply_email'];            
            fclose($handle1);
This is the form the information is loaded into.

Code:
<form>
<textarea name='completed_message_content' id='completed_message_content' style="width:618px;height:150px" wrap="hard">
<?php echo $completed_message_content; ?>
</textarea>
</form>
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
251
Points
63
Instead of using file reads, why don't you include() the file?
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Code:
$completed_message_content='Dear $first_name $last_name,\n\n We thank you for your purchase. Please allow 7-10 days for delivery. If you have any questions please feel free to email us at info@yourwebsite.com.';
Aha, single quotes. Only escaped single quotes and backslashes are processed within single quotes. Use the heredoc syntax and read up on string literals in PHP.
Code:
$handle1 = fopen($completed_email, 'r');
            $contents1 = fread($handle1, filesize($completed_email));
            // get values from file
                foreach($completed_array as $from => $to)
                { 
                    if(preg_match("/$from\=\'(.*?)\';/", $contents1, $matches)) {
                         $config_values[$from] = $matches[1];
                    } 
                    else { 
                        $config_values[$from] = '';
                    } 
                } 
                $completed_message_content = $config_values['\$completed_message_content'];
                $completed_subject = $config_values['\$completed_subject'];
                $completed_from_email = $config_values['\$completed_from_email'];
                $completed_reply_email = $config_values['\$completed_reply_email'];            
            fclose($handle1);

fopen (rather than include) looks OK in this context as you need to process the email before printing. However, the '\$completed...' aren't doing what you think. You don't (and can't) escape '$' in single quotes; the result of '\$' is '\$' (try print '\$foo'; in a PHP script).

Also, $config_values[$from] = $matches[1]; looks wrong (if $from is [e.g.] 'info@yoursite.com', this line will set $config_values['info@yoursite.com'] to $matches[1]). Have you defined $from? Are you sure you don't want to set $config_values['from']?
 
Top