1

I have html form and I want to validate all fields until user insert the correct details. My current code is not validating the form.

My code:

ex

<html>
<body>

<form method ="POST" >
<label>Company*</label> <br/>
<input type="text" name="company" class="form-control" style="width:40%" required data-parsley-minlength="10">
<label>Address*</label> <br/>
<textarea class="form-control" name="address" rows="3"style="width:40%;resize:none"  required ></textarea>
<label>Website(URL)</label><br/>
<input type="text" name="url" class="form-control" style="width:40%">
<label>Key Contact Person*</label> <br/>
<input type="text" name="contact" class="form-control" style="width:40%" required>
<label>Email</label> <br/>
<input type="text" name="email" class="form-control" style="width:40%"  pattern="^\w+([.-]?\w+)*@\w+([.-]?\w+)*(.\w{2,3})+$">

</form>

</body>
</html>

I want only the string value with email validation but unfortunately it is not working.

php validation:

if(isset($_POST['register']))
    {

       if($company == "" && $address =="" && $url =="" && $contact =="" && $email =="" && $requirements=="")
         {

            $alert = '<script> bootbox.alert("Please fill the form Correctly.")</script>';
         }
         else
         {
              if($company !=="" || $address !=="")
              {

                $alert = '<script> bootbox.alert("Please fill the form Correctly.")</script>';


              }


         }

        $company = $_POST['company']."\n";
        $address = $_POST['address']."\n";
        $url     = $_POST['url']."\n";
        $contact = $_POST['contact']."\n";
        $email= $_POST['email']."\n";

        $msg =  'Company Name: '.$company;
        $msg.=  'Address: '.$address;
        $msg.=  'URL: '.$address;
        $msg.=  'Contact: '.$contact;
        $msg.=  'Email: '.$email;

            }
?>
2
  • You need to just validate Email in the form, that is it? Commented Dec 16, 2014 at 6:48
  • @nuke_infer your first condition where you check your address and so on is allways true. you have to define the variables before you check them. i.e. $company = $_POST['company'] and if they are defined, your condition is most likely always false because you use and (you should use or i.e. ||). Commented Dec 16, 2014 at 7:01

3 Answers 3

1

First of all you are trying to validate $_POST['register'] which is not part of your HTML code. you might wanna give your form a name . And I think it's best if you validate your code with Javascript (preferably with JQuery) at client side level first.

Check if the fields like $_POST['email'] are set and then see if the content is empty.

You have to tell your form the script that will take care of the form.

If your php code is in a separate file,

<form action="my_validator.php" method="post"> 

or else,

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Sign up to request clarification or add additional context in comments.

1 Comment

i did all posibilities
0

From the code you have written, I am not seeing a form field "register", which means the validation would not work.

I am not sure what the register is supposed to be doing but

<input type=hidden name=register value=1>

That might need to be added to the form

1 Comment

and where is this button?
0

For checking if the value is a string or not, you can use php is_string(value) function which it returns TRUE or False.

You can learn more about it here Also you can learn how to validate user input with php here: http://www.w3schools.com/php/php_form_validation.asp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.