1

我编写了一个以红色显示错误的 php 代码。但不知何故,这似乎行不通。这是我的代码:

<?php
...    
if(!$name || !$email || !$contact || !$itemid){
                        //if not display an error message
                        echo "<span style="color: red;" /><center>Fields marked with <strong>&#40; &#42; &#421</strong> are mandatory!</center></span>";
                        }else{...


?>
4

6 回答 6

3

做这样的事情——

echo '<span style="color: red;" /><center>Fields marked with <strong>&#40; &#42; &#421</strong> are mandatory!</center></span>';

您的""报价相互矛盾

于 2012-06-14T05:53:56.990 回答
2
    <?php
...    
if(!$name || !$email || !$contact || !$itemid){
                        //if not display an error message
                        echo "<span style='color: red;' /><center>Fields marked with <strong>&#40; &#42; &#421</strong> are mandatory!</center></span>";
                        }else{...


?>

试试这个,你正在关闭报价

于 2012-06-14T05:54:10.280 回答
2

问题是您的 PHP 表达式中未转义的引号。

echo "<span style="color: red;" />...
                //^ Right here        

因为,您的 PHPecho语句也以相同的引号开头,即".

以下是您可以解决此问题的不同方法:

  1. 使用混合引号

    echo "<span style='color: red;' />...
          // Single quote in the HTML part
    
  2. 转义引号

    echo "<span style=\"color: red;\" />...
         // Escape the quotes so that it gets treated as string rather than a modifier
    
于 2012-06-14T06:02:10.603 回答
0

您正在生成无效的 HTML。一个<span>元素不能包含一个<center>元素。<center>浏览器可能会通过将元素移动到外部来纠正错误<span>(因此应用于跨度的样式规则不会继承到<center>元素中)。

生成有效的标记(并将您的样式规则移动到样式表)。

echo "<p class='error'>Fields marked with <strong>&#40; &#42; &#421</strong> are mandatory!</p>";
于 2012-06-14T05:53:30.907 回答
0

你的报价是矛盾的。要么转义它们,要么使用单引号和双引号

 echo '<span style="color: red;" /><center>Fields marked with <strong>&#40; &#42; &#421</strong> are mandatory!</center></span>';
于 2012-06-14T05:53:55.600 回答
0

用颜色属性中的单引号修改并删除“中心”, echo "<span style='color: red;' />

于 2012-06-14T05:55:38.363 回答