+ Reply to Thread
Results 1 to 7 of 7

Thread: PHP textarea problems

  1. #1
    djcustom is offline x10Hosting Member djcustom is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    15

    PHP textarea problems

    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.

  2. #2
    jrobert755 is offline x10Hosting Member jrobert755 is an unknown quantity at this point
    Join Date
    Jan 2009
    Posts
    15

    Re: PHP textarea problems

    here is a working example:

    PHP Code:
    <?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.

  3. #3
    djcustom is offline x10Hosting Member djcustom is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    15

    Re: PHP textarea problems

    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....

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP textarea problems

    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.

  5. #5
    djcustom is offline x10Hosting Member djcustom is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    15

    Re: PHP textarea problems

    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>

  6. #6
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: PHP textarea problems

    Instead of using file reads, why don't you include() the file?
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  7. #7
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP textarea problems

    Quote Originally Posted by djcustom View Post
    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.
    Quote Originally Posted by djcustom View Post
    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']?

+ Reply to Thread

Similar Threads

  1. Places to learn php
    By JaWasabi in forum Scripts & 3rd Party Apps
    Replies: 9
    Last Post: 01-13-2009, 02:03 AM
  2. PHP Problems on Lotus?
    By big5online in forum Free Hosting
    Replies: 2
    Last Post: 12-22-2008, 04:15 AM
  3. php problems after upgrade
    By fparty in forum Free Hosting
    Replies: 1
    Last Post: 10-14-2008, 10:45 PM
  4. PHP Version Problems
    By WinGate in forum Free Hosting
    Replies: 5
    Last Post: 09-25-2007, 05:46 PM
  5. "PHP Startup: Invalid Library" - Interesting error
    By javaguy78 in forum Free Hosting
    Replies: 5
    Last Post: 03-27-2007, 02:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers