vendredi 29 mai 2015

How HTML FORM take only letters not numbers?

In may Addvalue.php file 3 fields id, name, color… in id only numbers allow, in name and color only letter is allowed. Values successfully inserted into my database, but the problem is when user write NAME and COLOR in the HTML FORM field only LETTERS allow to write, if write any NUMBER message appears "Only letter allowed".... But it takes both Numbers and Letter. here is my both file code:

Addvalue.php code:

<form  method="post" action="addh.php">
<input type="number" name="id"  id="id" required /> <span class="error">* </span> 
<input type="text" name="name" id="name" required /> <span class="error">* </span>
<input type="text" name="color" id="color" />
<?php
include("config.php");
if (isset($_POST["submit"])) {
                 $id = $_POST["id"];
      $name = $_POST["name"];
      $color = $_POST["color"];     
     if (!preg_match("/^[a-zA-Z -]+$/",$name)) {
       echo "Only letters allowed"; 
     } 
 if(!preg_match("/^[a-zA-Z -]+$/",$color)) {
       echo "Only letters allowed"; 
     }
     }
?>

addh.php code:

<?php
include("config.php");
    $sql="INSERT INTO honda (id, name, color)
VALUES ('$_POST[id]','$_POST[name]','$_POST[color]')";
    $result = mysqli_query($link,$sql) or die(mysqli_error($link));
    echo "Values Inserted";
?>




Aucun commentaire:

Enregistrer un commentaire