PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 19, 2022

[FIXED] How can i add Bad word filter in this form function (if bad word exist it wont submit)

 February 19, 2022     codeigniter, javascript, php     No comments   

Issue

This is the script i tried it worked and gives alert but at last form gets submitted i want to stop that if bad word exist in the textarea . the form should not get submitted and the people get the alert function.. im using CODEIGNITOR framework.

JAVSCRIPT:

<script type="text/javascript">
var swear_words_arr=new Array("fuck","kill","ugly");

var swear_alert_arr=new Array;
var swear_alert_count=0;
function reset_alert_count()
{
 swear_alert_count=0;
}
function validate_text()
{
 reset_alert_count();
 var compare_text=document.form1.msg_text.value;
 for(var i=0; i<swear_words_arr.length; i++)
 {
  for(var j=0; j<(compare_text.length); j++)
  {
   if(swear_words_arr[i]==compare_text.substring(j,(j+swear_words_arr[i].length)).toLowerCase())
   {
    swear_alert_arr[swear_alert_count]=compare_text.substring(j,(j+swear_words_arr[i].length));
    swear_alert_count++;
   }
  }
 }
 var alert_text="";
 for(var k=1; k<=swear_alert_count; k++)
 {
  alert_text+="\n" + "(" + k + ")  " + swear_alert_arr[k-1];
 }
 if(swear_alert_count>0)
 {
  alert("The message will not be sent!!!\nThe following illegal words were found:\n_______________________________\n" + alert_text + "\n_______________________________");
  document.form1.msg_text.select();
 }
 else
 {
  document.form1.submit();
 }
}
function select_area()
{
 document.form1.msg_text.select();
}
window.onload=reset_alert_count;
    </script>

is there any possible to convert this in the php function.

MODEL FUNCTION:

    public function create_msg($msg_text, $user_id){

        $data = array(
            'msg_text' => $msg_text,
            'user_id' => $user_id
        );

        if( $this->db->insert('swall_message', $data) ){
            return true;
        }else{
            return false;
        }
   }

TEXTAREA CODE:

 <form action="<?php echo base_url('user/'.$user->user_uid);?>" method="POST" name="form1">
   
   

<div class="form-group">



    <textarea name="msg_text" onClick="select_area()" cols="30"  class="form-control" rows="5" minlength="1" maxlength="5000" required=""></textarea>
    
    
    
    
    <input type="hidden" name="user_id" value="<?php echo $user->user_id;?>">
</div>
 <div class="col-12">
                                    <label class="custom-label"><?php if(isset($msg)) echo $msg;?></label>
                                </div>



<button type="submit" onClick="validate_text();" class="btn btn-block text-uppercase">submit</button>
</form>

Solution

Filter data in php code

This code is untested. Refer here

<?php

$msg_text = 'fuck';
 $data = array(
            'msg_text' => $msg_text,
            'user_id' => $user_id
        );
$swear_words_arr=array("fuck","kill","ugly");

if(in_array($data['msg_text'], $swear_words_arr)){
    echo "failed";
    return false;

}

// else this is true. your db code below

echo "passed";

return true;

?>


Answered By - Alaksandar Jesus Gene
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing