How to add "default text" in php form

I would like the following code to change so that the text area has a default value that disappears when it is focused.

if ($txtActive != 'N') {
    $value = (isset($_POST['mod_SEF_textarea'])) ? 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";
+3
source share
4 answers

If I understood the question correctly, you would change this line like this:

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

So, if mod_SEF_textarea has not been installed, i.e. no value, a false sentence would be executed, which would be your default.

+1
source

You need to use the attribute placeholder. Please note that this part of the HTML5 specification only works with the latest versions of some browsers. If you want it to work in older browsers, you need to do this using Javascript.

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

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>";

.

+1

PHP - , JavaScript.

jQuery, .

If you use only modern standards-oriented browsers, use the attribute placeholder.

+1
source
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
source

Source: https://habr.com/ru/post/1793483/


All Articles