• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I save variables in database to display in textarea?

#1
I have an app that allows users to send an email response with a customizable default email. I have some variables available in the view ($r->firstName, $r->lastName) which is the name of the person the user is responding to.

The customizable default message is saved in the database for each user, for example, ...
Code:
Dear <?php echo $r->firstName; ?> <?php echo $r->lastName; ?>, Thank you for your interest in...

but when the view gets the message from the database after the user saves it, it displays the <?php echo $r->firstName; ?> and <?php echo $r->lastName; ?> just like that in the textarea (see attached image). I would like it to show the actual values for those in the view.

The variables show the correct names in the view if you code them in the view, just getting them from the database and populating the textarea with them is a problem. I know why it happens, just dont know the way to fix it. Any help would be appreciated


Attached Files Thumbnail(s)
   
Reply

#2
If your code example is the content of your textarea, than its saved so ;o)
you cannot save this in the database and evaluate this on the view because its been saved as text.
What do you want exactly? A kind of template witch gets replaced in the view?

Reply

#3
Thanks,
Yeah, I want users to be able to customize their own default response message and have the ability to include a variable for the name of the person they're responding to.
I have the app set up so that they can customize their own default message, its just the problem of not knowing how/if I can let them include the variables for names in their message and have it display the actual names
Reply

#4
Ok, for that you need a placeholder replacement. For e.g. you have text like that

Code:
$txt = "the user %username% are crazy";

This string can be saved in the database and after get it back he has to be replaced with the needed content like that

PHP Code:
str_replace_first('%username%'$r->lastName$txt_back_from_db); 

See details at http://php.net/manual/de/function.str-replace.php or search here for preg_replace.

Hope this helps.

Reply

#5
Ah, excellent. Thanks!
Reply

#6
That worked great, thanks.

One more question - So the textarea is being sent in an email but is there a way to preserve the breaks (<br />) and possibly html the user puts in the textarea in the email message? Right now it just removes all breaks and sends the message.
Reply

#7
Normaly the data will be saved with a newline (\n, \n\r) in your database. To convert it back in your view you can use

PHP Code:
nl2br($r->text); 

Reply

#8
Thanks!
Reply

#9
Your text editor should handle the line breaks there should be a setting for it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.