基于PHP if条件更改文本区域占位符文本


Changing textarea placeholder text based on PHP if condition

我有这个文本区域(以表单形式)echo'd:

<textarea id='textarea' name='msg' rows='2' maxlength='255' cols='80' placeholder=' Share a thought...'></textarea>

单击表单的提交按钮时,我希望它检查文本区域是否为空,如果为空,我希望其更改文本区域的占位符文本。

所以,像。。。

$post_msg = htmlentities(strip_tags(@$_POST['msg']));
if ($post_msg == ""){
   echo "<textarea id='textarea' name='msg' rows='2' maxlength='255' cols='80' placeholder=' please enter some text...'></textarea>";
} else {
  // if textarea has text, then execute INSERT query
}

但我显然不能把上面的语句放在文本区域所在的echo中,因为当加载页面时,文本区域总是空的。

如果按下提交按钮并且文本区域为空,我如何更改占位符文本(以及颜色为红色)?

<?php
    if ( ! empty( $_POST ) ) {
        if( empty( htmlentities( strip_tags( $_POST['msg'] ) ) ) {
            // Output updated textarea (with color, etc.)
        } else {
            // Insert
        }
    } else {
        // Output default textarea
    }
?>

我想你会的PHP代码

if(isset($_POST['button'])){//when button have pressed
if(empty($_POST['msg'])){//if msg is not empty 
    $msg = 'please enter some text...';
}
else{//msg is posted msg
    $msg = $_POST['msg'];
}
$field1 = $_POST['field1'];)//another fields red