Form validation in PHP


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Form validation in PHP
# 1  
Old 07-25-2010
Form validation in PHP

My form validation script looks like this of the form like this....



Code:
<html>

<form method="POST" action="">
<table >

<tr>
<td >Name:</td>
<td ><input type="text" name="name" size="30%"></td>
</tr>

<tr>
<td >Phone:</td>
<td ><input type="text" name="phone" size="30%"></td>
</tr>

<tr>
<td >Email:</td>
<td ><input type="text" colspan="2" name="email" size="50%"></td>
</tr>

<tr>
<td>Your Message:</td><td><textarea name="comments" cols="40" rows="4">
</textarea></td>
</tr>

<tr>
<td colspan="2"><input type="submit" name="submit" STYLE="background-color:#b74b4e; color:white">
</td>
 
</tr>
</table>
</form>

</html>




Code:
<?php

header("Cache: no-cahce");
error_reporting('E_DEPRECATED');
  if (isset($_POST['submit']))
    {
    
 $to="vivekttl@gmail.com";
 
 
 
 


 #Validation for Name


$name=$_POST['name'];

 if (ereg('[^A-Za-z]',$name) || $name == "")
 { 
 echo "Enter Name correctly !</br>" ; 
 }
 else
 { 
 $name=$_POST['name'];
 } 
 
 
 
 
#Validation for phone number
 $phone=$_POST['phone'];
  if((!is_numeric($_POST['phone'])) || $phone == "")
 {
 echo "Enter Phone no Correctly !</br>";
 
 }
 else
 {
  $phone=$_POST['phone'];

 }
 
 
 
 
 # Validation for email
 
 $email = $_POST['email'];
 if(checkEmail("somebody@gmail.com") == FALSE) 
{
   echo "E-mail entered is not valid.";
} 
else 
{
   echo "E-mail entered is valid.";
}


function checkEmail($email) 
{
   if(eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)) 
   {
      return FALSE;
   }

   list($Username, $Domain) = split("@",$email);

   if(getmxrr($Domain, $MXHost)) 
   {
      return TRUE;
   }
   else 
   {
      if(fsockopen($Domain, 25, $errno, $errstr, 30)) 
      {
         return TRUE; 
      }
      else 
      {
         return FALSE; 
      }
   }
}





 

 
 #Validation for Comments
 $comment=$_POST['comments'];
 if(ereg('[^A-Za-z]',$comment) || $comment == "")
 {
  
   echo "Enter Message Please !";
 }
 else
 {
 $comment=$_POST['comments'];
  }
  
  
 $body="Name = ".$name."\nPhone = ".$phone."\nEmail = ".$email."\nComments : ".$comment."\n";
 
 //if (mail($to, "Feedback", $body)) {
   //echo("<p>Message successfully sent!</p>");
 // } else {
 //  echo("<p>Message delivery failed...</p>");
 // }  
  }
 
 
 
?>





Can any one plz correct me where I went wrong as the <textarea> tag & email is not working....

Last edited by pludi; 07-25-2010 at 06:09 PM.. Reason: code tags, please...
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Web Development

Php help to copy form field if empty

I have an input form with several fields. What I would like to achieve is to auto populate or copy certain fields if they are empty when the form is submitted. I would like to use php if not then javascript but not jquery if possible - I have sort of had a go but I really have no idea... (4 Replies)
Discussion started by: barrydocks
4 Replies

2. UNIX for Dummies Questions & Answers

How to submit form on an php webpage from command line?

Hello, i have page domain.com/form.php the form fields on form.php are named: name=ipaddress name=port and submit button is named: submit i want to ask how the linux command will look like to submit the form filled with: ipaddress: 127.0.0.1 port: 80 I tried various curl and... (5 Replies)
Discussion started by: postcd
5 Replies

3. Web Development

Attachement Form Mail in php

Dear Sir, I have used a php form mail code in a website. The problem I am facing is: the text in mail body is getting injected to a configured yahoo mail ID, but no attachement file. I need the attachement to go to the configured yahoo mail ID. The code I have used : <?php ... (1 Reply)
Discussion started by: swapan
1 Replies

4. Web Development

Validation in php help !

I am validating a form using php script and I want to "echo" the error message near to the text box itself & not below all the controls....Can I Position the display messages ?..Pls help me... (2 Replies)
Discussion started by: gameboy87
2 Replies

5. Shell Programming and Scripting

Pass variable to php from html form

I am sure this is easy but I can't figure it out... Here is the form. <?php $searchString = $_POST; if (!isset($_POST)) ?> <html> <head> <title>Personal INFO</title> </head> <body> <form method="post" action="search.php"> <input type="text" size="20" maxlength="20" name="search">... (1 Reply)
Discussion started by: mrlayance
1 Replies

6. Shell Programming and Scripting

PHP Help with Form Submit

Hi, I have a custom HTML form that has a couple radio buttons and a text field that requires a number. I'm not a php programmer and could use some help with putting together php code to calculate a total based on the radio button selection and the text field number. ... (3 Replies)
Discussion started by: nck
3 Replies

7. Shell Programming and Scripting

form validation with perl

Hey guys, I'm just messing around with a perl webpage. The idea is to make a simple validation form that will later insert a record into my DVD database. it's all very basic at the moment, and I worked up my script from the form validation example I found on this website:... (5 Replies)
Discussion started by: LNC
5 Replies

8. UNIX for Advanced & Expert Users

Changing Unix form to Microsoft Word form to be able to email it to someone.

Please someone I need information on how to change a Unix form/document into a microsoft word document in order to be emailed to another company. Please help ASAP. Thankyou :confused: (8 Replies)
Discussion started by: Cheraunm
8 Replies
Login or Register to Ask a Question