Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 单击提交时验证文本区域_Php_Javascript_Html - Fatal编程技术网

Php 单击提交时验证文本区域

Php 单击提交时验证文本区域,php,javascript,html,Php,Javascript,Html,我正在开发一个允许用户向系统发送反馈的网站。我使用文本区和提交按钮创建了反馈表。最重要的是,当用户点击submit时,如果用户输入了一些我不想让他们输入的单词,反馈将不会发送到系统;它会提醒用户在点击提交之前退出该单词 从现在起,我只创建了一个简单的代码,如果用户输入了我不希望他们在反馈表单中输入的单词,它将响应一些警告 这是我的代码 这不是工作。有人能帮我解决这个问题吗 提前感谢。试试看 $entry = $_POST['text']; // Not $_POST['add'];

我正在开发一个允许用户向系统发送反馈的网站。我使用文本区和提交按钮创建了反馈表。最重要的是,当用户点击submit时,如果用户输入了一些我不想让他们输入的单词,反馈将不会发送到系统;它会提醒用户在点击提交之前退出该单词

从现在起,我只创建了一个简单的代码,如果用户输入了我不希望他们在反馈表单中输入的单词,它将响应一些警告

这是我的代码



这不是工作。有人能帮我解决这个问题吗

提前感谢。

试试看

$entry = $_POST['text'];    // Not $_POST['add'];
foreach ($banned as $word):
    if (strpos($entry, $word) !== false) {
        echo 'Contains banned word';
        exit;
    }
endforeach;
您的
$entry
将是文本框值,即$\u POST['text']
不是
$\u POST['add']

在数组函数中使用


与每次输入文本时都将代码提交给服务器不同,我建议您在客户端使用Javascript,并且只有在通过验证后才提交代码

HTML


//也适用于文本区域。
Javascript

validate_function(a)
{
r=0;
banned = array('dog', 'cat', 'cow');
var lines = a.val().split('\n');
for(var x=0;x<banned.length;x++) 
{    
for(var i = 0;i < lines.length;i++)
{
if (lines[i].indexOf(banned[x]) > -1)
 {
      r=1;
      break;
}
 else 
{

}

}
if(r==1)
break;
}
if( r==0)
return true;
else 
return false;
}
validate_函数(a)
{
r=0;
禁用=数组('dog'、'cat'、'cow');
var lines=a.val().split('\n');
对于(变量x=0;x-1)
{
r=1;
打破
}
其他的
{
}
}
如果(r==1)
打破
}
如果(r==0)
返回true;
其他的
返回false;
}

您想进行文字审查。要完成此操作,请使用str_ireplace()函数。我在下面给出了一个示例代码。这个函数的作用是用另一个词替换这个词

<?php
$find=array('galaxy', 'planet', 'moon');
$replace=array('spiral', 'earth', 'satelite');
if (isset($_POST['userinput'])&&!empty($_POST['userinput']))
{
$user=$_POST['userinput'];
$user_new=str_ireplace($find, $replace, $user);
echo $user_new;

}
?>
<hr>
<form action="index.php" method="POST">
<textarea cols="50" rows="10" name="userinput"><?php echo $user; ?></textarea><br />
<input type="submit" value="submit" />
</form>




如果用户有任何禁止的单词,您只需停止表单提交

<form action="main.php" method="post">
<textarea cols='10' rows='5' name='text' id='text'></textarea>
<br/>
<input type='submit' name='add' Value='Add to list' onclick='validate()' />
</form>


在javascript中

<script>
function validate() {
   var text = document.getElementById('text').value;
   var banned = new Array('dog', 'cat', 'cow');
   for (var x in banned) {
      if ( strpos(text,banned[x],0) ) {
            continue; // will stop the loop
            return false; // will stop everything and will not let the form post to php

      }
   }
   return true; // will return true and will post the form to php
}

// Custom Function
function strpos (haystack, needle, offset) { // http://phpjs.org/functions/strpos/
  var i = (haystack + '').indexOf(needle, (offset || 0));
  return i === -1 ? false : i;
}
</script>

函数验证(){
var text=document.getElementById('text')。值;
var banked=新数组('dog'、'cat'、'cow');
for(禁用中的var x){
if(strpos(文本,禁止[x],0)){
continue;//将停止循环
return false;//将停止所有操作,并且不会让表单发布到php
}
}
return true;//将返回true并将表单发布到php
}
//自定义函数
函数strpos(草垛、针、偏移){//http://phpjs.org/functions/strpos/
变量i=(干草堆+“”).indexOf(针,(偏移量| | 0));
返回i==-1?false:i;
}
我希望这会有所帮助


PS:strpos的函数
strpos
是从谷歌亵渎检查程序中获取的。有很多插件可以帮你完成这项工作。@Sovat may我的回答会对你有所帮助
<?php
if(isset($_POST['add'])){
    $banned = array('dog', 'cat', 'cow'); // Add more
    $entry = $_POST['text'];

    if(in_array($entry,$banned))
    {
        die('Contains banned word');
    }
}
?>
<?php
$find=array('galaxy', 'planet', 'moon');
$replace=array('spiral', 'earth', 'satelite');
if (isset($_POST['userinput'])&&!empty($_POST['userinput']))
{
$user=$_POST['userinput'];
$user_new=str_ireplace($find, $replace, $user);
echo $user_new;

}
?>
<hr>
<form action="index.php" method="POST">
<textarea cols="50" rows="10" name="userinput"><?php echo $user; ?></textarea><br />
<input type="submit" value="submit" />
</form>
<form action="main.php" method="post">
<textarea cols='10' rows='5' name='text' id='text'></textarea>
<br/>
<input type='submit' name='add' Value='Add to list' onclick='validate()' />
</form>
<script>
function validate() {
   var text = document.getElementById('text').value;
   var banned = new Array('dog', 'cat', 'cow');
   for (var x in banned) {
      if ( strpos(text,banned[x],0) ) {
            continue; // will stop the loop
            return false; // will stop everything and will not let the form post to php

      }
   }
   return true; // will return true and will post the form to php
}

// Custom Function
function strpos (haystack, needle, offset) { // http://phpjs.org/functions/strpos/
  var i = (haystack + '').indexOf(needle, (offset || 0));
  return i === -1 ? false : i;
}
</script>