2012-02-14 41 views
0

我有一个textarea字段,当字段为空时,如果该字段不为空以添加另一个php代码,则希望添加文本或php代码。例如:用php检查textarea

如果该字段为空>做一些别的.....>做我不擅长PHP的东西...

,所以我会很高兴的任何帮助。 谢谢。

p.s.Sourry for my english。

回答

2

假设您从表单发布此信息,则可以执行以下操作。

我会因此更容易理解很长的路要走写了这一点:

if (!empty($_POST['TEXTAREA_NAME']){ 
    // If the textarea is set then do something here 
} else { 
    // If it is NOT set it then it will do the code here 
} 
+1

你需要使用'empty'和'isset'来符合@ Vendeta的标准。我已经为你添加了它。 – Treffynnon 2012-02-14 09:40:44

+0

谢谢@Treffynnon。我们布赖顿人需要坚持到一起。 – SpaceBeers 2012-02-14 09:42:16

+0

好吧,也许我的问题很愚蠢,但我必须输入TEXTAREA_NAME(文本区域html“name”attrybute或sth else)? – Newbie 2012-02-14 09:48:30

2
<?php echo isset($_POST['texarea_name']) && !empty($_POST['texarea_name']) ? 'textarea not empty' : 'texarea is empty'; ?> 
+0

为什么要投票?不回答这个问题? – Vytautas 2012-02-14 11:08:55

+0

它的工作原理,但我检查了上述作为答案,因为它更容易理解 – Newbie 2012-02-14 17:26:31

+0

但有人下了投票我的答案我不明白为什么。只是忘了它.. – Vytautas 2012-02-15 07:31:41

1

为了测试是否外变量为空或没有,你可以用空字符串进行比较

if ($_POST['text'] === '') { 
    echo 'there was something'; 
} else { 
    echo 'nothing to say'; 
}