Author Topic: Protect a form from special characters  (Read 611 times)

strongdevteam

  • New Member
  • *
  • Posts: 1
  • Karma: 0
    • View Profile
Protect a form from special characters
« on: May 14, 2012, 06:11:09 PM »
Hello everyone. I need help to protect my insert form from special characters as those (~!@#$%^&*()_+=][\';/.,<>., (AND CAPITAL LETTERS)) I tried everything but it didn't work. Here is a basic example of my project.
(the form work perfectly, I can see the posted messages in the database)

1st php page
PHP Code: [Select]


$HOST
="*******"
$USERNAME="*******"
$PASSWORD="********"
$DB_NAME="********"
$TABLE_NAME="******"

Mysql_connect(...)
mysql_select_db(...)

//get value from insert form
$name=$_POST['name']; 

//remove special characters
$name htmlentities($name) ;


$sql="INSERT INTO $tbl_name(name)VALUES('$name')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful". 
if($result){
echo 
"Successful";
echo 
"<BR>";
echo 
"<a href='members.php'>Back to main page</a>";
}




else {
echo 
"ERROR";
}

// close connection 
mysql_close();



2nd php page:

PHP Code: [Select]
<html>

<
form name="form1" method="post" action="insert.php" >
<
input name="name" maxlength="15" type="text" id="name"></td>                     
<
input type="submit" name="Submit" value="Submit"></td>
</
form>

</
html>





Please help me to remove special characters from being send to my MySQL database. I want to allow only those characters (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) Also, if you can tell me how to prevent my form from sql injections it will be great  If you can add a captcha to that form, it will be cool 2. thank for helping :)

sajan

  • Regular Member
  • **
  • Posts: 54
  • Karma: 4
    • View Profile
Re: Protect a form from special characters
« Reply #1 on: June 12, 2012, 04:23:35 AM »
Check the javascript below: try to type number and special charecters in texbox. Also Try backspace arrow keys :) :
Code: [Select]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
   <SCRIPT language=Javascript>
      <!--
      function ischar(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if ((charCode >= 65 && charCode <=90) ||(charCode>=97 &&charCode<=122) || charCode==8)
            return true;

         return false;
      }
      //-->
   </SCRIPT>
<body>
    <div>
<form name="form1" method="post" action="insert.php" >
<input name="name" maxlength="15" type="text" id="name" onkeypress="return ischar(event);"></td>                     
<input type="submit" name="Submit" value="Submit"></td>
</form>
      </div>
    </form>
</body>
</html>
Looking for FREEEEE HOSTING? Here it is http://www.000webhost.com/594131.html