开发者

How do i add 'default text' in a php form

I'd like the following code to be change so that the textarea has a default value that disappears when it's focused.

if ($txtActive != 'N') {
    $value = (isset($_POST['mod_SEF_textar开发者_运维技巧ea'])) ? htmlspecialchars($_POST['mod_SEF_textarea']) : "";
    echo "<tr>";
    echo "<th align='" . $labelAlign . "'></th>";
    echo "<td><textarea class='SEFTextArea' name='mod_SEF_textarea' id='textarea' value=\'tester\' rows='$txtRows' cols='$txtCols'>" . stripslashes($value) . "" . "</textarea>";
    echo ($txtError) ? "<br /><b style='color: $errorTxtColor;'>$txtError</b>" : '';
    //  echo "wendy TESTerburger";
    echo "</td>";
    echo "</tr>\n";


If I'm understanding the question properly, you would change this line, like so:

$value = (isset($_POST['mod_SEF_textarea'])) ? htmlspecialchars($_POST['mod_SEF_textarea']) : "Default Text";

So if mod_SEF_textarea was not set, i.e. no value, the false clause would execute, which would be your default.


Any client side interaction won't use PHP - but your in client friend, JavaScript.

I wrote a jQuery plugin that does this in a cross browser fashion.

If only targeting modern standards compliant browsers, use the placeholder attribute.


You need to use the placeholder attribute. Note that it's part of the HTML5 spec, only works with the latest versions of some browsers. If you want it to work in older browsers, you'll need to do it with Javascript.

echo "<td><textarea class='SEFTextArea' name='mod_SEF_textarea' id='textarea' value=\'tester\' rows='$txtRows' cols='$txtCols' placeholder='Default text'>" . stripslashes($value) . "" . "</textarea>";

Here's an example of doing it with Javascript:

echo "<td><textarea class='SEFTextArea' name='mod_SEF_textarea' id='textarea' value=\'tester\' rows='$txtRows' cols='$txtCols' onfocus=\"if(this.value=='Default value')this.value='';\" onblur=\"if(this.value=='')this.value='Default value';\">" . stripslashes($value) . "" . "</textarea>";

Very simple.


echo "<td><textarea class='SEFTextArea' name='mod_SEF_textarea' id='textarea' value='Comments...' rows='$txtRows' cols='$txtCols' onfocus=\"if(this.value=='Comments...')this.value='';\" onblur=\"if(this.value=='')this.value='Comments...';\">" . stripslashes($value) . "Comments..." . "</textarea>";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜