问题描述
我正在开发允许用户向系统发送反馈的网站.我已经通过使用textarea和按钮提交创建了反馈表.最重要的是,当用户单击提交"时,如果用户输入了一些我不希望他们输入的词,则该反馈将不会发送到系统;它将提醒用户在取消提交之前放弃该单词.
I am developing website that allow user sending feedback to system. I have created feedback form by using textarea and button for submit. In most important thing is when user click on submit, if user input some words that I don't want them to input, that feedback won't send to the system; it will alert user to drop out that word before cicking submit.
从现在开始,我只创建了一个简单的代码,如果用户输入了我不希望他们以反馈形式输入的字词,则会回显一些警告.
Since now, i just create a simple code that will echo some warning if user input the word that i don't want them to input in feedback form.
这是我的代码
Here is my code
<form action="main.php" method="post">
<textarea cols='10' rows='5' name='text'></textarea>
<br/>
<input type='submit' name='add' Value='Add to list' />
</form>
<?php
if (isset($_POST['add'])) {
$banned = array('dog', 'cat', 'cow'); // Add more
$entry = $_POST['add'];
foreach($banned as $word): if (strpos($entry, $word) !== false) die('Contains banned word');
endforeach;
}
?>
这是行不通的.谁能帮我解决这个问题?
It is not work. Can anyone help me to solve this problem?
谢谢.
推荐答案
尝试一下
$entry = $_POST['text']; // Not $_POST['add'];
foreach ($banned as $word):
if (strpos($entry, $word) !== false) {
echo 'Contains banned word';
exit;
}
endforeach;
您的$entry
将是文本框值,即 $ _ POST ['text'] not
$ _POST ['add']
Your $entry
will be the text box value i.e. $_POST['text'] not
$_POST['add']
这篇关于单击提交时验证文本区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!