473,599 Members | 3,183 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP Form Validation

4 New Member
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 1568
Markus
6,050 Recognized Expert Expert
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 Recognized Expert Expert
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
3mman321
4 New Member
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 Recognized Expert Expert
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
3mman321
4 New Member
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
3mman321
4 New Member
you're in england too :D sry
Nov 23 '08 #7
nathj
938 Recognized Expert Contributor
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
9959
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 performing the validation check on the values that were entered into the form, I simply repost the form to perform the PHP validation. If any of the values that have been entered into the form are incorrect, I display a warning message on the screen...
16
2221
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 individual fields, such as an "Email Validation Script" or a "Phone Validation Script". Is it ok to put all these scripts on page as they are or should they be joined in some way together to be one script? I'm a total JavaScript newbie and am completely...
9
4163
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 honest I usually hire someone to do it for me, grab predone scripts and kind of hack out the parts that I need, or just do very minimal validation (e.g. this is numeric, this is alpha-numeric, etc.)
27
4719
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 appears that the data goes straight to the processing page, rather than the javascript seeing if data is missing and popping up an alert. I thought it may be because much of the form is populated with data from the db (lists, etc.), but when I leave...
11
2986
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 certain fields match certain criteria, and inform the user in different ways when the data is wrong (offcourse, this will be checked on posting the data again, but that's something I've got a lot of experience with). Now, offcourse it's...
0
7993
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7916
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8404
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8268
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6729
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5866
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3900
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
1252
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.