473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

PHP Form Validation

Hi there,
I wish to have a PHP form for which users enter thier name and email address. On clicking submit the form checks for any empty fields or erraneous email address (without the @ for example). If validation is OK, then sends to my email.

I have no idea about how to go about the validation in PHP, my code for the form is here:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $msg .= "Full name :        ".$_POST["Name"]."\n";
  4. $msg .= "Other Info : ".$_POST["textfield"]."\n";
  5.  
  6. $recipient = "<email removed>";
  7. $subject = "My Form";
  8. $mailheaders = "From: NeunieFlicks.com <email removed> \n";
  9.  
  10. mail($recipient, $subject, $msg, $mailheaders);
  11. ?>
If any one could help id be very greatful. Thankyou.
Nov 21 '08 #1
7 1553
Markus
6,050 Expert 4TB
Welcome to the forums, 3mman321.

There's 3 things I need to inform you about.
  1. Post title - You have provided a title for this thread that does not suggest the problem you have. Titles help others who are searching the forums find a problem similar to theirs. I will change yours to something a bit more descriptive.
  2. Code tags - You posted code in your thread. Without code tags, the code is very hard to read, meaning that our experts won't be able to help you. Do them a favor and use [code] tags. Highlight your code, then hit the '#' key at the top of the text input area.
  3. Email addresses - I have removed the email addresses which you posted. This is a forum rule and it is in place to prevent spam from being sent to your email account.

If you're unsure about what you're posting, check out the Posting Guidelines.

Again, welcome to the forums.

Moderator.
Nov 21 '08 #2
Markus
6,050 Expert 4TB
So you already have some client-side (javascript maybe?) validation in place and you're wanting to do the same on the backend (php)? Good. Most people stop at the client-side and think that's enough - it isn't.

Anyway, all you have to do is write the PHP to do the same as the Javascript does. If you could show us your Javascript we could help you port it over.
Nov 21 '08 #3
Thank you for your advise on using this forum Moderator :)

I have the following javascript embeded in my HTML code:

Expand|Select|Wrap|Line Numbers
  1. <script language="Javascript">
  2. <!--
  3. function doClear(theText) 
  4. {
  5.      if (theText.value == theText.defaultValue)
  6.  {
  7.          theText.value = ""
  8.      }
  9.  }
  10. function MM_validateForm() { //v4.0
  11.   if (document.getElementById){
  12.     var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  13.     for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
  14.       if (val) { nm=val.name; if ((val=val.value)!="") {
  15.         if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
  16.           if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
  17.         } else if (test!='R') { num = parseFloat(val);
  18.           if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
  19.           if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
  20.             min=test.substring(8,p); max=test.substring(p+1);
  21.             if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
  22.       } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  23.     } if (errors) alert('The following error(s) occurred:\n'+errors);
  24.     document.MM_returnValue = (errors == '');
  25. } }
  26. //-->
I have some code in there that I dont think is necessary to have as I only have to fields that I want to validate (being 'Name' and 'Textfield')
Nov 21 '08 #4
Markus
6,050 Expert 4TB
Aha, I couldn't make sense of the Javascript. So do you just want to go ahead and describe what validation needs to be done on the backend?
Nov 21 '08 #5
Sorry for the delay in replying, im in UK time so waited til morning which I think is Americas evening :/

OK I wish to have validation (checking for empty or error in emails without @) done on the fields 'Email' - for their email address, 'Number' -for a telephone number and 'How'- this will be a dropdown list comprising of the following: YouTube, Google, Through a friend, Other. Other will have a textbox asking them to specify where so in the validation it would check for an empty field.

So far I've given you fields for only two that im using, please feel free to add your own names to fields I havnt specified and I will just use those.
Nov 23 '08 #6
you're in england too :D sry
Nov 23 '08 #7
nathj
938 Expert 512MB
I do this kind of thing quite a lot and it's good that you have client side validation and that you want to perform server side validation as well.

to validate the email address I would recommend using a regular expression similar to:
Expand|Select|Wrap|Line Numbers
  1. !empty($lcEmail) && ereg('^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$', $lcEmail))
  2.  
In this sample I have assumed that you have taken the email address supplied on the form out of the $_POST array and into a local variable.

This check ensures something has been entered and if so it checks that it is an email address.

In terms of name you can do something very similar. Though regular expressions can be a bot of overkill for something as simple and as variable as a name so just test the variable is not empty.

Finally, going a bit beyond the scope of the initial question have you thought about what to do if the server side validation fails? Personally I find it annoying if something happens and the server side validation fails and then I have to re-complete the form(worst-case scenario) or go back and hope the data is there to be corrected.

I recommend using some AJAX for this (technically pseudo AJAX with form submission) so that the issues can be displayed on the same page as the form and the user can then see what sis wrong, fix it and re-submit the form.

There are two ways to do this, one using an iFrame and normal form submission the other using a JS submission method.

I hope I've helped with the main question and given you some pointers for the next step. If you have any more questions pot them back here and I'll be sure to stop by.

Cheers
nathj
Nov 24 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: TG | last post by:
I have a validation form that must behave differently based on the results of a PHP validation check. I have a post command at the top of my form that calls itself. I don't leave the form when...
16
by: Hosh | last post by:
I have a form on a webpage and want to use JavaScript validation for the form fields. I have searched the web for form validation scripts and have come up with scripts that only validate...
9
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
11
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.