NYCPHP Meetup

NYPHP.org

[nycphp-talk] Prefill <textarea><?=$variable?></textarea>

Tedd Sperling tedd.sperling at gmail.com
Mon Nov 7 17:44:07 EST 2011


On Nov 6, 2011, at 6:19 PM, tuon1 at netzero.net wrote:

> Hi, Guys!
> 
> This maybe a simple solution that I overlooked. I can't seem to get my variables to populate in text areas with <textarea> tags. Input boxes (<input>) work just fine, but for some reason <textarea> boxes won't populate any content at all.
> 
> Is there something else I have to do to get <textarea> to accept content or populate content?
> 
> As far as I know, it shouldn't be any different than <input> boxes in term of populating content, for example, here is the code to populate the two types of boxes:
> 
> <?php
> 
> echo "<input><?=$variable1?></input>";
> 
> echo "<textarea><?=$variable2?></textarea>";
> 
> ?>
> 
> As I said, the input boxes work just fine, but the text area boxes didn't work and came up blank text area with no content at all.
> 
> Thanks in advance.
> 
> Paul Tuon
> 

Paul:

First, use of "<?=" is  'no no' -- it's not good practice. Keep in mind that if the "short tag" option was not set your code would fail. Considering that you have no control over where your code might be run in the future, save everyone time/effort and use "<?php echo(); ?>" and not '<=' . In addition, your code may be used in an XML environment where '<=' is not recognized as being a PHP identifier.

In any event, the way to do it is:

<?php echo($variable1); ?>  // I prefer

OR

<?php echo $variable1; ?>

Second, let's look at what you told the interpreter to do -- namely echo "<input><?=$variable2?></input>"

a. You don't need an ending tag for an input statement, thus no </input> is required.

b. Your use of an echo (i.e., '<?=') inside of an echo is problematic at best -- it's sloppy coding.

So, to fix this, I would suggest:

echo("<input type='text' value=/"$variable1/">");

or

echo '<input type="text" value="' . $variable1 .'">';

There are other ways of doing this (note the mix of different quotes).

For the second part, namely the <textarea>, it does need a closing tag and thus </textarea> is required. To do that I would suggest:

echo("<textarea rows=/"4" cols="/40/">$variable2</textarea>");

or

echo '<textarea rows="4" cols="40">' . $variable2 . '</textarea>';

Please note the above is typed from memory and may have typos in it -- but it should be generally correct.

Cheers,

tedd


_____________________
tedd at sperling.com
http://sperling.com








More information about the talk mailing list