如何在HTML、Javascript和PHP的混合中更改表单下的span标签的警告错误颜色?

huangapple go评论78阅读模式
英文:

How to change the warning error's color of a span tag under a form in a mixture of HTML, Javascript and PHP

问题

I am trying to change the color of a warning error in a span tag under a form. I wrote toggle in JS using style.color or classList to change the color, but unable to change the color. Due to the space problem, I am not writing here the JS toggle code.

Here is the code:

PHP:

$nameErr = "";
$fname = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
     if (empty($_POST["fname"])) {
          $nameErr = "Name is required";
     } else {
          $fname = cleanvar($_POST["fname"]);
          if (!preg_match("/^[a-zA-Z ]*$/", $fname)) {
               $nameErr = "Only letters allowed";
          }
     }
}

function cleanvar($userinput) {
     $inp = trim($userinput);
     $inp = stripslashes($userinput);
     $inp = htmlspecialchars($userinput);
     return $inp;
}

HTML:

<form method="post" class="formbox" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
     <div>
          <label for="fname"> First Name</label>
          <br />
          <input type="text" name="fname" value="<?php echo $fname;?>" required>
          <br />
          <span style="font-size:14px;color:#063701;"> Only letters allowed </span>
          <span style="font-size:14px;color:#FF0000;">
          <?php echo $nameErr;?>
          </span> 
     </div>
     <div>
          <input type="submit" name="submit" class="buttons"  value="Submit">
     </div>
</form>
英文:

I am trying to change the color of a warning error in a span tag under a form. I wrote toggle in JS using style.color or classList to change the color, but unable to change the color. Due to the space problem, I am not writing here the JS toggle code.

Here is the code:

PHP:

 $nameErr = &quot;&quot;;
 $fname = &quot;&quot;;
 if ( $_SERVER[ &quot;REQUEST_METHOD&quot; ] == &quot;POST&quot; ) {
      if ( empty( $_POST[ &quot;fname&quot; ] ) ) {
           $nameErr = &quot;Name is required&quot;;
      } else {
           $fname = cleanvar( $_POST[ &quot;fname&quot; ] );
           if ( !preg_match( &quot;/^[a-zA-Z ]*$/&quot;, $fname ) ) {
                $nameErr = &quot;Only letters allowed&quot;;
           }
      }
 }

 function cleanvar( $userinput ) {
      $inp = trim( $userinput );
      $inp = stripslashes( $userinput );
      $inp = htmlspecialchars( $userinput );
      return $inp;
 }

HTML:

  &lt;form method=&quot;post&quot; class=&quot;formbox&quot; action=&quot;&lt;?php echo htmlspecialchars($_SERVER[&quot;PHP_SELF&quot;]);?&gt;&quot;&gt;
      &lt;div&gt;
           &lt;label for=&quot;fname&quot;&gt; First Name&lt;/label&gt;
           &lt;br /&gt;
           &lt;input type=&quot;text&quot; name=&quot;fname&quot; value=&quot;&lt;?php echo $fname;?&gt;&quot; required&gt;
           &lt;br /&gt;
           &lt;span style=&quot;font-size:14px;color:#063701;&quot;&gt; Only letters allowed &lt;/span&gt; &lt;span style=&quot;font-size:14px;color:#FF0000;&quot; &gt;
           &lt;?php echo $nameErr;?&gt;
           &lt;/span&gt; &lt;/div&gt;
      &lt;div&gt;
           &lt;input type=&quot;submit&quot; name=&quot;submit&quot; class=&quot;buttons&quot;  value=&quot;Submit&quot;&gt;
      &lt;/div&gt;
 &lt;/form&gt;

答案1

得分: 1

我创建了自己版本的您的代码,尝试为元素添加新样式并将其制作成警告框...

&lt;form method=&quot;post&quot; class=&quot;formbox&quot; action=&quot;&lt;?php echo htmlspecialchars($_SERVER[&quot;PHP_SELF&quot;]);?&gt;&quot;&gt;
    &lt;div&gt;
        &lt;label for=&quot;fname&quot;&gt; 名字&lt;/label&gt;
        &lt;br /&gt;
        &lt;input type=&quot;text&quot; name=&quot;fname&quot; value=&quot;&lt;?php echo $fname;?&gt;&quot; required&gt;
        &lt;br /&gt;
        &lt;?php if(!empty($nameErr)){?&gt;
           &lt;span class=&quot;alert-box&quot; style=&quot;display: block; width: fit-content; border-radius: 5px; font-size:14px;color:#fff; background-color: goldenrod; padding: 10px;&quot; &gt;
                &lt;?=$nameErr?&gt;
           &lt;/span&gt;
        &lt;?php } ?&gt; 
        &lt;/div&gt;
        &lt;div&gt;
        &lt;input type=&quot;submit&quot; name=&quot;submit&quot; class=&quot;buttons&quot;  value=&quot;提交&quot;&gt;
     &lt;/div&gt;
&lt;/form&gt;

然后在JavaScript中,

setTimeout(() =&gt; {
    document.querySelector(&#39;.alert-box&#39;).style.display = &quot;none&quot;;
}, 1000);

这将在表单提交后出现一次,出现错误时。

英文:

I create my own version of your code, I tried to put a new style to the span and make it an alert box...

&lt;form method=&quot;post&quot; class=&quot;formbox&quot; action=&quot;&lt;?php echo htmlspecialchars($_SERVER[&quot;PHP_SELF&quot;]);?&gt;&quot;&gt;
    &lt;div&gt;
        &lt;label for=&quot;fname&quot;&gt; First Name&lt;/label&gt;
        &lt;br /&gt;
        &lt;input type=&quot;text&quot; name=&quot;fname&quot; value=&quot;&lt;?php echo $fname;?&gt;&quot; required&gt;
        &lt;br /&gt;
        &lt;?php if(!empty($nameErr)){?&gt;
           &lt;span class=&quot;alert-box&quot; style=&quot;display: block; width: fit-content; border-radius: 5px; font-size:14px;color:#fff; background-color: goldenrod; padding: 10px;&quot; &gt;
                &lt;?=$nameErr?&gt;
           &lt;/span&gt;
        &lt;?php } ?&gt; 
        &lt;/div&gt;
        &lt;div&gt;
        &lt;input type=&quot;submit&quot; name=&quot;submit&quot; class=&quot;buttons&quot;  value=&quot;Submit&quot;&gt;
     &lt;/div&gt;
&lt;/form&gt;

then in javascript,

setTimeout(() =&gt; {
    document.querySelector(&#39;.alert-box&#39;).style.display = &quot;none&quot;;
}, 1000);

that will appear once the form is posted and an error has occured

huangapple
  • 本文由 发表于 2023年6月5日 06:14:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定