2015-01-08 158 views
-1

我想实现像twitter这样的后期更新系统,用户可以在160个角色中更新其状态。我想添加一些限制如果用户输入超过160的字符,则update_post.php文件必须向他/她显示警告和HTML del标签内的额外字符(+160)。 **贝娄是我迄今为止试过的代码。但它不输出任何内容!**任何帮助都非常值得!感谢如果字符多于160个字符串,则显示警告

sample_update.php

<form action="<?php echo $_SERVER['PHP_SELF'];?>"method="post"> 
    <textarea name="msg"></textarea> 
    <input type="submit"value="Post"> 
</form> 

<?php 
    if(strlen($txt)>160) { 
     echo "Your post couldn't be submitted as it contains more then 160 chrecters!"; 
     $txt=$_POST['msg']; 
     $checking=substr($txt,160); 
     echo "<del style='color:red;'>$checking</del>"; 
    } 
?> 
+0

您是否收到任何错误消息/通知?如果不。我建议开启错误报告和格式化代码,使其更具可读性 –

+1

你在PHP中有更多的东西吗?或者是所有的东西?你在哪里填写'$ txt'变量?你试过输出'$ txt'变量吗? –

+0

你真的需要这个字符长度限制与PHP完成吗? –

回答

4

$txt设为您的if语句里面需要外移动它

$txt=$_POST['msg']; 

if(strlen($txt)>160) 
{ 
    echo "Your post couldn't be submitted as it contains more then 160 chrecters!"; 
    $checking=substr($txt,160); 
    echo "<del style='color:red;'>$checking</del>"; 
} 
+2

注意:Undefined index'msg' –

+0

谢谢!你解决了它! – starkeen

+0

@AmitThakur BTW:没有人注意到'$ _SERVER ['SELF']'甚至不存在! – Rizier123

1

这应该为你工作:

$_SERVER['SELF']不存在只有$_SERVER['PHP_SELF']你也必须先作为在可以检查长度之前对变量进行签名)

<form action="<?= $_SERVER['PHP_SELF'];?>"method="post"> 
    <textarea name="msg"></textarea> 
    <input type="submit"value="Post"> 
</form> 

<?php 

    if(!empty($_POST['msg'])) { 
     $txt = $_POST['msg']; 

     if(strlen($txt) > 160) { 
      echo "Your post couldn't be submitted as it contains more then 160 chrecters!"; 

      $checking = substr($txt,160); 
      echo "<del style='color:red;'>$checking</del>"; 
     } 
    } 



?> 
2

您应该了解有关未定义变量的通知。根据我/我们可以看到的$txt$txt是在您的if循环中定义的。我已将您的代码修改为最小行数,但同样有效。

if (isset($_POST['msg'])){ 
    if (strlen($_POST['msg']) > 160){ 
     echo "Your post could not be submitted as it contains more than 160 characters!"; 
     echo "<del style='color:red;'>".substr($_POST['msg'],160)."</del>"; 
    } 

} 

我还包裹着你的$ _ POST围绕isset声明,如果它做任何事情之前设置,这将检查。如果没有设置,那么代码将不会被执行,并引发一些烦人的错误消息

所有的
0

首先,你必须使用$_SERVER['PHP_SELF']代替$_SERVER['SELF']

您可能希望将一些你的条件了,所以你可以用别的东西检查。此外,将用户输入的文本插入到textarea中是一种很好的做法,因此用户dosnt必须重新输入文本。

<?php 
    $maxlen = 160; 
    $txt=(isset($_POST['msg'])) ? $_POST['msg'] : ""; 
    $check = strlen($txt) > $maxlen; 
?> 

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> 
<textarea name="msg"><?php echo $txt; ?></textarea> 
<input type="submit" value="post"> 
</form> 
<?php 
if ($check){ 
    echo "Your post couldn't be submitted as it contains more then $maxlen chrecters!"; 
    $checking = substr($txt,$maxlen); 
    echo "<del style='color:red;'>$checking</del>"; 
} else { 
    echo "You are good to go ma man - do something"; 
} 
?> 
相关问题