PHP Form Validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 3mman321
    New Member
    • Nov 2008
    • 4

    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:

    Code:
    <?php
    
    $msg .= "Full name :    	".$_POST["Name"]."\n";
    $msg .= "Other Info : ".$_POST["textfield"]."\n";
    
    $recipient = "<email removed>";
    $subject = "My Form";
    $mailheaders = "From: NeunieFlicks.com <email removed> \n";
    
    mail($recipient, $subject, $msg, $mailheaders);
    ?>
    If any one could help id be very greatful. Thankyou.
    Last edited by Markus; Nov 21 '08, 07:36 PM. Reason: added # tags, removed email address
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    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.

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      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.

      Comment

      • 3mman321
        New Member
        • Nov 2008
        • 4

        #4
        Thank you for your advise on using this forum Moderator :)

        I have the following javascript embeded in my HTML code:

        Code:
        <script language="Javascript">
        <!--
        function doClear(theText) 
        {
             if (theText.value == theText.defaultValue)
         {
                 theText.value = ""
             }
         }
        function MM_validateForm() { //v4.0
          if (document.getElementById){
            var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
            for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
              if (val) { nm=val.name; if ((val=val.value)!="") {
                if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
                  if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
                } else if (test!='R') { num = parseFloat(val);
                  if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
                  if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
                    min=test.substring(8,p); max=test.substring(p+1);
                    if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
              } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
            } if (errors) alert('The following error(s) occurred:\n'+errors);
            document.MM_returnValue = (errors == '');
        } }
        //-->
        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')

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          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?

          Comment

          • 3mman321
            New Member
            • Nov 2008
            • 4

            #6
            Validation

            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.

            Comment

            • 3mman321
              New Member
              • Nov 2008
              • 4

              #7
              you're in england too :D sry

              Comment

              • nathj
                Recognized Expert Contributor
                • May 2007
                • 937

                #8
                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:
                Code:
                !empty($lcEmail) && ereg('^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$', $lcEmail))
                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

                Comment

                Working...