PHP Contact Form - Validation Issues

Discussion in 'PHP' started by GAdsense, Jul 5, 2008.

  1. #1
    I have two forms

    http://mylotlovers.info/form/check/samplecontact.html
    http://mylotlovers.info/form/check/sampleQuery.html

    The forms work fine, but I need to do the validation for both forms. For sample contact, I just need to make sure same email address are typed in both the email and confirm email fields. Making the subject and question fields required.

    As for the samplequery form, I like those which which has a * to be required.

    Now, which validation to use? Javascript (client side) or PHP (server-side) or both? .....

    Can someone help me do this please?
     
    GAdsense, Jul 5, 2008 IP
  2. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #2
    The client site may be used for ease of user to fill the form fully, Server-end validation is a MUST, and must be implemented. Becuase there are 100s of software and programmers who can submit data to your script without even visiting your form page.

    Secondly, please tell me more about your forms. What exactly you want next?

    regards
     
    Vooler, Jul 5, 2008 IP
  3. GAdsense

    GAdsense Well-Known Member

    Messages:
    1,247
    Likes Received:
    60
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Well, I want two things.

    1) Adding validation to both the contact and query forms. In contact form, the user must type subject, question and email address (in both email and confirm email box, same address). In query form, the user must fill those fields which has * like First Name, Last Name, Street address and so on...

    2) The current form code is not w3c validating, because some divs are missing and span valign etc... making the code to validate.

    That's it. Thank you for replying Vooler
     
    GAdsense, Jul 5, 2008 IP
  4. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #4
    I can give you tips, regarding Client-end validation and server-end validation, I go create a sample orm for you.

    regards
     
    Vooler, Jul 5, 2008 IP
  5. GAdsense

    GAdsense Well-Known Member

    Messages:
    1,247
    Likes Received:
    60
    Best Answers:
    0
    Trophy Points:
    140
    #5
    My friend has done client-side validation but it only works in IE, not FF. He did server side validation too, but there are some issues.
     
    GAdsense, Jul 5, 2008 IP
  6. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #6
    Please first test the script and then integarte it with your exisitng module.

    It is working with IE, FF, and KMelion. You can get idea from this:


    Client-End part:

    <form name=frx method=post action="thescript.php">
    Subject * <input type=text maxlength=100 name=subject id=subject>
    Enter Email * <input type=text maxlength=100 name=email1 id=email1>
    confirm Email * <input type=text maxlength=100 name=email2 id=email2>
    
    <script>
         function trim(str) { return str.replace(/^\s*([\S\s]*?)\s*$/, '$1'); }
         var star_fields = new Array(
               'subject,Please enter subject','email1,Please enter email address','email2,Please enter confirm email address.'
         );
         var eq_checks = new Array(
         );
         function check_fields() {
    		for(var i=0; i<star_fields.length; i++) {
    			var temp = star_fields[i].split(',');
    			if( trim(document.getElementById(temp[0]).value) == '') {
    				alert(temp[1]);
    				document.getElementById(temp[0]).focus;
    				return false;
    			}
    		}       
    		//field equality check, custom
    		if( trim(document.getElementById('email1').value) != trim(document.getElementById('email2').value)) {	
    			alert('Confirm email address is not same as actual one');
    			document.getElementById('email2').focus();
    			return false;
    		}	
    		frx.submit();
    		return true;
         }
    </script>
    
    <input type=button value="Submit" onclick="javascript:check_fields();">
    </form>
    HTML:

    Server-end thescript.php

    <?
    
    	extract($_POST);
    	
    	#fields to keep check on
    	$star_fields = Array( 'subject', 'email1', 'email2' );
    	
    	#check necessary data
    	
    	foreach($star_fields as $v)
    		if(!isset($_POST[$v]))
    			die("Invalid form data received!");
    			
    	#all is fone, now check empty data
    
    	foreach($star_fields as $v)
    		if(trim(${$v}) == "")
    			die("Empty data in one of fields! probably skipped form, using submitter");
    			
    	#custom checks of email address
    	if(!valid_email(trim($email1)) or !valid_email(trim($email2)))	
    		die("One/both of email addresses, is not a valid email!");
    
    	if(trim($email1) != trim($email2))	
    		die("Retyped email address mismatch");
    		
    		
    		
    	# NOW PROCESS YOUR REQUEST HERE
    	
    	#@mail(...........................);	
    	
    		
    
    	#being used function for email validity
    	function valid_email($email) { return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email); }
    
    ?>
    PHP:

    I hope it helps.

    regards
     
    Vooler, Jul 5, 2008 IP